#include <bits/stdc++.h>
using namespace std;
typedef int64_t llo;
#define mp make_pair
#define pb push_back
#define a first
#define b second
//#define inbuf_len 1 << 16
//#define outbuf_len 1 << 16
//#define endl '\n'
int n;
int co=0;
int st=0;
int par[1000001][5];
set<int> adj[1000001];
vector<int> dd;
int vis[4];
int find(int no,int ind){
if(par[no][ind]==no){
return no;
}
par[no][ind]=find(par[no][ind],ind);
return par[no][ind];
}
int ans;
void Init(int N_) {
n = N_;
for(int j=0;j<5;j++){
for(int i=0;i<n;i++){
par[i][j]=i;
}
}
ans=n;
}
vector<pair<int,int>> ed;
void push(int aa,int bb,int ind){
if(dd[ind-1]==aa or dd[ind-1]==bb){
return ;
}
int x=find(aa,ind);
int y=find(bb,ind);
if(x==y){
vis[ind-1]=1;
// cout<<dd[ind-1]<<":<<"<<endl;
// cout<<aa<"::"<<bb<<ind<<endl;
}
else{
par[x][ind]=y;
}
}
vector<int> path;
vector<int> cot;
int vis2[1000001];
void dfs(int no,int aa,int bb,int par2=-1){
vis2[no]=1;
// cout<<no<<",";
cot.pb(no);
for(auto j:adj[no]){
if(j==par2){
continue;
}
if(no==aa and j==bb){
continue;
}
if(vis2[j]){
path=cot;
}
else{
dfs(j,aa,bb,no);
}
}
cot.pop_back();
}
void find3(int aa,int bb){
dfs(aa,aa,bb);
}
void Link(int aa, int bb){
ed.pb({aa,bb});
if(st){
for(int i=0;i<dd.size();i++){
push(aa,bb,i+1);
}
}
int prev=st;
/*if(st){
vector<int> dd;
int k=0;
for(auto j:cur){
k+=1;
if(j==aa or j==bb){
continue;
}
int x=find(aa,k);
int y=find(bb,k);
if(x==y){
dd.pb(j);
}
else{
par[x][j]=y;
}
}
for(auto j:dd){
cur.erase(j);
}
}*/
adj[aa].insert(bb);
// cout<<adj[aa].size()<<"...."<<endl;
if(adj[aa].size()==3){
if(st==0){
st=1;
dd.pb(aa);
for(auto j:adj[aa]){
dd.pb(j);
// cout<<j<<"<";
}
// cout<<endl;
}
else{
for(int i=0;i<dd.size();i++){
int stt=0;
if(dd[i]==aa){
stt=1;
}
for(auto j:adj[aa]){
if(dd[i]==j){
stt=1;
}
}
if(stt==0){
vis[i]=1;
// cout<<i<<"<........<"<<endl;
}
}
}
}
else if(adj[aa].size()==4){
if(st==0){
vis[1]=1;
vis[2]=1;
vis[3]=1;
dd.pb(aa);
st=1;
}
else{
for(int i=0;i<dd.size();i++){
if(dd[i]!=aa){
vis[i]=1;
}
}
}
}
swap(aa,bb);
adj[aa].insert(bb);
if(adj[aa].size()==3){
if(st==0){
st=1;
dd.pb(aa);
for(auto j:adj[aa]){
dd.pb(j);
}
}
else{
for(int i=0;i<dd.size();i++){
int stt=0;
if(dd[i]==aa){
stt=1;
}
for(auto j:adj[aa]){
if(dd[i]==j){
stt=1;
}
}
if(stt==0){
vis[i]=1;
// cout<<i<<"<........<"<<endl;
}
}
}
}
else if(adj[aa].size()==4){
if(st==0){
vis[1]=1;
vis[2]=1;
vis[3]=1;
dd.pb(aa);
st=1;
}
else{
for(int i=0;i<dd.size();i++){
if(dd[i]!=aa){
vis[i]=1;
}
}
}
}
// cout<<111<<"<"<<prev<<"<"<<st<<endl;
if(prev==0 and st==1){
// cout<<11<<endl;
for(int i=0;i<dd.size();i++){
// cout<<dd[i]<<"/";
for(auto j:ed){
push(j.a,j.b,i+1);
}
}
// cout<<endl;
}
if(st==0){
int x=find(aa,0);
int y=find(bb,0);
if(x!=y){
par[x][0]=y;
}
else{
co+=1;
if(co==1){
find3(aa,bb);
ans=path.size();
// cout<<endl;
}
else{
ans=0;
}
}
}
}
int CountCritical(){
if(ans==0){
return 0;
}
if(st){
int ans2=0;
for(int i=0;i<4;i++){
ans2+=1-vis[i];
}
return ans2;
}
else{
return ans;
}
}
/*int main() {
int tmp;
char *inbuf, *outbuf;
inbuf = (char*) malloc(inbuf_len * sizeof(char));
outbuf = (char*) malloc(outbuf_len * sizeof(char));
tmp = setvbuf(stdin, inbuf, _IOFBF, inbuf_len);
tmp = setvbuf(stdout, outbuf, _IOFBF, outbuf_len);
int N, L;
tmp = scanf("%d %d", &N, &L);
assert(tmp == 2);
Init(N);
int i;
for (i = 0; i < L; i++) {
int A, B;
tmp = scanf("%d", &A);
if (A == -1) {
int critical;
critical = CountCritical();
// printf("%d\n", critical);
cout<<critical<<endl;
} else {
tmp = scanf("%d", &B);
assert(tmp == 1);
Link(A, B);
}
}
return 0;
}*/
/*
7 13
-1
1 2
-1
0 5
-1
2 0
-1
3 2
-1
3 5
-1
4 3
-1
*/
/*
g++ -DEVAL -Wall -static -O2 -o aa grader.cpp rings.cpp
*/
Compilation message
rings.cpp: In function 'void Link(int, int)':
rings.cpp:81:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<dd.size();i++){
~^~~~~~~~~~
rings.cpp:120:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<dd.size();i++){
~^~~~~~~~~~
rings.cpp:147:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<dd.size();i++){
~^~~~~~~~~~
rings.cpp:165:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<dd.size();i++){
~^~~~~~~~~~
rings.cpp:192:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<dd.size();i++){
~^~~~~~~~~~
rings.cpp:202:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<dd.size();i++){
~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
47352 KB |
Output is correct |
2 |
Correct |
35 ms |
47864 KB |
Output is correct |
3 |
Correct |
38 ms |
48000 KB |
Output is correct |
4 |
Correct |
33 ms |
47360 KB |
Output is correct |
5 |
Correct |
37 ms |
47872 KB |
Output is correct |
6 |
Correct |
36 ms |
48376 KB |
Output is correct |
7 |
Correct |
32 ms |
47480 KB |
Output is correct |
8 |
Correct |
34 ms |
47864 KB |
Output is correct |
9 |
Correct |
35 ms |
48000 KB |
Output is correct |
10 |
Correct |
40 ms |
48000 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
678 ms |
110428 KB |
Output is correct |
2 |
Correct |
1344 ms |
144524 KB |
Output is correct |
3 |
Correct |
1912 ms |
163080 KB |
Output is correct |
4 |
Correct |
1541 ms |
170068 KB |
Output is correct |
5 |
Correct |
1572 ms |
188116 KB |
Output is correct |
6 |
Correct |
1764 ms |
231836 KB |
Output is correct |
7 |
Correct |
1770 ms |
170824 KB |
Output is correct |
8 |
Correct |
1742 ms |
173312 KB |
Output is correct |
9 |
Correct |
2068 ms |
181896 KB |
Output is correct |
10 |
Correct |
1099 ms |
177728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
47352 KB |
Output is correct |
2 |
Correct |
35 ms |
47864 KB |
Output is correct |
3 |
Correct |
38 ms |
48000 KB |
Output is correct |
4 |
Correct |
33 ms |
47360 KB |
Output is correct |
5 |
Correct |
37 ms |
47872 KB |
Output is correct |
6 |
Correct |
36 ms |
48376 KB |
Output is correct |
7 |
Correct |
32 ms |
47480 KB |
Output is correct |
8 |
Correct |
34 ms |
47864 KB |
Output is correct |
9 |
Correct |
35 ms |
48000 KB |
Output is correct |
10 |
Correct |
40 ms |
48000 KB |
Output is correct |
11 |
Correct |
36 ms |
48000 KB |
Output is correct |
12 |
Correct |
39 ms |
48880 KB |
Output is correct |
13 |
Correct |
39 ms |
48640 KB |
Output is correct |
14 |
Correct |
37 ms |
48248 KB |
Output is correct |
15 |
Correct |
39 ms |
48504 KB |
Output is correct |
16 |
Correct |
39 ms |
48680 KB |
Output is correct |
17 |
Correct |
39 ms |
48640 KB |
Output is correct |
18 |
Correct |
48 ms |
48888 KB |
Output is correct |
19 |
Correct |
40 ms |
48760 KB |
Output is correct |
20 |
Correct |
40 ms |
48640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
47352 KB |
Output is correct |
2 |
Correct |
35 ms |
47864 KB |
Output is correct |
3 |
Correct |
38 ms |
48000 KB |
Output is correct |
4 |
Correct |
33 ms |
47360 KB |
Output is correct |
5 |
Correct |
37 ms |
47872 KB |
Output is correct |
6 |
Correct |
36 ms |
48376 KB |
Output is correct |
7 |
Correct |
32 ms |
47480 KB |
Output is correct |
8 |
Correct |
34 ms |
47864 KB |
Output is correct |
9 |
Correct |
35 ms |
48000 KB |
Output is correct |
10 |
Correct |
40 ms |
48000 KB |
Output is correct |
11 |
Correct |
36 ms |
48000 KB |
Output is correct |
12 |
Correct |
39 ms |
48880 KB |
Output is correct |
13 |
Correct |
39 ms |
48640 KB |
Output is correct |
14 |
Correct |
37 ms |
48248 KB |
Output is correct |
15 |
Correct |
39 ms |
48504 KB |
Output is correct |
16 |
Correct |
39 ms |
48680 KB |
Output is correct |
17 |
Correct |
39 ms |
48640 KB |
Output is correct |
18 |
Correct |
48 ms |
48888 KB |
Output is correct |
19 |
Correct |
40 ms |
48760 KB |
Output is correct |
20 |
Correct |
40 ms |
48640 KB |
Output is correct |
21 |
Correct |
56 ms |
51576 KB |
Output is correct |
22 |
Correct |
76 ms |
53952 KB |
Output is correct |
23 |
Correct |
89 ms |
55852 KB |
Output is correct |
24 |
Correct |
116 ms |
55540 KB |
Output is correct |
25 |
Correct |
49 ms |
50512 KB |
Output is correct |
26 |
Correct |
89 ms |
56688 KB |
Output is correct |
27 |
Correct |
111 ms |
58732 KB |
Output is correct |
28 |
Correct |
112 ms |
59220 KB |
Output is correct |
29 |
Correct |
79 ms |
53108 KB |
Output is correct |
30 |
Correct |
144 ms |
62060 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
47352 KB |
Output is correct |
2 |
Correct |
35 ms |
47864 KB |
Output is correct |
3 |
Correct |
38 ms |
48000 KB |
Output is correct |
4 |
Correct |
33 ms |
47360 KB |
Output is correct |
5 |
Correct |
37 ms |
47872 KB |
Output is correct |
6 |
Correct |
36 ms |
48376 KB |
Output is correct |
7 |
Correct |
32 ms |
47480 KB |
Output is correct |
8 |
Correct |
34 ms |
47864 KB |
Output is correct |
9 |
Correct |
35 ms |
48000 KB |
Output is correct |
10 |
Correct |
40 ms |
48000 KB |
Output is correct |
11 |
Correct |
678 ms |
110428 KB |
Output is correct |
12 |
Correct |
1344 ms |
144524 KB |
Output is correct |
13 |
Correct |
1912 ms |
163080 KB |
Output is correct |
14 |
Correct |
1541 ms |
170068 KB |
Output is correct |
15 |
Correct |
1572 ms |
188116 KB |
Output is correct |
16 |
Correct |
1764 ms |
231836 KB |
Output is correct |
17 |
Correct |
1770 ms |
170824 KB |
Output is correct |
18 |
Correct |
1742 ms |
173312 KB |
Output is correct |
19 |
Correct |
2068 ms |
181896 KB |
Output is correct |
20 |
Correct |
1099 ms |
177728 KB |
Output is correct |
21 |
Correct |
36 ms |
48000 KB |
Output is correct |
22 |
Correct |
39 ms |
48880 KB |
Output is correct |
23 |
Correct |
39 ms |
48640 KB |
Output is correct |
24 |
Correct |
37 ms |
48248 KB |
Output is correct |
25 |
Correct |
39 ms |
48504 KB |
Output is correct |
26 |
Correct |
39 ms |
48680 KB |
Output is correct |
27 |
Correct |
39 ms |
48640 KB |
Output is correct |
28 |
Correct |
48 ms |
48888 KB |
Output is correct |
29 |
Correct |
40 ms |
48760 KB |
Output is correct |
30 |
Correct |
40 ms |
48640 KB |
Output is correct |
31 |
Correct |
56 ms |
51576 KB |
Output is correct |
32 |
Correct |
76 ms |
53952 KB |
Output is correct |
33 |
Correct |
89 ms |
55852 KB |
Output is correct |
34 |
Correct |
116 ms |
55540 KB |
Output is correct |
35 |
Correct |
49 ms |
50512 KB |
Output is correct |
36 |
Correct |
89 ms |
56688 KB |
Output is correct |
37 |
Correct |
111 ms |
58732 KB |
Output is correct |
38 |
Correct |
112 ms |
59220 KB |
Output is correct |
39 |
Correct |
79 ms |
53108 KB |
Output is correct |
40 |
Correct |
144 ms |
62060 KB |
Output is correct |
41 |
Correct |
313 ms |
83252 KB |
Output is correct |
42 |
Correct |
771 ms |
117540 KB |
Output is correct |
43 |
Correct |
323 ms |
77296 KB |
Output is correct |
44 |
Correct |
1081 ms |
168280 KB |
Output is correct |
45 |
Correct |
1266 ms |
137368 KB |
Output is correct |
46 |
Correct |
1044 ms |
159184 KB |
Output is correct |
47 |
Correct |
1503 ms |
194180 KB |
Output is correct |
48 |
Correct |
648 ms |
108004 KB |
Output is correct |
49 |
Correct |
948 ms |
151300 KB |
Output is correct |
50 |
Correct |
1024 ms |
150692 KB |
Output is correct |
51 |
Correct |
392 ms |
80748 KB |
Output is correct |
52 |
Correct |
937 ms |
146612 KB |
Output is correct |
53 |
Correct |
667 ms |
110428 KB |
Output is correct |
54 |
Correct |
1479 ms |
156368 KB |
Output is correct |
55 |
Correct |
1525 ms |
166128 KB |
Output is correct |