#include <iostream>
#include <algorithm>
#include <vector>
#include <cassert>
#define rep(i, n) for (int i=0; i<(n); i++)
#define pb push_back
#define INF (1LL<<60)
#define all(x) (x).begin(), (x).end()
#define _1 first
#define _2 second
using namespace std;
typedef pair<int, int> P;
struct UF {
vector<int> U, R;
UF(int N) {
rep(i, N) U.pb(i), R.pb(1);
}
int find(int x) {
if (U[x]==x)return x;
return U[x] = find(U[x]);
}
void unite(int x, int y) {
x=find(x), y=find(y);
if(x==y)return;
if (R[x]<R[y])swap(x,y);
U[y]=x;
R[x]+=R[y];
}
bool same(int x, int y) {
return find(x)==find(y);
}
};
int N;
struct Tree {
UF uf;
int X;
bool dead;
Tree() :uf(0){}
Tree(int X, vector<P> edges) : uf(N), X(X), dead(false) {
for (P p : edges) add_edge(p._1, p._2);
}
void add_edge(int a, int b) {
if (dead) return;
if (a==X||b==X) return;
if (uf.same(a, b)) dead = true;
uf.unite(a, b);
}
};
vector<int> G[1000000];
UF uf(0);
bool deg012 = true;
vector<int> cycles;
Tree cand[4];
vector<P> edges;
void cand_filter(vector<int> seq) {
rep(i, 4) if (!cand[i].dead) {
bool ok = false;
for (int x : seq) if (x == cand[i].X) ok = true;
if (!ok) cand[i].dead = true;
}
}
void Init(int N_) {
N = N_;
uf = UF(N);
}
void Link(int a, int b) {
edges.pb(P(a, b));
G[a].pb(b);
G[b].pb(a);
if (!deg012) rep(i, 4) cand[i].add_edge(a, b);
if (G[a].size() < 3) swap(a, b);
if (deg012 && G[a].size() >= 3) {
deg012 = false;
cand[0] = Tree(a, edges);
cand[1] = Tree(G[a][0], edges);
cand[2] = Tree(G[a][1], edges);
cand[3] = Tree(G[a][2], edges);
}
if (G[a].size() == 3) cand_filter({a, G[a][0], G[a][1], G[a][2]});
if (G[b].size() == 3) cand_filter({b, G[b][0], G[b][1], G[b][2]});
if (G[a].size() >= 4) cand_filter({a});
if (G[b].size() >= 4) cand_filter({b});
if (deg012) {
bool same = uf.same(a, b);
uf.unite(a, b);
if (same) cycles.pb(uf.R[uf.find(a)]);
}
}
int CountCritical() {
if (deg012) {
if (cycles.size() == 0) return N;
else if (cycles.size() == 1) return cycles[0];
else return 0;
}
int s = 0;
rep(i, 4) s += !cand[i].dead;
return s;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
23804 KB |
Output is correct |
2 |
Correct |
24 ms |
24368 KB |
Output is correct |
3 |
Correct |
24 ms |
24368 KB |
Output is correct |
4 |
Correct |
22 ms |
24368 KB |
Output is correct |
5 |
Correct |
23 ms |
24368 KB |
Output is correct |
6 |
Correct |
24 ms |
24368 KB |
Output is correct |
7 |
Correct |
22 ms |
24368 KB |
Output is correct |
8 |
Correct |
23 ms |
24368 KB |
Output is correct |
9 |
Correct |
24 ms |
24488 KB |
Output is correct |
10 |
Correct |
25 ms |
24488 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
490 ms |
48412 KB |
Output is correct |
2 |
Correct |
1120 ms |
86684 KB |
Output is correct |
3 |
Correct |
1031 ms |
97920 KB |
Output is correct |
4 |
Correct |
1346 ms |
97920 KB |
Output is correct |
5 |
Correct |
1349 ms |
97920 KB |
Output is correct |
6 |
Correct |
1342 ms |
97920 KB |
Output is correct |
7 |
Correct |
955 ms |
97936 KB |
Output is correct |
8 |
Correct |
1692 ms |
105336 KB |
Output is correct |
9 |
Correct |
1855 ms |
113360 KB |
Output is correct |
10 |
Correct |
952 ms |
113360 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
23804 KB |
Output is correct |
2 |
Correct |
24 ms |
24368 KB |
Output is correct |
3 |
Correct |
24 ms |
24368 KB |
Output is correct |
4 |
Correct |
22 ms |
24368 KB |
Output is correct |
5 |
Correct |
23 ms |
24368 KB |
Output is correct |
6 |
Correct |
24 ms |
24368 KB |
Output is correct |
7 |
Correct |
22 ms |
24368 KB |
Output is correct |
8 |
Correct |
23 ms |
24368 KB |
Output is correct |
9 |
Correct |
24 ms |
24488 KB |
Output is correct |
10 |
Correct |
25 ms |
24488 KB |
Output is correct |
11 |
Correct |
24 ms |
113360 KB |
Output is correct |
12 |
Correct |
29 ms |
113360 KB |
Output is correct |
13 |
Correct |
29 ms |
113360 KB |
Output is correct |
14 |
Correct |
34 ms |
113360 KB |
Output is correct |
15 |
Correct |
28 ms |
113360 KB |
Output is correct |
16 |
Correct |
27 ms |
113360 KB |
Output is correct |
17 |
Correct |
27 ms |
113360 KB |
Output is correct |
18 |
Correct |
30 ms |
113360 KB |
Output is correct |
19 |
Correct |
28 ms |
113360 KB |
Output is correct |
20 |
Correct |
29 ms |
113360 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
23804 KB |
Output is correct |
2 |
Correct |
24 ms |
24368 KB |
Output is correct |
3 |
Correct |
24 ms |
24368 KB |
Output is correct |
4 |
Correct |
22 ms |
24368 KB |
Output is correct |
5 |
Correct |
23 ms |
24368 KB |
Output is correct |
6 |
Correct |
24 ms |
24368 KB |
Output is correct |
7 |
Correct |
22 ms |
24368 KB |
Output is correct |
8 |
Correct |
23 ms |
24368 KB |
Output is correct |
9 |
Correct |
24 ms |
24488 KB |
Output is correct |
10 |
Correct |
25 ms |
24488 KB |
Output is correct |
11 |
Correct |
24 ms |
113360 KB |
Output is correct |
12 |
Correct |
29 ms |
113360 KB |
Output is correct |
13 |
Correct |
29 ms |
113360 KB |
Output is correct |
14 |
Correct |
34 ms |
113360 KB |
Output is correct |
15 |
Correct |
28 ms |
113360 KB |
Output is correct |
16 |
Correct |
27 ms |
113360 KB |
Output is correct |
17 |
Correct |
27 ms |
113360 KB |
Output is correct |
18 |
Correct |
30 ms |
113360 KB |
Output is correct |
19 |
Correct |
28 ms |
113360 KB |
Output is correct |
20 |
Correct |
29 ms |
113360 KB |
Output is correct |
21 |
Correct |
44 ms |
113360 KB |
Output is correct |
22 |
Correct |
64 ms |
113360 KB |
Output is correct |
23 |
Correct |
71 ms |
113360 KB |
Output is correct |
24 |
Correct |
76 ms |
113360 KB |
Output is correct |
25 |
Correct |
87 ms |
113360 KB |
Output is correct |
26 |
Correct |
75 ms |
113360 KB |
Output is correct |
27 |
Correct |
77 ms |
113360 KB |
Output is correct |
28 |
Correct |
84 ms |
113360 KB |
Output is correct |
29 |
Correct |
68 ms |
113360 KB |
Output is correct |
30 |
Correct |
91 ms |
113360 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
23804 KB |
Output is correct |
2 |
Correct |
24 ms |
24368 KB |
Output is correct |
3 |
Correct |
24 ms |
24368 KB |
Output is correct |
4 |
Correct |
22 ms |
24368 KB |
Output is correct |
5 |
Correct |
23 ms |
24368 KB |
Output is correct |
6 |
Correct |
24 ms |
24368 KB |
Output is correct |
7 |
Correct |
22 ms |
24368 KB |
Output is correct |
8 |
Correct |
23 ms |
24368 KB |
Output is correct |
9 |
Correct |
24 ms |
24488 KB |
Output is correct |
10 |
Correct |
25 ms |
24488 KB |
Output is correct |
11 |
Correct |
490 ms |
48412 KB |
Output is correct |
12 |
Correct |
1120 ms |
86684 KB |
Output is correct |
13 |
Correct |
1031 ms |
97920 KB |
Output is correct |
14 |
Correct |
1346 ms |
97920 KB |
Output is correct |
15 |
Correct |
1349 ms |
97920 KB |
Output is correct |
16 |
Correct |
1342 ms |
97920 KB |
Output is correct |
17 |
Correct |
955 ms |
97936 KB |
Output is correct |
18 |
Correct |
1692 ms |
105336 KB |
Output is correct |
19 |
Correct |
1855 ms |
113360 KB |
Output is correct |
20 |
Correct |
952 ms |
113360 KB |
Output is correct |
21 |
Correct |
24 ms |
113360 KB |
Output is correct |
22 |
Correct |
29 ms |
113360 KB |
Output is correct |
23 |
Correct |
29 ms |
113360 KB |
Output is correct |
24 |
Correct |
34 ms |
113360 KB |
Output is correct |
25 |
Correct |
28 ms |
113360 KB |
Output is correct |
26 |
Correct |
27 ms |
113360 KB |
Output is correct |
27 |
Correct |
27 ms |
113360 KB |
Output is correct |
28 |
Correct |
30 ms |
113360 KB |
Output is correct |
29 |
Correct |
28 ms |
113360 KB |
Output is correct |
30 |
Correct |
29 ms |
113360 KB |
Output is correct |
31 |
Correct |
44 ms |
113360 KB |
Output is correct |
32 |
Correct |
64 ms |
113360 KB |
Output is correct |
33 |
Correct |
71 ms |
113360 KB |
Output is correct |
34 |
Correct |
76 ms |
113360 KB |
Output is correct |
35 |
Correct |
87 ms |
113360 KB |
Output is correct |
36 |
Correct |
75 ms |
113360 KB |
Output is correct |
37 |
Correct |
77 ms |
113360 KB |
Output is correct |
38 |
Correct |
84 ms |
113360 KB |
Output is correct |
39 |
Correct |
68 ms |
113360 KB |
Output is correct |
40 |
Correct |
91 ms |
113360 KB |
Output is correct |
41 |
Correct |
262 ms |
113360 KB |
Output is correct |
42 |
Correct |
733 ms |
113360 KB |
Output is correct |
43 |
Correct |
335 ms |
113360 KB |
Output is correct |
44 |
Correct |
752 ms |
113360 KB |
Output is correct |
45 |
Correct |
922 ms |
113360 KB |
Output is correct |
46 |
Correct |
863 ms |
113360 KB |
Output is correct |
47 |
Correct |
1127 ms |
113360 KB |
Output is correct |
48 |
Correct |
559 ms |
113360 KB |
Output is correct |
49 |
Correct |
869 ms |
113360 KB |
Output is correct |
50 |
Correct |
917 ms |
113360 KB |
Output is correct |
51 |
Correct |
376 ms |
113360 KB |
Output is correct |
52 |
Correct |
638 ms |
113360 KB |
Output is correct |
53 |
Correct |
520 ms |
113360 KB |
Output is correct |
54 |
Correct |
1534 ms |
113360 KB |
Output is correct |
55 |
Correct |
1238 ms |
113360 KB |
Output is correct |