#include <bits/stdc++.h>
using namespace std;
const int N = 1000005;
int n;
int szCircle;
int sz[N], par[N];
vector<int> G[N];
vector< pair<int, int> > E;
struct DSU {
int root;
int valid;
int par[N];
int cnt[3][N];
int deg[N];
int find(int u) { return u == par[u] ? u : par[u] = find(par[u]); }
bool join(int u, int v) {
u = find(u), v = find(v);
if (u == v) return 0;
cnt[0][u] += cnt[0][v];
cnt[1][u] += cnt[1][v];
par[v] = u;
return 1;
}
void add(int u, int v) {
if (u == root || v == root) return;
int ru = find(u), rv = find(v);
if (deg[u] == 2 || deg[v] == 2 || ru == rv) {
valid = 0; return;
}
cnt[deg[u]][ru]--;
cnt[deg[v]][rv]--;
deg[u]++, deg[v]++;
cnt[deg[u]][ru]++;
cnt[deg[v]][rv]++;
join(u, v);
}
DSU(int _root) {
valid = 1, root = _root;
for (int i = 0; i < n; ++i) {
cnt[0][i] = 1, par[i] = i, deg[i] = 0;
}
for (auto i : E) add(i.first, i.second);
}
};
vector<DSU> vec;
int find(int u) { return u == par[u] ? u : par[u] = find(par[u]); }
bool join(int u, int v) {
u = find(u), v = find(v);
if (u == v) return 0;
sz[u] += sz[v], par[v] = u; return 1;
}
void Init(int _n) {
n = _n;
for (int i = 0; i < n; ++i) sz[i] = 1, par[i] = i;
}
void Link(int A, int B) {
if (vec.size()) {
for (int i = 0; i < vec.size(); ++i) {
if (!vec[i].valid) continue;
vec[i].add(A, B);
}
return;
}
E.push_back({A, B});
G[A].push_back(B), G[B].push_back(A);
if (G[A].size() < G[B].size()) swap(A, B);
if (G[A].size() == 3) {
vec.push_back(DSU(A));
for (auto i : G[A]) {
vec.push_back(DSU(i));
}
return;
}
if (!join(A, B)) {
szCircle = sz[find(A)];
}
}
int CountCritical() {
int res = 0;
if (vec.size()) {
for (int i = 0; i < vec.size(); ++i) res += vec[i].valid;
}
else if (szCircle) res = szCircle;
else res = n;
return res;
}
Compilation message
rings.cpp: In function 'void Link(int, int)':
rings.cpp:71:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < vec.size(); ++i) {
~~^~~~~~~~~~~~
rings.cpp: In function 'int CountCritical()':
rings.cpp:95:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < vec.size(); ++i) res += vec[i].valid;
~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
23800 KB |
Output is correct |
2 |
Correct |
144 ms |
122144 KB |
Output is correct |
3 |
Correct |
148 ms |
122144 KB |
Output is correct |
4 |
Correct |
25 ms |
122144 KB |
Output is correct |
5 |
Correct |
24 ms |
122144 KB |
Output is correct |
6 |
Correct |
26 ms |
122144 KB |
Output is correct |
7 |
Correct |
139 ms |
122144 KB |
Output is correct |
8 |
Incorrect |
26 ms |
122144 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
477 ms |
122144 KB |
Output is correct |
2 |
Correct |
1297 ms |
159976 KB |
Output is correct |
3 |
Correct |
508 ms |
159976 KB |
Output is correct |
4 |
Correct |
1296 ms |
159976 KB |
Output is correct |
5 |
Correct |
1313 ms |
159976 KB |
Output is correct |
6 |
Correct |
1227 ms |
159976 KB |
Output is correct |
7 |
Correct |
511 ms |
159976 KB |
Output is correct |
8 |
Correct |
2736 ms |
179100 KB |
Output is correct |
9 |
Correct |
3118 ms |
186212 KB |
Output is correct |
10 |
Incorrect |
924 ms |
186212 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
23800 KB |
Output is correct |
2 |
Correct |
144 ms |
122144 KB |
Output is correct |
3 |
Correct |
148 ms |
122144 KB |
Output is correct |
4 |
Correct |
25 ms |
122144 KB |
Output is correct |
5 |
Correct |
24 ms |
122144 KB |
Output is correct |
6 |
Correct |
26 ms |
122144 KB |
Output is correct |
7 |
Correct |
139 ms |
122144 KB |
Output is correct |
8 |
Incorrect |
26 ms |
122144 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
23800 KB |
Output is correct |
2 |
Correct |
144 ms |
122144 KB |
Output is correct |
3 |
Correct |
148 ms |
122144 KB |
Output is correct |
4 |
Correct |
25 ms |
122144 KB |
Output is correct |
5 |
Correct |
24 ms |
122144 KB |
Output is correct |
6 |
Correct |
26 ms |
122144 KB |
Output is correct |
7 |
Correct |
139 ms |
122144 KB |
Output is correct |
8 |
Incorrect |
26 ms |
122144 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
23800 KB |
Output is correct |
2 |
Correct |
144 ms |
122144 KB |
Output is correct |
3 |
Correct |
148 ms |
122144 KB |
Output is correct |
4 |
Correct |
25 ms |
122144 KB |
Output is correct |
5 |
Correct |
24 ms |
122144 KB |
Output is correct |
6 |
Correct |
26 ms |
122144 KB |
Output is correct |
7 |
Correct |
139 ms |
122144 KB |
Output is correct |
8 |
Incorrect |
26 ms |
122144 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |