사이클 게임

· 백준
코드#include using namespace std;int n,m,res,a,b,lst[500002];int find(int x) { if(x == lst[x]) return x; return lst[x] = find(lst[x]);}void merge(int x, int y) { x = find(x); y = find(y); if(x > y) lst[x] = y; else lst[y] = x;}int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n >> m; for(int i = 0; i > a >> b; if(find(a) != find(b)) { merge(a,b..
· 백준
코드 # 사이클 판별 # 무방향 그래프인 경우 유니온 파인드로 사이클을 판별할 수 있다 # 참고로 유향 그래프인 경우에는 dfs로 판별할 수 있다 import sys sys.setrecursionlimit(10**5) # 재귀 깊이 제한 input = sys.stdin.readline # 빠른 입력 n,m = map(int, input().split()) parent = [i for i in range(n)] # 집합 def find(x,lst): # 트리의 루트 노드를 찾아주는 find 함수 if x != lst[x]: lst[x] = find(lst[x], lst) return lst[x] def union(x,y,lst): # 트리를 합쳐주는 union 함수 x = find(x,lst) y = fi..