백준 14675 단절점과 단절선

aiden·2025년 11월 10일

백준 문제풀이

목록 보기
8/11

문제 링크 : https://www.acmicpc.net/problem/14675
문제 풀이 :

#include <iostream>
#include <vector>
using namespace std;

vector<vector<int>>graph;

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int n;
	cin >> n;
	graph.resize(n + 1);

	for(int i = 0; i < n - 1; i++) {
		int u, v;
		cin >> u >> v;
		graph[u].push_back(v);
		graph[v].push_back(u);
	}
	int q;
	cin >> q;
	for(int i = 0; i < q; i++) {
		int t, k;
		cin >> t >> k;
		if (t == 1) {
			if (graph[k].size() > 1)
				cout << "yes" << "\n";
			else
				cout << "no" << "\n";
		}
		else
			cout << "yes" << "\n";
	}
}

트리라는 자료구조의 정의를 골똘히 생각해보면 쉬운 문제다.
단절선은 무조건 yes이며 단절점은 해당 노드가 리프 노드이냐에 따라 갈린다.

profile
All's well that ends well

0개의 댓글