Skip to content

Conversation

@alirz-pixel
Copy link
Member

🚀 이슈 번호

Resolve: {#2331}

🧩 문제 해결

스스로 해결:

🔎 접근 과정

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

  • 🔹 어떤 알고리즘을 사용했는지: DFS
  • 🔹 어떤 방식으로 접근했는지

해당 문제의 조건은 그래프 중에서도 "트리" 라는 것을 알려준다.

따라서 각 노드에 대해 자식노드를 쭉 타고 내려갔을 때의 weight들 중 가작 작은 값을 기록한다.
이렇게 하여 기록된 weight 들을 합산하여 해당 노드의 총 weight를 구할 수 있다.

이러한 weight들을 합산해가면서 min 값을 계산하면 된다.

⏱️ 시간 복잡도

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

  • Big-O 표기법: O(N+E)
  • 이유:

해당 문제는 단순 DFS로 풀 수 있으므로
DFS의 시간복잡도인 O(N+E)와 같다.

💻 구현 코드

#include <iostream>
#include <vector>

using namespace std;

struct Info {
	int to;
	int weight;
};

int dfs(vector<bool> &visited, vector<vector<Info>> &edges, int start) {
	visited[start] = true;

	if (start != 0 && edges[start].size() == 1)
		return edges[start][0].weight;

	int need = 0;

	for (Info edge : edges[start]) {
		if (visited[edge.to])
			continue;

		need += min(edge.weight, dfs(visited, edges, edge.to));
	}

	//cout << start << " " << need << "\n";
	return need;
}

int solve() {
	int N, M;
	cin >> N >> M;

	vector<vector<Info>> edges(N);
	vector<bool> visited(N);
	for (int i = 0; i < M; i++) {
		int from, to, weight;
		cin >> from >> to >> weight;

		from--; to--;
		edges[from].push_back({ to, weight });
		edges[to].push_back({ from, weight });
	}

	return dfs(visited, edges, 0);
}

int main() {
	int T;
	cin >> T;

	while (T--) {
		cout << solve() << "\n";
	}

	return 0;
}

@alirz-pixel alirz-pixel self-assigned this Jan 26, 2026
@alirz-pixel alirz-pixel 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