#include <bits/stdc++.h>
#ifdef local
#define debug(x...) qqbx(#x, x)
#define pary(x...) danb(#x, x)
#define safe std::cerr<<__PRETTY_FUNCTION__<<" line "<<__LINE__<<" safe\n"
template <typename ...T> void qqbx(const char *s, T ...a) {
int cnt = sizeof...(T);
((std::cerr << "(" << s << ") = ("), ..., (std::cerr << a << (--cnt ? ", " : ")\n")));
}
template <typename T> void danb(const char *t, T L, T R) {
std::cerr << "[ " << t << " ] = [ ";
for (int f = 0; L != R; ++L) std::cerr << (f++ ? ", " : "") << *L;
std::cerr << " ]\n";
}
#else
#define debug(...) ((void)0)
#define pary(...) ((void)0)
#define safe ((void)0)
#endif // local
using namespace std;
// #include "lib1270.h"
const int maxn = 1000025, K = 4;
struct Dsu {
int pa[maxn], sz[maxn];
int deg[maxn];
bool ok;
// Dsu() { memset(pa, 0, sizeof pa); memset(sz, 0, sizeof sz); memset(deg, 0, sizeof deg); }
void init(int n) {
for (int i = 0; i < n; i++) pa[i] = i, sz[i] = 1, deg[i] = 0;
ok = true;
}
int anc(int x) { return x==pa[x] ? x : pa[x]=anc(pa[x]); }
bool join(int x, int y) {
if (++deg[x] == 3) ok = false;
if (++deg[y] == 3) ok = false;
if ((x=anc(x)) == (y=anc(y))) return ok = false;
if (sz[x] < sz[y]) swap(x, y);
return pa[y] = x, sz[x] += sz[y];
}
} dsu[K], org;
vector<int> cand;
pair<int,int> E[maxn];
vector<int> adj[maxn];
bool inCand[maxn];
int tot, N, cyc;
bool hasFour;
void Init(int _N) {
N = _N;
org.init(N);
for (int i = 0; i < K; i++)
dsu[i].init(N);
for (int i = 0; i < N; i++)
adj[i].clear();
for (int i = 0; i < N; i++)
inCand[i] = false;
cand.clear();
tot = 0;
hasFour = false;
cyc = 0;
}
void addCand(int x) {
if (cand.size() >= K) return;
if (inCand[x]) return;
inCand[x] = true;
int pos = cand.size();
cand.push_back(x);
for (int i = 0; i < tot; i++)
if (x != E[i].first && x != E[i].second)
dsu[pos].join(E[i].first, E[i].second);
}
void Link(int a, int b) {
adj[a].push_back(b);
adj[b].push_back(a);
for (int x: {a, b}) {
if (!hasFour && adj[x].size() == 4) {
hasFour = true;
debug("NO", x);
pary(cand.begin(),cand.end());
if (inCand[x]) continue;
inCand[x] = true;
debug("FOUR", x);
if (cand.empty()) cand.push_back(7122);
cand[0] = x;
dsu[0].init(N);
for (int i = 0; i < tot; i++)
if (x != E[i].first && x != E[i].second)
dsu[0].join(E[i].first, E[i].second);
}
}
if (adj[a].size() == 3) {
addCand(a);
for (int j: adj[a])
addCand(j);
}
if (adj[b].size() == 3) {
addCand(b);
for (int j: adj[b])
addCand(j);
}
E[tot++] = { a, b };
for (int i = 0; i < cand.size(); i++)
if (a != cand[i] && b != cand[i]) {
dsu[i].join(a, b);
}
if (!org.join(a, b)) {
debug(cyc, a, b);
if (cyc)
cyc = -1;
else
cyc = org.sz[org.anc(a)];
}
}
int CountCritical() {
if (cand.size() == 0) {
debug(cyc);
return cyc ? max(0, cyc) : N;
}
pary(cand.begin(),cand.end());
int ans = 0;
/*
for (int i = 0; i < N; i++) {
dsu[0].init(N);
for (int j = 0; j < tot; j++) if (i != E[j].first && i != E[j].second) dsu[0].join(E[j].first, E[j].second);
if (dsu[0].ok) ++ans;
}
*/
for (int i = 0; i < cand.size(); i++) if (dsu[i].ok) ++ans;
return ans;
}
#ifdef local
signed main() {
ios_base::sync_with_stdio(0), cin.tie(0);
int n;
cin >> n;
Init(n);
int q;
cin >> q;
while (q--) {
char c;
cin >> c;
if (c == 'L') {
int x, y;
cin >> x >> y;
Link(x, y);
} else if (c == 'Q') {
cout << CountCritical() << '\n';
}
}
}
#endif // local
/*
6
6
L 1 2
L 2 3
L 4 5
L 4 6
L 1 3
Q
*/
Compilation message
rings.cpp: In function 'void Link(int, int)':
rings.cpp:103:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
103 | for (int i = 0; i < cand.size(); i++)
| ~~^~~~~~~~~~~~~
rings.cpp: In function 'int CountCritical()':
rings.cpp:129:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
129 | for (int i = 0; i < cand.size(); i++) if (dsu[i].ok) ++ans;
| ~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
23896 KB |
Output is correct |
2 |
Correct |
18 ms |
24344 KB |
Output is correct |
3 |
Correct |
19 ms |
24396 KB |
Output is correct |
4 |
Correct |
17 ms |
23948 KB |
Output is correct |
5 |
Correct |
20 ms |
24236 KB |
Output is correct |
6 |
Correct |
18 ms |
24440 KB |
Output is correct |
7 |
Correct |
16 ms |
24140 KB |
Output is correct |
8 |
Correct |
19 ms |
24276 KB |
Output is correct |
9 |
Correct |
20 ms |
24396 KB |
Output is correct |
10 |
Correct |
21 ms |
24444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
487 ms |
75288 KB |
Output is correct |
2 |
Correct |
1928 ms |
102960 KB |
Output is correct |
3 |
Correct |
2267 ms |
130820 KB |
Output is correct |
4 |
Correct |
1310 ms |
136000 KB |
Output is correct |
5 |
Correct |
1379 ms |
136004 KB |
Output is correct |
6 |
Correct |
1248 ms |
134068 KB |
Output is correct |
7 |
Correct |
2198 ms |
129376 KB |
Output is correct |
8 |
Correct |
2120 ms |
128784 KB |
Output is correct |
9 |
Correct |
2214 ms |
136032 KB |
Output is correct |
10 |
Correct |
903 ms |
133004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
23896 KB |
Output is correct |
2 |
Correct |
18 ms |
24344 KB |
Output is correct |
3 |
Correct |
19 ms |
24396 KB |
Output is correct |
4 |
Correct |
17 ms |
23948 KB |
Output is correct |
5 |
Correct |
20 ms |
24236 KB |
Output is correct |
6 |
Correct |
18 ms |
24440 KB |
Output is correct |
7 |
Correct |
16 ms |
24140 KB |
Output is correct |
8 |
Correct |
19 ms |
24276 KB |
Output is correct |
9 |
Correct |
20 ms |
24396 KB |
Output is correct |
10 |
Correct |
21 ms |
24444 KB |
Output is correct |
11 |
Correct |
19 ms |
24448 KB |
Output is correct |
12 |
Correct |
21 ms |
24908 KB |
Output is correct |
13 |
Correct |
21 ms |
25036 KB |
Output is correct |
14 |
Correct |
22 ms |
24772 KB |
Output is correct |
15 |
Correct |
22 ms |
25404 KB |
Output is correct |
16 |
Correct |
21 ms |
25056 KB |
Output is correct |
17 |
Correct |
25 ms |
25036 KB |
Output is correct |
18 |
Correct |
24 ms |
25688 KB |
Output is correct |
19 |
Correct |
21 ms |
25040 KB |
Output is correct |
20 |
Correct |
22 ms |
24988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
23896 KB |
Output is correct |
2 |
Correct |
18 ms |
24344 KB |
Output is correct |
3 |
Correct |
19 ms |
24396 KB |
Output is correct |
4 |
Correct |
17 ms |
23948 KB |
Output is correct |
5 |
Correct |
20 ms |
24236 KB |
Output is correct |
6 |
Correct |
18 ms |
24440 KB |
Output is correct |
7 |
Correct |
16 ms |
24140 KB |
Output is correct |
8 |
Correct |
19 ms |
24276 KB |
Output is correct |
9 |
Correct |
20 ms |
24396 KB |
Output is correct |
10 |
Correct |
21 ms |
24444 KB |
Output is correct |
11 |
Correct |
19 ms |
24448 KB |
Output is correct |
12 |
Correct |
21 ms |
24908 KB |
Output is correct |
13 |
Correct |
21 ms |
25036 KB |
Output is correct |
14 |
Correct |
22 ms |
24772 KB |
Output is correct |
15 |
Correct |
22 ms |
25404 KB |
Output is correct |
16 |
Correct |
21 ms |
25056 KB |
Output is correct |
17 |
Correct |
25 ms |
25036 KB |
Output is correct |
18 |
Correct |
24 ms |
25688 KB |
Output is correct |
19 |
Correct |
21 ms |
25040 KB |
Output is correct |
20 |
Correct |
22 ms |
24988 KB |
Output is correct |
21 |
Correct |
35 ms |
28444 KB |
Output is correct |
22 |
Correct |
49 ms |
31216 KB |
Output is correct |
23 |
Correct |
70 ms |
33124 KB |
Output is correct |
24 |
Correct |
98 ms |
32100 KB |
Output is correct |
25 |
Correct |
37 ms |
30416 KB |
Output is correct |
26 |
Correct |
84 ms |
32984 KB |
Output is correct |
27 |
Correct |
69 ms |
33304 KB |
Output is correct |
28 |
Correct |
111 ms |
33652 KB |
Output is correct |
29 |
Correct |
75 ms |
32256 KB |
Output is correct |
30 |
Correct |
76 ms |
34756 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
23896 KB |
Output is correct |
2 |
Correct |
18 ms |
24344 KB |
Output is correct |
3 |
Correct |
19 ms |
24396 KB |
Output is correct |
4 |
Correct |
17 ms |
23948 KB |
Output is correct |
5 |
Correct |
20 ms |
24236 KB |
Output is correct |
6 |
Correct |
18 ms |
24440 KB |
Output is correct |
7 |
Correct |
16 ms |
24140 KB |
Output is correct |
8 |
Correct |
19 ms |
24276 KB |
Output is correct |
9 |
Correct |
20 ms |
24396 KB |
Output is correct |
10 |
Correct |
21 ms |
24444 KB |
Output is correct |
11 |
Correct |
487 ms |
75288 KB |
Output is correct |
12 |
Correct |
1928 ms |
102960 KB |
Output is correct |
13 |
Correct |
2267 ms |
130820 KB |
Output is correct |
14 |
Correct |
1310 ms |
136000 KB |
Output is correct |
15 |
Correct |
1379 ms |
136004 KB |
Output is correct |
16 |
Correct |
1248 ms |
134068 KB |
Output is correct |
17 |
Correct |
2198 ms |
129376 KB |
Output is correct |
18 |
Correct |
2120 ms |
128784 KB |
Output is correct |
19 |
Correct |
2214 ms |
136032 KB |
Output is correct |
20 |
Correct |
903 ms |
133004 KB |
Output is correct |
21 |
Correct |
19 ms |
24448 KB |
Output is correct |
22 |
Correct |
21 ms |
24908 KB |
Output is correct |
23 |
Correct |
21 ms |
25036 KB |
Output is correct |
24 |
Correct |
22 ms |
24772 KB |
Output is correct |
25 |
Correct |
22 ms |
25404 KB |
Output is correct |
26 |
Correct |
21 ms |
25056 KB |
Output is correct |
27 |
Correct |
25 ms |
25036 KB |
Output is correct |
28 |
Correct |
24 ms |
25688 KB |
Output is correct |
29 |
Correct |
21 ms |
25040 KB |
Output is correct |
30 |
Correct |
22 ms |
24988 KB |
Output is correct |
31 |
Correct |
35 ms |
28444 KB |
Output is correct |
32 |
Correct |
49 ms |
31216 KB |
Output is correct |
33 |
Correct |
70 ms |
33124 KB |
Output is correct |
34 |
Correct |
98 ms |
32100 KB |
Output is correct |
35 |
Correct |
37 ms |
30416 KB |
Output is correct |
36 |
Correct |
84 ms |
32984 KB |
Output is correct |
37 |
Correct |
69 ms |
33304 KB |
Output is correct |
38 |
Correct |
111 ms |
33652 KB |
Output is correct |
39 |
Correct |
75 ms |
32256 KB |
Output is correct |
40 |
Correct |
76 ms |
34756 KB |
Output is correct |
41 |
Correct |
255 ms |
67432 KB |
Output is correct |
42 |
Correct |
865 ms |
100040 KB |
Output is correct |
43 |
Correct |
386 ms |
86596 KB |
Output is correct |
44 |
Correct |
1486 ms |
126152 KB |
Output is correct |
45 |
Correct |
1467 ms |
117780 KB |
Output is correct |
46 |
Correct |
852 ms |
116972 KB |
Output is correct |
47 |
Correct |
1083 ms |
118648 KB |
Output is correct |
48 |
Correct |
941 ms |
109012 KB |
Output is correct |
49 |
Correct |
850 ms |
125084 KB |
Output is correct |
50 |
Correct |
935 ms |
123744 KB |
Output is correct |
51 |
Correct |
492 ms |
81076 KB |
Output is correct |
52 |
Correct |
1284 ms |
106684 KB |
Output is correct |
53 |
Correct |
973 ms |
109492 KB |
Output is correct |
54 |
Correct |
1855 ms |
115052 KB |
Output is correct |
55 |
Correct |
2271 ms |
123168 KB |
Output is correct |