forked from mohitsh/SPOJ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpt07y.cpp
68 lines (68 loc) · 836 Bytes
/
pt07y.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// 2008-07-23
#include <cstdio>
#ifdef ONLINE_JUDGE
#define GC getchar_unlocked
#else
#define GC getchar
#endif
int input()
{
int x;
scanf("%d",&x);
return x;
}
int main()
{
int id[10000];
int sz[10000];
int V,i,E,v1,v2;
V=input();
for (i=0; i<V; i++)
{
id[i]=i;
sz[i]=1;
}
E=input();
if (E>=V)
{
printf("NO\n");
return 0;
}
for (i=0; i<E; i++)
{
v1=input()-1;
v2=input()-1;
while (v1!=id[v1])
{
if (id[v1]!=id[id[v1]])
sz[id[v1]]-=sz[v1];
v1=id[v1]=id[id[v1]];
}
while (v2!=id[v2])
{
if (id[v2]!=id[id[v2]])
sz[id[v2]]-=sz[v2];
v1=id[v2]=id[id[v2]];
}
if (v1==v2)
{
printf("NO\n");
return 0;
}
else
{
if (sz[v1]<sz[v2])
{
id[v1]=v2;
sz[v2]+=sz[v1];
}
else
{
id[v2]=v1;
sz[v1]+=sz[v2];
}
}
}
printf("YES\n");
return 0;
}