forked from sukhoeing/aoapc-bac2nd-keys
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LA6043.cpp
120 lines (99 loc) · 2.89 KB
/
LA6043.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <map>
#include <set>
#include <sstream>
#include <iomanip>
#include <cmath>
#include <bitset>
#include <assert.h>
using namespace std;
typedef long long llong;
typedef set<int>::iterator ssii;
#define Cmp(a, b) memcmp(a, b, sizeof(b))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define Set(a, v) memset(a, v, sizeof(a))
#define debug(x) cout << #x << ": " << x << endl
#define _forS(i, l, r) for(set<int>::iterator i = (l); i != (r); i++)
#define _rep(i, l, r) for(int i = (l); i <= (r); i++)
#define _for(i, l, r) for(int i = (l); i < (r); i++)
#define _forDown(i, l, r) for(int i = (l); i >= r; i--)
#define debug_(ch, i) printf(#ch"[%d]: %d\n", i, ch[i])
#define debug_m(mp, p) printf(#mp"[%d]: %d\n", p->first, p->second)
#define debugS(str) cout << "dbg: " << str << endl;
#define debugArr(arr, x, y) _for(i, 0, x) { _for(j, 0, y) printf("%c", arr[i][j]); printf("\n"); }
#define _forPlus(i, l, d, r) for(int i = (l); i + d < (r); i++)
#define lowbit(i) (i & (-i))
const int maxn = 1000000 + 5;
int L, P;
struct Node {
int pre, nxt;
};
Node nodes[maxn];
int twist[maxn];
int level[maxn];
void del(int x) {
level[x] = 0;
nodes[nodes[x].pre].nxt = nodes[x].nxt;
nodes[nodes[x].nxt].pre = nodes[x].pre;
}
void init() {
Set(twist, 0);
Set(level, 0);
}
int main() {
freopen("input.txt", "r", stdin);
int T;
scanf("%d", &T);
_rep(kase, 1, T) {
init();
scanf("%d%d", &L, &P);
_for(i, 0, L) {
nodes[i].pre = i - 1;
nodes[i].nxt = i + 1;
}
nodes[0].pre = L - 1;
nodes[L - 1].nxt = 0;
_rep(i, 1, P) {
int u, v;
scanf("%d%d", &u, &v);
// (u, v)
twist[u] = v;
twist[v] = u;
level[u] = 1;
level[v] = -1;
}
// then we solve the problem
_for(i, 0, L) if(level[i] == 0) del(i);
int idx = 0;
while (P) {
bool untie = false;
while (level[idx] == 0) idx++;
for(int i = nodes[idx].nxt; i != idx && untie == false; i = nodes[i].nxt) {
int u = i, v = nodes[i].nxt;
if(level[u] == level[v] && (nodes[twist[u]].nxt == twist[v] || nodes[twist[v]].nxt == twist[u])) {
del(u); del(v);
del(twist[u]); del(twist[v]);
P -= 2;
untie = true;
}
else if(twist[u] == v || twist[v] == u) {
del(u);
del(v);
P -= 1;
untie = true;
}
}
if(untie == false) break;
}
printf("Case #%d: ", kase);
if(P) printf("NO\n");
else printf("YES\n");
}
}