-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathABC_231_D.cpp
84 lines (69 loc) · 1.08 KB
/
ABC_231_D.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <iostream>
#include <vector>
#include <map>
using namespace std;
struct Node
{
int parent;
int rank;
};
Node DSU[100001];
//Struktura za rad sa disjunktnim skupovima
//Slozenost: Amortizovano O(alfa(N)) po operaciji
inline void MakeSet(int x)
{
DSU[x].parent = x;
DSU[x].rank = 0;
}
inline int Find(int x)
{
if (DSU[x].parent == x) return x;
DSU[x].parent = Find(DSU[x].parent);
return DSU[x].parent;
}
inline void Union(int x, int y)
{
int xRoot = Find(x);
int yRoot = Find(y);
if (xRoot == yRoot) return;
if (DSU[xRoot].rank < DSU[yRoot].rank)
{
DSU[xRoot].parent = yRoot;
}
else if (DSU[xRoot].rank > DSU[yRoot].rank)
{
DSU[yRoot].parent = xRoot;
}
else
{
DSU[yRoot].parent = xRoot;
DSU[xRoot].rank++;
}
}
int cnt[100001];
int main() {
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++)
{
MakeSet(i);
}
string ans = "Yes";
while (m--)
{
int a, b;
cin >> a >> b;
if (Find(a) == Find(b))
{
ans = "No";
}
Union(a, b);
if (cnt[a] >= 2 || cnt[b] >= 2)
{
ans = "No";
}
cnt[a]++;
cnt[b]++;
}
cout << ans << endl;
}