#include <bits/stdc++.h>
using namespace std;
#define int long long
#define sz(a) (int)a.size()
#define all(a) a.begin(), a.end()
int n;
bool hasloop=false;
vector<vector<int>> neigh;
vector<int> visited;
vector<int> parent;
vector<int> pr;
vector<int> sz;
vector<int> crit;
vector<int> incycle;
vector<int> iscycle;
vector<int> orig;
int timer=0;
int exceptation=-1;
bool cycl=0;
int cc=0;
int c=0;
int upds=0;
int getParent(int a) {
if(parent[a]==a) return a;
else return parent[a]=getParent(parent[a]);
}
void unite(int a, int b){
a=getParent(a); b=getParent(b);
if(a==b) return;
if(sz[b]>sz[a]) swap(a,b);
sz[a]+=sz[b];
parent[a]=b;
}
void detect_cycle(int x, int p) {
if(visited[x]==2) return;
if (visited[x] == 1) {
vector<int> v;
iscycle[x]=true;
incycle.push_back(x);
int cr=pr[x];
while (cr!=x) {
incycle.push_back(cr);
iscycle[cr]=true;
cr=pr[cr];
}
return;
}
pr[x]=p;
visited[x]=1;
for (int to : neigh[x]) {
if (to==p) continue;
detect_cycle(to,x);
}
visited[x]=2;
}
void get_origin(int x, int p,int og) {
if(orig[x]>=0) return;
orig[x]=og;
for (int to : neigh[x]) {
if (to==p||iscycle[to]) continue;
get_origin(to,x,orig[x]);
}
}
void Init(signed N_) {
n = N_;
parent.resize(n);
visited.resize(n,0);
neigh.resize(n);
orig.resize(n,-1);
cc=0;
c=n;
sz.resize(n,1);
iscycle.resize(n,0);
pr.resize(n,-1);
crit.resize(n,0);
upds=0;
for (int i = 0; i < n; i++) parent[i]=i;
}
void update_crit(int X){
if(sz(neigh[X])==3){
crit[neigh[X][0]]++;
crit[neigh[X][1]]++;
crit[neigh[X][2]]++;
crit[X]++;
upds++;
c=0;
if(crit[neigh[X][0]]==upds) c++;
if(crit[neigh[X][1]]==upds) c++;
if(crit[neigh[X][2]]==upds) c++;
if(crit[X]==upds) c++;
}else if(sz(neigh[X])>3){
crit[X]++;
upds++;
c=0;
if(crit[X]==upds) c++;
}
}
void case1(int A, int B){ //two vertex of different sets O(1);
unite(A,B);
if(orig[A]>=0&&orig[B]<0) get_origin(B,A,orig[A]);
else if(orig[B]>=0&&orig[A]<0) get_origin(A,B,orig[B]);
update_crit(A);
update_crit(B);
}
void case2(int A, int B){ //two vertex of same sets !! NO CYCLE !! O(N+M)
detect_cycle(A,B);
cycl=1;
upds++;
c=0;
for (int i = 0; i < sz(incycle); i++){
crit[incycle[i]]++;
if(crit[incycle[i]]==upds) c++;
}
visited.clear();
visited.resize(n,0);
for (int i = 0; i < sz(incycle); i++) get_origin(incycle[i],-1,incycle[i]);
case1(A,B);
}
//UPD
void case3(int A, int B){ //two vertex of same sets !! CYCLE !! O(1)
//cerr << "case 3";
if(orig[A]<0||orig[B]<0) { c=0; upds++; return; }
upds++;
c=0;
crit[orig[A]]++;
if(crit[orig[A]]==upds) c++;
if(orig[A]!=orig[B]) {
crit[orig[B]]++;
if(crit[orig[B]]==upds) c++;
}
update_crit(A);
update_crit(B);
update_crit(orig[A]);
update_crit(orig[B]);
}
void Link(signed A, signed B) {
neigh[A].push_back(B);
neigh[B].push_back(A);
if(getParent(A)==getParent(B)){
if(cycl) case3(A,B);
else case2(A,B);
}else{
case1(A,B);
}
}
signed CountCritical() {
return c;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
3 ms |
856 KB |
Output is correct |
3 |
Correct |
3 ms |
992 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
860 KB |
Output is correct |
6 |
Correct |
2 ms |
1112 KB |
Output is correct |
7 |
Correct |
1 ms |
860 KB |
Output is correct |
8 |
Correct |
1 ms |
860 KB |
Output is correct |
9 |
Correct |
3 ms |
860 KB |
Output is correct |
10 |
Correct |
3 ms |
860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
327 ms |
64284 KB |
Output is correct |
2 |
Correct |
668 ms |
98668 KB |
Output is correct |
3 |
Correct |
977 ms |
124480 KB |
Output is correct |
4 |
Correct |
924 ms |
123416 KB |
Output is correct |
5 |
Correct |
870 ms |
125008 KB |
Output is correct |
6 |
Correct |
1127 ms |
155912 KB |
Output is correct |
7 |
Correct |
996 ms |
122780 KB |
Output is correct |
8 |
Correct |
813 ms |
115412 KB |
Output is correct |
9 |
Correct |
814 ms |
123164 KB |
Output is correct |
10 |
Correct |
574 ms |
121168 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
3 ms |
856 KB |
Output is correct |
3 |
Correct |
3 ms |
992 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
860 KB |
Output is correct |
6 |
Correct |
2 ms |
1112 KB |
Output is correct |
7 |
Correct |
1 ms |
860 KB |
Output is correct |
8 |
Correct |
1 ms |
860 KB |
Output is correct |
9 |
Correct |
3 ms |
860 KB |
Output is correct |
10 |
Correct |
3 ms |
860 KB |
Output is correct |
11 |
Correct |
2 ms |
860 KB |
Output is correct |
12 |
Correct |
4 ms |
1884 KB |
Output is correct |
13 |
Correct |
3 ms |
1884 KB |
Output is correct |
14 |
Correct |
3 ms |
1372 KB |
Output is correct |
15 |
Correct |
4 ms |
2260 KB |
Output is correct |
16 |
Correct |
4 ms |
1628 KB |
Output is correct |
17 |
Correct |
3 ms |
1628 KB |
Output is correct |
18 |
Correct |
6 ms |
2396 KB |
Output is correct |
19 |
Correct |
3 ms |
1628 KB |
Output is correct |
20 |
Correct |
3 ms |
1624 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
3 ms |
856 KB |
Output is correct |
3 |
Correct |
3 ms |
992 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
860 KB |
Output is correct |
6 |
Correct |
2 ms |
1112 KB |
Output is correct |
7 |
Correct |
1 ms |
860 KB |
Output is correct |
8 |
Correct |
1 ms |
860 KB |
Output is correct |
9 |
Correct |
3 ms |
860 KB |
Output is correct |
10 |
Correct |
3 ms |
860 KB |
Output is correct |
11 |
Correct |
2 ms |
860 KB |
Output is correct |
12 |
Correct |
4 ms |
1884 KB |
Output is correct |
13 |
Correct |
3 ms |
1884 KB |
Output is correct |
14 |
Correct |
3 ms |
1372 KB |
Output is correct |
15 |
Correct |
4 ms |
2260 KB |
Output is correct |
16 |
Correct |
4 ms |
1628 KB |
Output is correct |
17 |
Correct |
3 ms |
1628 KB |
Output is correct |
18 |
Correct |
6 ms |
2396 KB |
Output is correct |
19 |
Correct |
3 ms |
1628 KB |
Output is correct |
20 |
Correct |
3 ms |
1624 KB |
Output is correct |
21 |
Correct |
11 ms |
5720 KB |
Output is correct |
22 |
Correct |
20 ms |
8840 KB |
Output is correct |
23 |
Correct |
40 ms |
11128 KB |
Output is correct |
24 |
Correct |
31 ms |
10324 KB |
Output is correct |
25 |
Correct |
13 ms |
8884 KB |
Output is correct |
26 |
Correct |
38 ms |
10720 KB |
Output is correct |
27 |
Correct |
41 ms |
10576 KB |
Output is correct |
28 |
Correct |
30 ms |
11096 KB |
Output is correct |
29 |
Correct |
22 ms |
10460 KB |
Output is correct |
30 |
Correct |
34 ms |
13348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
3 ms |
856 KB |
Output is correct |
3 |
Correct |
3 ms |
992 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
860 KB |
Output is correct |
6 |
Correct |
2 ms |
1112 KB |
Output is correct |
7 |
Correct |
1 ms |
860 KB |
Output is correct |
8 |
Correct |
1 ms |
860 KB |
Output is correct |
9 |
Correct |
3 ms |
860 KB |
Output is correct |
10 |
Correct |
3 ms |
860 KB |
Output is correct |
11 |
Correct |
327 ms |
64284 KB |
Output is correct |
12 |
Correct |
668 ms |
98668 KB |
Output is correct |
13 |
Correct |
977 ms |
124480 KB |
Output is correct |
14 |
Correct |
924 ms |
123416 KB |
Output is correct |
15 |
Correct |
870 ms |
125008 KB |
Output is correct |
16 |
Correct |
1127 ms |
155912 KB |
Output is correct |
17 |
Correct |
996 ms |
122780 KB |
Output is correct |
18 |
Correct |
813 ms |
115412 KB |
Output is correct |
19 |
Correct |
814 ms |
123164 KB |
Output is correct |
20 |
Correct |
574 ms |
121168 KB |
Output is correct |
21 |
Correct |
2 ms |
860 KB |
Output is correct |
22 |
Correct |
4 ms |
1884 KB |
Output is correct |
23 |
Correct |
3 ms |
1884 KB |
Output is correct |
24 |
Correct |
3 ms |
1372 KB |
Output is correct |
25 |
Correct |
4 ms |
2260 KB |
Output is correct |
26 |
Correct |
4 ms |
1628 KB |
Output is correct |
27 |
Correct |
3 ms |
1628 KB |
Output is correct |
28 |
Correct |
6 ms |
2396 KB |
Output is correct |
29 |
Correct |
3 ms |
1628 KB |
Output is correct |
30 |
Correct |
3 ms |
1624 KB |
Output is correct |
31 |
Correct |
11 ms |
5720 KB |
Output is correct |
32 |
Correct |
20 ms |
8840 KB |
Output is correct |
33 |
Correct |
40 ms |
11128 KB |
Output is correct |
34 |
Correct |
31 ms |
10324 KB |
Output is correct |
35 |
Correct |
13 ms |
8884 KB |
Output is correct |
36 |
Correct |
38 ms |
10720 KB |
Output is correct |
37 |
Correct |
41 ms |
10576 KB |
Output is correct |
38 |
Correct |
30 ms |
11096 KB |
Output is correct |
39 |
Correct |
22 ms |
10460 KB |
Output is correct |
40 |
Correct |
34 ms |
13348 KB |
Output is correct |
41 |
Correct |
148 ms |
51540 KB |
Output is correct |
42 |
Correct |
321 ms |
88660 KB |
Output is correct |
43 |
Correct |
173 ms |
79956 KB |
Output is correct |
44 |
Correct |
548 ms |
112676 KB |
Output is correct |
45 |
Correct |
616 ms |
115260 KB |
Output is correct |
46 |
Correct |
491 ms |
102208 KB |
Output is correct |
47 |
Correct |
762 ms |
126396 KB |
Output is correct |
48 |
Correct |
337 ms |
101976 KB |
Output is correct |
49 |
Correct |
519 ms |
114660 KB |
Output is correct |
50 |
Correct |
565 ms |
112968 KB |
Output is correct |
51 |
Correct |
209 ms |
71252 KB |
Output is correct |
52 |
Correct |
412 ms |
91216 KB |
Output is correct |
53 |
Correct |
372 ms |
102196 KB |
Output is correct |
54 |
Correct |
672 ms |
100148 KB |
Output is correct |
55 |
Correct |
699 ms |
109024 KB |
Output is correct |