-
Notifications
You must be signed in to change notification settings - Fork 0
/
E.cpp
46 lines (42 loc) · 864 Bytes
/
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
#include <bits/stdc++.h>
using namespace std;
const int MAX = 200007;
const int MOD = 1000000007;
void solve() {
int n;
cin >> n;
vector<int> g[n + 1];
for (int i = 0; i < n; i++) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
for (int i = 1; i <= n; i++) {
if (g[i].size() > 2) {cout << "NO\n"; return;}
}
vector<int> col(n + 1, -1);
bool ok = true;
queue<int> q;
for (int i = 1; i <= n; i++) {
if (col[i] == -1) {
q.push(i);
col[i] = 0;
while (!q.empty()) {
int v = q.front();
q.pop();
for (int u : g[v]) {
if (col[u] == -1) {col[u] = col[v] ^ 1; q.push(u);}
else {ok &= (col[u] != col[v]);}
}
}
}
}
cout << (ok ? "YES\n" : "NO\n");
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int tt; cin >> tt; for (int i = 1; i <= tt; i++) {solve();}
// solve();
}