#include <bits/stdc++.h>
using namespace std;
#define SIZE 10000
int parent[SIZE+1];
int Find(int node) {
if(parent[node] != node) {
parent[node] = Find(parent[node]);
}
return parent[node];
}
void Union(int a, int b) {
int parent1, parent2;
parent1 = Find(a);
parent2 = Find(b);
if(parent1 != parent2)
parent[parent1] = parent2;
//or parent[parent2] = parent1
}
// here a and b can be two end points of the current edge
bool checkCycle(int a, int b) {
int parent1, parent2;
parent1 = Find(a);
parent2 = Find(b);
if(parent1 == parent2)
return true;
return false;
}
int main() {
for(int i=1;i<=SIZE;i++) {
parent[i] = i;
}
int m;
// assuming m is no of edges and no of nodes is < SIZE
cin>>m;
bool hasCycle = false;
for(int i=0;i<m;i++) {
int u,v;
cin>>u>>v;
if(!checkCycle(u, v)) {
Union(u, v);
// graph[u].pb(v);
// graph[v].pb(u);
}
else {
hasCycle = true;
break;
}
}
if(hasCycle) {
cout<<"Graph has cycle";
}
else {
cout<<"Graph doesn't have cycle";
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgojZGVmaW5lIFNJWkUgMTAwMDAKaW50IHBhcmVudFtTSVpFKzFdOwoKaW50IEZpbmQoaW50IG5vZGUpIHsKCWlmKHBhcmVudFtub2RlXSAhPSBub2RlKSB7CgkJcGFyZW50W25vZGVdID0gRmluZChwYXJlbnRbbm9kZV0pOwoJfQoJcmV0dXJuIHBhcmVudFtub2RlXTsKfQoKdm9pZCBVbmlvbihpbnQgYSwgaW50IGIpIHsKCWludCBwYXJlbnQxLCBwYXJlbnQyOwoJcGFyZW50MSA9IEZpbmQoYSk7CglwYXJlbnQyID0gRmluZChiKTsKCWlmKHBhcmVudDEgIT0gcGFyZW50MikKCQlwYXJlbnRbcGFyZW50MV0gPSBwYXJlbnQyOwkKCS8vb3IgcGFyZW50W3BhcmVudDJdID0gcGFyZW50MQp9CgovLyBoZXJlIGEgYW5kIGIgY2FuIGJlIHR3byBlbmQgcG9pbnRzIG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