#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 deg[N];
int find(int u) { return u == par[u] ? u : par[u] = find(par[u]); }
void join(int u, int v) { par[find(u)] = find(v); }
void add(int u, int v) {
if (!valid || u == root || v == root) return;
int ru = find(u), rv = find(v);
if (deg[u] == 2 || deg[v] == 2 || ru == rv) {
valid = 0; return;
}
deg[u]++, deg[v]++;
join(u, v);
}
DSU(int _root) {
valid = 1, root = _root;
for (int i = 0; i < n; ++i) {
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) 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:59:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < vec.size(); ++i) vec[i].add(A, B);
~~^~~~~~~~~~~~
rings.cpp: In function 'int CountCritical()':
rings.cpp:80: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 |
21 ms |
23800 KB |
Output is correct |
2 |
Correct |
77 ms |
63192 KB |
Output is correct |
3 |
Correct |
68 ms |
63268 KB |
Output is correct |
4 |
Correct |
23 ms |
63268 KB |
Output is correct |
5 |
Correct |
23 ms |
63268 KB |
Output is correct |
6 |
Correct |
25 ms |
63268 KB |
Output is correct |
7 |
Correct |
66 ms |
63268 KB |
Output is correct |
8 |
Incorrect |
23 ms |
63268 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
506 ms |
63268 KB |
Output is correct |
2 |
Correct |
676 ms |
91772 KB |
Output is correct |
3 |
Correct |
387 ms |
91772 KB |
Output is correct |
4 |
Correct |
1328 ms |
91772 KB |
Output is correct |
5 |
Correct |
1340 ms |
91772 KB |
Output is correct |
6 |
Correct |
1308 ms |
91772 KB |
Output is correct |
7 |
Correct |
394 ms |
91772 KB |
Output is correct |
8 |
Correct |
1279 ms |
109244 KB |
Output is correct |
9 |
Correct |
1504 ms |
115832 KB |
Output is correct |
10 |
Incorrect |
915 ms |
115832 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
23800 KB |
Output is correct |
2 |
Correct |
77 ms |
63192 KB |
Output is correct |
3 |
Correct |
68 ms |
63268 KB |
Output is correct |
4 |
Correct |
23 ms |
63268 KB |
Output is correct |
5 |
Correct |
23 ms |
63268 KB |
Output is correct |
6 |
Correct |
25 ms |
63268 KB |
Output is correct |
7 |
Correct |
66 ms |
63268 KB |
Output is correct |
8 |
Incorrect |
23 ms |
63268 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
23800 KB |
Output is correct |
2 |
Correct |
77 ms |
63192 KB |
Output is correct |
3 |
Correct |
68 ms |
63268 KB |
Output is correct |
4 |
Correct |
23 ms |
63268 KB |
Output is correct |
5 |
Correct |
23 ms |
63268 KB |
Output is correct |
6 |
Correct |
25 ms |
63268 KB |
Output is correct |
7 |
Correct |
66 ms |
63268 KB |
Output is correct |
8 |
Incorrect |
23 ms |
63268 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
23800 KB |
Output is correct |
2 |
Correct |
77 ms |
63192 KB |
Output is correct |
3 |
Correct |
68 ms |
63268 KB |
Output is correct |
4 |
Correct |
23 ms |
63268 KB |
Output is correct |
5 |
Correct |
23 ms |
63268 KB |
Output is correct |
6 |
Correct |
25 ms |
63268 KB |
Output is correct |
7 |
Correct |
66 ms |
63268 KB |
Output is correct |
8 |
Incorrect |
23 ms |
63268 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |