-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathe.cpp
75 lines (71 loc) · 1.28 KB
/
e.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
#include <bits/stdc++.h>
#define endl '\n'
#define eat cin
#define moo cout
#define int long long
using namespace std;
int T, N, C[200000];
pair<int, int> X[200000];
vector<int> graph[200000], nb[200000];
bool good = 1;
void dfs(int v, int cl){
if(C[v] == cl){
return;
}
if(C[v] == -1) C[v] = cl;
if(C[v] != cl){
good = 0;
return;
}
for(int c : graph[v]){
dfs(c, !cl);
if(!good) return;
}
}
int32_t main(){
eat.tie(0) -> sync_with_stdio(0);
eat >> T;
while(T--){
eat >> N;
good = 1;
for(int i = 0; i < N; i++){
graph[i].clear();
nb[i].clear();
}
for(int i = 0; i < N; i++){
int a, b; eat >> a >> b;
a--, b--;
X[i].first = a, X[i].second = b;
nb[a].push_back(i);
nb[b].push_back(i);
}
bool fd = 0;
for(int i = 0; i < N; i++){
if((int)nb[i].size() != 2){
fd = 1;
break;
}
}
if(fd){
moo << "NO" << endl;
continue;
}
for(int i = 0; i < N; i++){
int a = X[i].first, b = X[i].second;
if(nb[a][0] == i) swap(nb[a][0], nb[a][1]);
if(nb[b][0] == i) swap(nb[b][0], nb[b][1]);
graph[i].push_back(nb[a][0]);
graph[i].push_back(nb[b][0]);
}
fill(C, C+N, -1);
for(int i = 0; i < N; i++){
if(!good) break;
if(C[i] == -1) dfs(i, 0);
}
if(good){
moo << "YES" << endl;
}else{
moo << "NO" << endl;
}
}
}