#include<iostream>
#include<stack>
#include<map>
#include<vector>
#include<string>
#include<cassert>
#include<unordered_map>
#include <queue>
#include <cstdint>
#include<cstring>
#include<limits.h>
#include<cmath>
#include<set>
#include<algorithm>
#include <iomanip>
#include<numeric>
#include<bitset>
using namespace std;
#define ll long long
#define f first
#define s second
#define pii pair<int,int>
#define ppii pair<int,pii>
#define vi vector<int>
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define F(n) for(int i=0;i<n;i++)
#define lb lower_bound
#define ub upper_bound
#define fastio ios::sync_with_stdio(false);cin.tie(NULL);
#pragma GCC optimize ("03,unroll-lopps")
using namespace std;
const int mod=1e9+7,mxn=1e6+5,inf=1e9,minf=-1e9,lg=25;
int n;
vector<int>adj[mxn+10];
int found=-1;
struct dsu{
int pa[mxn+10],ans=1,deg[mxn+10],dep[mxn+10];
void init(){for(int i=0;i<n;i++)pa[i]=i,deg[i]=dep[i]=0;}
int find(int u){
if(pa[u]==u)return u;
int x=find(pa[u]);
dep[u]+=dep[pa[u]];
return pa[u]=x;
}
bool merg(int a,int b){
int x=a,y=b;
a=find(a),b=find(b);
deg[x]++,deg[y]++;
if(deg[x]==3||deg[y]==3)ans=0;
if(a==b){
ans=0;
return 0;
}
pa[a]=b;
dep[x]=dep[y]+1;
return 1;
}
}t[5];
vector<pii>event;
vector<int>have;
int havecycle=-1;
void Init(int N_){
found=-1;
havecycle=-1;
n=N_;
t[0].init();
}
void make(int node,int pos){
t[pos].init();
for(auto i:event)if(i.f!=node&&i.s!=node)t[pos].merg(i.f,i.s);
}
void Link(int a,int b){
event.pb({a,b});
if(found!=-1){
t[0].merg(a,b);
for(int i=0;i<have.size();i++)if(a!=have[i]&&b!=have[i])t[i+1].merg(a,b);
}
else{
if(t[0].merg(a,b)==0){
if(havecycle==-1)havecycle=abs(t[0].dep[a]-t[0].dep[b])+1;
else havecycle=-2;
}
adj[a].pb(b);
adj[b].pb(a);
if(t[0].deg[a]==3)found=a;
else if(t[0].deg[b]==3)found=b;
if(found!=-1){
have.pb(found);
for(auto i:adj[found])have.pb(i);
for(int i=0;i<have.size();i++)make(have[i],i+1);
}
}
}
int CountCritical(){
if(found!=-1){
int sum=0;
for(int i=0;i<have.size();i++)sum+=t[i+1].ans;
return sum;
}
if(havecycle==-2)return 0;
if(havecycle!=-1)return havecycle;
return n;
}
/*
if we can find intersect from all these set:
1. in every cycle
2. have deg >=3
3. directly connected to deg 3
if there are no deg>3 then there can be atmost 4 candidates
because the node has to be directly connected to all deg 3 and every node deg<=3
if theres a node deg>3 we just have to check 1 node but the deg can be a lot
how to know if a node is in cycle?
*/
Compilation message
rings.cpp:32:40: warning: bad option '-funroll-lopps' to pragma 'optimize' [-Wpragmas]
32 | #pragma GCC optimize ("03,unroll-lopps")
| ^
rings.cpp:40:13: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
40 | void init(){for(int i=0;i<n;i++)pa[i]=i,deg[i]=dep[i]=0;}
| ^
rings.cpp:41:17: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
41 | int find(int u){
| ^
rings.cpp:47:24: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
47 | bool merg(int a,int b){
| ^
rings.cpp:64:17: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
64 | void Init(int N_){
| ^
rings.cpp:70:27: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
70 | void make(int node,int pos){
| ^
rings.cpp:74:22: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
74 | void Link(int a,int b){
| ^
rings.cpp: In function 'void Link(int, int)':
rings.cpp:78:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
78 | for(int i=0;i<have.size();i++)if(a!=have[i]&&b!=have[i])t[i+1].merg(a,b);
| ~^~~~~~~~~~~~
rings.cpp:92:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
92 | for(int i=0;i<have.size();i++)make(have[i],i+1);
| ~^~~~~~~~~~~~
rings.cpp: At global scope:
rings.cpp:97:19: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
97 | int CountCritical(){
| ^
rings.cpp: In function 'int CountCritical()':
rings.cpp:100:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
100 | for(int i=0;i<have.size();i++)sum+=t[i+1].ans;
| ~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
37212 KB |
Output is correct |
2 |
Correct |
8 ms |
53852 KB |
Output is correct |
3 |
Correct |
8 ms |
53852 KB |
Output is correct |
4 |
Incorrect |
5 ms |
37212 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
158 ms |
63928 KB |
Output is correct |
2 |
Correct |
400 ms |
106164 KB |
Output is correct |
3 |
Correct |
706 ms |
91144 KB |
Output is correct |
4 |
Incorrect |
462 ms |
84676 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
37212 KB |
Output is correct |
2 |
Correct |
8 ms |
53852 KB |
Output is correct |
3 |
Correct |
8 ms |
53852 KB |
Output is correct |
4 |
Incorrect |
5 ms |
37212 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
37212 KB |
Output is correct |
2 |
Correct |
8 ms |
53852 KB |
Output is correct |
3 |
Correct |
8 ms |
53852 KB |
Output is correct |
4 |
Incorrect |
5 ms |
37212 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
37212 KB |
Output is correct |
2 |
Correct |
8 ms |
53852 KB |
Output is correct |
3 |
Correct |
8 ms |
53852 KB |
Output is correct |
4 |
Incorrect |
5 ms |
37212 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |