#include<bits/stdc++.h>
using namespace std;
const int NMAX = 1000005;
vector<int> gr[NMAX];
struct chainsdsu{
int dad;
int l, r;
int sz;
bool iscycle;
};
int other(int x, int a1, int a2){ /// cel care nu e egal cu x, daca ambele sunt egale x
if(x == a1)
return a2;
return a1;
}
struct chainDS{ /// pentru variantele care accepta doar lanturi => cand answer devine mic (leg lant de lant de necapete, leg un ciclu in interior, leg lant de ciclu)
///variante cand nu merge deloc : mai mult de un ciclu
vector<chainsdsu> d;
int N;
bool ans;
chainDS(int sz) : d(sz + 1), N(sz){
for(int i = 1; i<= N; i++){
d[i] = {i, i, i, 1, false};
}
ans = true;
}
int findd(int x){
if(d[x].dad == x)
return x;
return d[x].dad = findd(d[x].dad);
}
bool AddEdge(int x, int y){
int dx = findd(x);
int dy = findd(y);
if(dx == dy){
ans = false;
return ans;
}
if(!((x == d[dx].l || x == d[dx].r) && (y == d[dy].l || y == d[dy].r))){
ans = false;
return ans;
}
if(d[dx].sz < d[dy].sz){
swap(dx, dy);
swap(x, y);
}
d[dy].dad = dx;
d[dx].l = other(x, d[dx].l, d[dx].r);
d[dx].r = other(y, d[dy].l, d[dy].r);
d[dx].sz += d[dy].sz;
return ans;
}
};
vector<pair<int, chainDS>> pos; /// nodurile ramase posibile
vector<pair<int, int>> edges;
chainsdsu d[NMAX];
int findd(int x){
if(x == d[x].dad)
return x;
return d[x].dad = findd(d[x].dad);
}
int N;
int nrcycle = 0;
int cans = 0;
void Init(int N_) {
N = N_;
for(int i = 1; i<= N; i++){
d[i] = {i, i, i, 1, false};
gr[i].clear();
}
pos.clear();
edges.clear();
nrcycle = 0;
cans = N;
}
void makepos(int A, int B){
unordered_set<int> vec;
for(auto x: gr[A])
vec.insert(x);
for(auto x: gr[B])
vec.insert(x);
for(auto x: vec){
pos.push_back({x, chainDS(N)});
for(auto e:edges){
if(e.first != x && e.second != x){
pos.back().second.AddEdge(e.first, e.second);
}
}
}
}
void Link(int A, int B) {
A++;
B++;
if(pos.size()){
for(auto &x:pos){
if(x.first != A && x.first != B)
x.second.AddEdge(A, B);
}
return;
}
gr[A].push_back(B);
gr[B].push_back(A);
edges.push_back({A, B});
chainsdsu &dA = d[findd(A)];
chainsdsu &dB = d[findd(B)];
if(dA.dad != dB.dad){
/// cazul in care leg doua lanturi (bine sau gresit) sau un ciclu cu un lant
if(dA.iscycle == false && dB.iscycle == false && (A == dA.l || A == dA.r) && (B == dB.l || B == dB.r)){
/// cazul ok
if(dA.sz > dB.sz){
dB.dad = dA.dad;
dA.sz += dB.sz;
dA.l = other(A, dA.l, dA.r);
dA.r = other(B, dB.l, dB.r);
}
else{
dA.dad = dB.dad;
dB.sz += dA.sz;
dB.r = other(B, dB.l, dB.r);
dB.l = other(A, dA.l, dA.r);
}
return;
}
/// stric ceva deci trebuie sa repar
makepos(A, B);
}
else{
/// cazul in care creez un ciclu
if(dA.iscycle == false && ((A == dA.l && B == dA.r) || (A == dA.r && B == dA.l))){
/// ciclu simplu
dA.iscycle = true;
nrcycle++;
cans = dA.sz;
return;
}
/// stric ceva deci trb sa fortez poz optime
makepos(A, B);
}
}
int CountCritical() {
if(pos.size()){
int ans = 0;
for(auto &x:pos){
if(x.second.ans == true)
ans++;
}
return ans;
}
if(nrcycle > 1)
return 0;
return cans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
23756 KB |
Output is correct |
2 |
Correct |
16 ms |
24300 KB |
Output is correct |
3 |
Correct |
17 ms |
24328 KB |
Output is correct |
4 |
Correct |
15 ms |
23792 KB |
Output is correct |
5 |
Correct |
16 ms |
24012 KB |
Output is correct |
6 |
Correct |
17 ms |
24140 KB |
Output is correct |
7 |
Correct |
17 ms |
24204 KB |
Output is correct |
8 |
Correct |
16 ms |
24044 KB |
Output is correct |
9 |
Correct |
16 ms |
24396 KB |
Output is correct |
10 |
Correct |
17 ms |
24396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
430 ms |
54240 KB |
Output is correct |
2 |
Correct |
1049 ms |
118104 KB |
Output is correct |
3 |
Correct |
943 ms |
135040 KB |
Output is correct |
4 |
Correct |
1130 ms |
95756 KB |
Output is correct |
5 |
Correct |
1099 ms |
95992 KB |
Output is correct |
6 |
Correct |
1076 ms |
94164 KB |
Output is correct |
7 |
Correct |
933 ms |
134616 KB |
Output is correct |
8 |
Correct |
1677 ms |
177564 KB |
Output is correct |
9 |
Correct |
1871 ms |
191644 KB |
Output is correct |
10 |
Correct |
712 ms |
92960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
23756 KB |
Output is correct |
2 |
Correct |
16 ms |
24300 KB |
Output is correct |
3 |
Correct |
17 ms |
24328 KB |
Output is correct |
4 |
Correct |
15 ms |
23792 KB |
Output is correct |
5 |
Correct |
16 ms |
24012 KB |
Output is correct |
6 |
Correct |
17 ms |
24140 KB |
Output is correct |
7 |
Correct |
17 ms |
24204 KB |
Output is correct |
8 |
Correct |
16 ms |
24044 KB |
Output is correct |
9 |
Correct |
16 ms |
24396 KB |
Output is correct |
10 |
Correct |
17 ms |
24396 KB |
Output is correct |
11 |
Correct |
17 ms |
24524 KB |
Output is correct |
12 |
Correct |
19 ms |
25324 KB |
Output is correct |
13 |
Correct |
19 ms |
25292 KB |
Output is correct |
14 |
Correct |
18 ms |
24780 KB |
Output is correct |
15 |
Correct |
18 ms |
25876 KB |
Output is correct |
16 |
Correct |
18 ms |
24524 KB |
Output is correct |
17 |
Correct |
18 ms |
24868 KB |
Output is correct |
18 |
Correct |
20 ms |
25932 KB |
Output is correct |
19 |
Correct |
19 ms |
24584 KB |
Output is correct |
20 |
Correct |
19 ms |
25156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
23756 KB |
Output is correct |
2 |
Correct |
16 ms |
24300 KB |
Output is correct |
3 |
Correct |
17 ms |
24328 KB |
Output is correct |
4 |
Correct |
15 ms |
23792 KB |
Output is correct |
5 |
Correct |
16 ms |
24012 KB |
Output is correct |
6 |
Correct |
17 ms |
24140 KB |
Output is correct |
7 |
Correct |
17 ms |
24204 KB |
Output is correct |
8 |
Correct |
16 ms |
24044 KB |
Output is correct |
9 |
Correct |
16 ms |
24396 KB |
Output is correct |
10 |
Correct |
17 ms |
24396 KB |
Output is correct |
11 |
Correct |
17 ms |
24524 KB |
Output is correct |
12 |
Correct |
19 ms |
25324 KB |
Output is correct |
13 |
Correct |
19 ms |
25292 KB |
Output is correct |
14 |
Correct |
18 ms |
24780 KB |
Output is correct |
15 |
Correct |
18 ms |
25876 KB |
Output is correct |
16 |
Correct |
18 ms |
24524 KB |
Output is correct |
17 |
Correct |
18 ms |
24868 KB |
Output is correct |
18 |
Correct |
20 ms |
25932 KB |
Output is correct |
19 |
Correct |
19 ms |
24584 KB |
Output is correct |
20 |
Correct |
19 ms |
25156 KB |
Output is correct |
21 |
Correct |
32 ms |
26408 KB |
Output is correct |
22 |
Correct |
47 ms |
27836 KB |
Output is correct |
23 |
Correct |
56 ms |
28968 KB |
Output is correct |
24 |
Correct |
61 ms |
33712 KB |
Output is correct |
25 |
Correct |
33 ms |
33828 KB |
Output is correct |
26 |
Correct |
71 ms |
34116 KB |
Output is correct |
27 |
Correct |
61 ms |
29852 KB |
Output is correct |
28 |
Correct |
78 ms |
33604 KB |
Output is correct |
29 |
Correct |
53 ms |
34448 KB |
Output is correct |
30 |
Correct |
96 ms |
30800 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
23756 KB |
Output is correct |
2 |
Correct |
16 ms |
24300 KB |
Output is correct |
3 |
Correct |
17 ms |
24328 KB |
Output is correct |
4 |
Correct |
15 ms |
23792 KB |
Output is correct |
5 |
Correct |
16 ms |
24012 KB |
Output is correct |
6 |
Correct |
17 ms |
24140 KB |
Output is correct |
7 |
Correct |
17 ms |
24204 KB |
Output is correct |
8 |
Correct |
16 ms |
24044 KB |
Output is correct |
9 |
Correct |
16 ms |
24396 KB |
Output is correct |
10 |
Correct |
17 ms |
24396 KB |
Output is correct |
11 |
Correct |
430 ms |
54240 KB |
Output is correct |
12 |
Correct |
1049 ms |
118104 KB |
Output is correct |
13 |
Correct |
943 ms |
135040 KB |
Output is correct |
14 |
Correct |
1130 ms |
95756 KB |
Output is correct |
15 |
Correct |
1099 ms |
95992 KB |
Output is correct |
16 |
Correct |
1076 ms |
94164 KB |
Output is correct |
17 |
Correct |
933 ms |
134616 KB |
Output is correct |
18 |
Correct |
1677 ms |
177564 KB |
Output is correct |
19 |
Correct |
1871 ms |
191644 KB |
Output is correct |
20 |
Correct |
712 ms |
92960 KB |
Output is correct |
21 |
Correct |
17 ms |
24524 KB |
Output is correct |
22 |
Correct |
19 ms |
25324 KB |
Output is correct |
23 |
Correct |
19 ms |
25292 KB |
Output is correct |
24 |
Correct |
18 ms |
24780 KB |
Output is correct |
25 |
Correct |
18 ms |
25876 KB |
Output is correct |
26 |
Correct |
18 ms |
24524 KB |
Output is correct |
27 |
Correct |
18 ms |
24868 KB |
Output is correct |
28 |
Correct |
20 ms |
25932 KB |
Output is correct |
29 |
Correct |
19 ms |
24584 KB |
Output is correct |
30 |
Correct |
19 ms |
25156 KB |
Output is correct |
31 |
Correct |
32 ms |
26408 KB |
Output is correct |
32 |
Correct |
47 ms |
27836 KB |
Output is correct |
33 |
Correct |
56 ms |
28968 KB |
Output is correct |
34 |
Correct |
61 ms |
33712 KB |
Output is correct |
35 |
Correct |
33 ms |
33828 KB |
Output is correct |
36 |
Correct |
71 ms |
34116 KB |
Output is correct |
37 |
Correct |
61 ms |
29852 KB |
Output is correct |
38 |
Correct |
78 ms |
33604 KB |
Output is correct |
39 |
Correct |
53 ms |
34448 KB |
Output is correct |
40 |
Correct |
96 ms |
30800 KB |
Output is correct |
41 |
Correct |
234 ms |
47208 KB |
Output is correct |
42 |
Correct |
708 ms |
144168 KB |
Output is correct |
43 |
Correct |
320 ms |
119492 KB |
Output is correct |
44 |
Correct |
788 ms |
126788 KB |
Output is correct |
45 |
Correct |
700 ms |
132136 KB |
Output is correct |
46 |
Correct |
689 ms |
84328 KB |
Output is correct |
47 |
Correct |
935 ms |
85376 KB |
Output is correct |
48 |
Correct |
526 ms |
130192 KB |
Output is correct |
49 |
Correct |
689 ms |
84896 KB |
Output is correct |
50 |
Correct |
743 ms |
84124 KB |
Output is correct |
51 |
Correct |
341 ms |
106148 KB |
Output is correct |
52 |
Correct |
689 ms |
106752 KB |
Output is correct |
53 |
Correct |
512 ms |
129096 KB |
Output is correct |
54 |
Correct |
1314 ms |
151672 KB |
Output is correct |
55 |
Correct |
1018 ms |
122492 KB |
Output is correct |