-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1702E Split Into Two Sets.cpp
77 lines (66 loc) · 1.46 KB
/
1702E Split Into Two Sets.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
/*
Author : MishkatIT
Created : Saturday 27-04-2024 19:56:31
*/
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "algo/debug.h"
#else
#define debug(...) 42
#endif
using ll = long long;
using ld = long double;
const ll mod = 1e9 + 7;
const ll N = 2e5 + 10;
const ll inf = 1e9;
const ll linf = 1e18;
vector<vector<int>> adj;
vector<int> color;
bool dfs(int i, int now = 0) {
color[i] = now;
for (auto &j : adj[i]) {
if (color[j] == -1) {
if (!dfs(j, now ^ 1)) {
return false;
}
} else if (color[j] == now) {
return false;
}
}
return true;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int tc;
cin >> tc;
while (tc--) {
int n;
cin >> n;
adj.clear();
color.clear();
adj.resize(n + 5);
color.resize(n + 5, -1);
vector<int> degree(n + 5);
bool ok = true;
for (int i = 0; i < n; i++) {
int u, v;
cin >> u >> v;
ok &= (u != v);
degree[u]++, degree[v]++;
adj[u].push_back(v);
adj[v].push_back(u);
}
for (int i = 1; i <= n; i++) {
ok &= (degree[i] <= 2);
}
for (int i = 1; i <= n; i++) {
if (color[i] == -1) {
ok &= dfs(i);
}
}
cout << (ok ? "YES" : "NO") << '\n';
}
return 0;
}