Skip to content

Conversation

@KII1ua
Copy link
Member

@KII1ua KII1ua commented Jan 26, 2026

🚀 이슈 번호

Resolve: {#2331}

🧩 문제 해결

스스로 해결: ✅ 1h

🔎 접근 과정

문제 해결을 위한 접근 방식을 설명해주세요.

  • 🔹 어떤 알고리즘을 사용했는지 dp, dfs
  • 🔹 어떤 방식으로 접근했는지
    처음엔 노드 1에서 인접한 노드를 다 제거해야 안전한거 아닌가? 생각을 했는데 계속 해서 그림을 보니 깊이우선탐색을 통해 리프 노드의 가중치중 가장 작은것을 없애는 문제였다. 따라서 dp배열을 만들어 최소한의 가중치 값을 가져와 더해주며 최솟값을 출력해주었다.

⏱️ 시간 복잡도

시간 복잡도 분석을 작성해주세요.
최악의 경우 수행 시간은 어느 정도인지 분석합니다.

  • Big-O 표기법: O(TN)
  • 이유:
    T만큼 반복 후 N개의 노드 반복

💻 구현 코드

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
#define endl "\n"

struct Tree {
    int Node, left, right;
};

const int INF = 1e9;
const int MAX = 1001;
const int MOD = 1e9 + 7;
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
int T, N, M;
int dp[MAX];

int func(int node, int par, vector<vector<pii>> &v) {
    int &ret = dp[node];

    if(ret != -1) return ret;

    ret = 0;

    bool isleaf = false;

    for(auto &it : v[node]) {
        int nextnode = it.first;
        int cost = it.second;

        if(par != nextnode) {
            isleaf = true;
            ret += min(func(nextnode, node, v), cost);
        }
    }

    return ((isleaf)? ret : INF);
}

void solve(vector<vector<pii>> &v) {
    if(N == 1) {
        cout << 0 << endl;
    }
    else cout << func(1, 0, v) << endl;
}

void input() {
    cin >> T;

    while(T--) {
        cin >> N >> M;

        vector<vector<pii>> v(N+1);

        memset(dp, -1, sizeof(dp));

        for(int i = 0; i < M; i++) {
            int a, b, c;
            cin >> a >> b >> c;
            v[a].push_back({b, c});
            v[b].push_back({a, c});
        }

        solve(v);
    }
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);

    input();
}

@KII1ua KII1ua self-assigned this Jan 26, 2026
@KII1ua KII1ua linked an issue Jan 26, 2026 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

260126 : 코딩테스트

2 participants