#include <bits/stdc++.h>
#include "simurgh.h"
#pragma GCC optimize("Ofast")
using namespace std;
#define pb push_back
#define pii pair<int,int>
#define sz(s) (int)s.size()
#define F first
#define S second
#define all(v) v.begin(),v.end()
const int MAX=2e5+10;
struct dsu{
int f[MAX];
void init(int n){
for(int i=0;i<n;i++)f[i]=i;
}
int get(int v){
return (f[v]==v?v:f[v]=get(f[v]));
}
void unite(int a,int b){
a=get(a),b=get(b);
f[a]=b;
}
}d;
int n;
vector<pii> g[MAX];
vector<int> u,v;
int is1[MAX],is0[MAX],dn[MAX];
int pr[MAX],dep[MAX];
set<int> st;
void dfs(int st){
queue<int> q;
q.push(st);
while(!q.empty()){
int v=q.front();
q.pop();
for(auto to:g[v]){
if(pr[v]==to.S)continue;
dep[to.F]=dep[v]+1;
pr[to.F]=to.S;
q.push(to.F);
}
}
}
int cq=1;
vector<int> vc(set<int> &st){
vector<int> res;
for(int i:st)res.pb(i);
return res;
}
int add(vector<int> &vec){
int CNT=0;
d.init(n);
vector<int> x;
for(int i:vec){
x.pb(i);
d.unite(u[i],v[i]);
}
for(int i:st){
if(d.get(u[i])!=d.get(v[i])){
CNT+=is1[i];
d.unite(u[i],v[i]);
x.pb(i);
}
}
cq++;
// assert(cq<=8000);
return count_common_roads(x)-CNT;
}
vector<int> find_roads(int N,vector<int> U,vector<int> V){
n=N;
u=U;
v=V;
vector<int> e;
d.init(n);
for(int i=0;i<sz(U);i++){
if(d.get(U[i])!=d.get(V[i])){
d.unite(U[i],V[i]);
g[U[i]].pb({V[i],i});
g[V[i]].pb({U[i],i});
st.insert(i);
}
else e.pb(i);
}
for(int i=0;i<sz(U);i++)dn[i]=1;
for(int i=0;i<n;i++)pr[i]=-1;
dfs(0);
int cnt=count_common_roads(vc(st));
for(int num:e){
int a=U[num],b=V[num];
vector<int> canzam;
int allv=0;
int CNT=0;
while(a!=b){
CNT++;
if(CNT>N){
return vector<int>(n,-1);
}
if(dep[a]>dep[b]){
canzam.pb(pr[a]);
if(U[pr[a]]==a)a=V[pr[a]];
else a=U[pr[a]];
}
else{
canzam.pb(pr[b]);
if(U[pr[b]]==b)b=V[pr[b]];
else b=U[pr[b]];
}
}
int ok=0;
int canEdge=-1;
for(int edge:canzam){
if(dn[edge])allv=1;
if(is1[edge]){
ok=1;
canEdge=edge;
}
else if(is0[edge]){
ok=2;
canEdge=edge;
}
}
if(!allv)continue;
if(ok==0){
for(int edge:canzam){
assert(dn[edge]);
st.erase(edge);
st.insert(num);
cq++;
int ncnt=count_common_roads(vc(st));
if(ncnt<cnt){
dn[edge]=0;
is1[edge]=1;
dn[num]=0;
is0[num]=1;
}
else if(ncnt>cnt){
dn[edge]=0;
is0[edge]=1;
dn[num]=0;
is1[num]=1;
}
st.insert(edge);
st.erase(num);
}
if(dn[num]){
dn[num]=0;
is0[num]=1;
for(int edge:canzam){
dn[edge]=0;
is0[edge]=1;
}
}
else{
for(int edge:canzam){
if(dn[edge]){
dn[edge]=0;
is0[edge]=is0[num];
is1[edge]=is1[num];
}
}
}
}
else if(ok==1){
st.erase(canEdge);
st.insert(num);
int ncnt=count_common_roads(vc(st));
cq++;
st.insert(canEdge);
st.erase(num);
dn[num]=0;
if(ncnt==cnt){
is1[num]=1;
}
else{
is0[num]=1;
}
}
else{
st.erase(canEdge);
st.insert(num);
int ncnt=count_common_roads(vc(st));
cq++;
st.insert(canEdge);
st.erase(num);
dn[num]=0;
if(ncnt==cnt){
is0[num]=1;
}
else{
is1[num]=1;
}
}
for(int edge:canzam){
if(dn[edge]){
dn[edge]=0;
st.insert(num);
st.erase(edge);
cq++;
int ncnt=count_common_roads(vc(st));
st.erase(num);
st.insert(edge);
if(ncnt==cnt){
is0[edge]=is0[num];
is1[edge]=is1[num];
}
else{
is0[edge]=1-is0[num];
is1[edge]=1-is1[num];
}
}
}
}
assert(cq<=2*n+1);
for(int edge:st){
if(dn[edge]){
dn[edge]=0;
is1[edge]=1;
}
}
for(int i=0;i<n;i++){
g[i].clear();
}
for(int i=0;i<sz(u);i++){
g[u[i]].pb({v[i],i});
g[v[i]].pb({u[i],i});
}
for(int i=0;i<n;i++){
vector<int> vec;
for(auto to:g[i]){
if(!st.count(to.S)&&i>=to.F)vec.pb(to.S);
}
int cnt=add(vec);
// cout<<i<<" "<<cnt<<"\n";
for(int j=1;j<=cnt;j++){
int l=0,r=sz(vec)-1,res=sz(vec)-1;
// cerr<<l<<" "<<r<<"\n";
while(l<=r){
int mid=(l+r)/2;
vector<int> v;
for(int j=0;j<=mid;j++){
if(!st.count(vec[j]))v.pb(vec[j]);
}
if(add(v)>=j){
r=mid-1;
res=mid;
}
else l=mid+1;
}
is1[vec[res]]=1;
}
}
vector<int> ans;
for(int i=0;i<sz(u);i++)if(is1[i])ans.pb(i);
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8792 KB |
correct |
2 |
Correct |
1 ms |
8796 KB |
correct |
3 |
Correct |
1 ms |
8892 KB |
correct |
4 |
Correct |
1 ms |
8796 KB |
correct |
5 |
Correct |
1 ms |
8796 KB |
correct |
6 |
Correct |
1 ms |
8796 KB |
correct |
7 |
Correct |
1 ms |
8896 KB |
correct |
8 |
Correct |
1 ms |
8796 KB |
correct |
9 |
Correct |
1 ms |
8796 KB |
correct |
10 |
Correct |
1 ms |
8796 KB |
correct |
11 |
Correct |
1 ms |
8796 KB |
correct |
12 |
Correct |
1 ms |
8796 KB |
correct |
13 |
Correct |
1 ms |
8796 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8792 KB |
correct |
2 |
Correct |
1 ms |
8796 KB |
correct |
3 |
Correct |
1 ms |
8892 KB |
correct |
4 |
Correct |
1 ms |
8796 KB |
correct |
5 |
Correct |
1 ms |
8796 KB |
correct |
6 |
Correct |
1 ms |
8796 KB |
correct |
7 |
Correct |
1 ms |
8896 KB |
correct |
8 |
Correct |
1 ms |
8796 KB |
correct |
9 |
Correct |
1 ms |
8796 KB |
correct |
10 |
Correct |
1 ms |
8796 KB |
correct |
11 |
Correct |
1 ms |
8796 KB |
correct |
12 |
Correct |
1 ms |
8796 KB |
correct |
13 |
Correct |
1 ms |
8796 KB |
correct |
14 |
Correct |
2 ms |
8796 KB |
correct |
15 |
Correct |
2 ms |
8796 KB |
correct |
16 |
Correct |
2 ms |
8796 KB |
correct |
17 |
Correct |
2 ms |
8792 KB |
correct |
18 |
Correct |
1 ms |
8796 KB |
correct |
19 |
Correct |
2 ms |
8796 KB |
correct |
20 |
Correct |
2 ms |
8796 KB |
correct |
21 |
Correct |
2 ms |
8792 KB |
correct |
22 |
Correct |
2 ms |
8796 KB |
correct |
23 |
Correct |
1 ms |
8796 KB |
correct |
24 |
Correct |
1 ms |
8808 KB |
correct |
25 |
Correct |
1 ms |
8796 KB |
correct |
26 |
Correct |
1 ms |
8796 KB |
correct |
27 |
Correct |
1 ms |
8796 KB |
correct |
28 |
Correct |
1 ms |
8796 KB |
correct |
29 |
Correct |
1 ms |
8796 KB |
correct |
30 |
Correct |
2 ms |
8796 KB |
correct |
31 |
Correct |
1 ms |
8796 KB |
correct |
32 |
Correct |
2 ms |
8796 KB |
correct |
33 |
Correct |
2 ms |
8796 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8792 KB |
correct |
2 |
Correct |
1 ms |
8796 KB |
correct |
3 |
Correct |
1 ms |
8892 KB |
correct |
4 |
Correct |
1 ms |
8796 KB |
correct |
5 |
Correct |
1 ms |
8796 KB |
correct |
6 |
Correct |
1 ms |
8796 KB |
correct |
7 |
Correct |
1 ms |
8896 KB |
correct |
8 |
Correct |
1 ms |
8796 KB |
correct |
9 |
Correct |
1 ms |
8796 KB |
correct |
10 |
Correct |
1 ms |
8796 KB |
correct |
11 |
Correct |
1 ms |
8796 KB |
correct |
12 |
Correct |
1 ms |
8796 KB |
correct |
13 |
Correct |
1 ms |
8796 KB |
correct |
14 |
Correct |
2 ms |
8796 KB |
correct |
15 |
Correct |
2 ms |
8796 KB |
correct |
16 |
Correct |
2 ms |
8796 KB |
correct |
17 |
Correct |
2 ms |
8792 KB |
correct |
18 |
Correct |
1 ms |
8796 KB |
correct |
19 |
Correct |
2 ms |
8796 KB |
correct |
20 |
Correct |
2 ms |
8796 KB |
correct |
21 |
Correct |
2 ms |
8792 KB |
correct |
22 |
Correct |
2 ms |
8796 KB |
correct |
23 |
Correct |
1 ms |
8796 KB |
correct |
24 |
Correct |
1 ms |
8808 KB |
correct |
25 |
Correct |
1 ms |
8796 KB |
correct |
26 |
Correct |
1 ms |
8796 KB |
correct |
27 |
Correct |
1 ms |
8796 KB |
correct |
28 |
Correct |
1 ms |
8796 KB |
correct |
29 |
Correct |
1 ms |
8796 KB |
correct |
30 |
Correct |
2 ms |
8796 KB |
correct |
31 |
Correct |
1 ms |
8796 KB |
correct |
32 |
Correct |
2 ms |
8796 KB |
correct |
33 |
Correct |
2 ms |
8796 KB |
correct |
34 |
Correct |
31 ms |
10076 KB |
correct |
35 |
Correct |
30 ms |
10076 KB |
correct |
36 |
Correct |
29 ms |
9820 KB |
correct |
37 |
Correct |
7 ms |
8796 KB |
correct |
38 |
Correct |
30 ms |
10096 KB |
correct |
39 |
Correct |
28 ms |
10076 KB |
correct |
40 |
Correct |
24 ms |
9820 KB |
correct |
41 |
Correct |
28 ms |
10072 KB |
correct |
42 |
Correct |
27 ms |
10072 KB |
correct |
43 |
Correct |
9 ms |
9564 KB |
correct |
44 |
Correct |
8 ms |
9308 KB |
correct |
45 |
Correct |
8 ms |
9372 KB |
correct |
46 |
Correct |
7 ms |
9476 KB |
correct |
47 |
Correct |
5 ms |
9048 KB |
correct |
48 |
Correct |
3 ms |
8796 KB |
correct |
49 |
Correct |
3 ms |
9012 KB |
correct |
50 |
Correct |
5 ms |
9052 KB |
correct |
51 |
Correct |
8 ms |
9564 KB |
correct |
52 |
Correct |
8 ms |
9308 KB |
correct |
53 |
Correct |
8 ms |
9280 KB |
correct |
54 |
Correct |
9 ms |
9820 KB |
correct |
55 |
Correct |
9 ms |
9380 KB |
correct |
56 |
Correct |
9 ms |
9564 KB |
correct |
57 |
Correct |
10 ms |
9384 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8796 KB |
correct |
2 |
Correct |
2 ms |
8796 KB |
correct |
3 |
Correct |
94 ms |
12780 KB |
correct |
4 |
Correct |
151 ms |
14568 KB |
correct |
5 |
Correct |
152 ms |
14536 KB |
correct |
6 |
Correct |
130 ms |
14536 KB |
correct |
7 |
Correct |
145 ms |
14536 KB |
correct |
8 |
Correct |
142 ms |
14536 KB |
correct |
9 |
Correct |
150 ms |
14536 KB |
correct |
10 |
Correct |
151 ms |
14560 KB |
correct |
11 |
Correct |
148 ms |
14536 KB |
correct |
12 |
Correct |
163 ms |
14556 KB |
correct |
13 |
Correct |
1 ms |
8792 KB |
correct |
14 |
Correct |
142 ms |
14544 KB |
correct |
15 |
Correct |
152 ms |
14600 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8792 KB |
correct |
2 |
Correct |
1 ms |
8796 KB |
correct |
3 |
Correct |
1 ms |
8892 KB |
correct |
4 |
Correct |
1 ms |
8796 KB |
correct |
5 |
Correct |
1 ms |
8796 KB |
correct |
6 |
Correct |
1 ms |
8796 KB |
correct |
7 |
Correct |
1 ms |
8896 KB |
correct |
8 |
Correct |
1 ms |
8796 KB |
correct |
9 |
Correct |
1 ms |
8796 KB |
correct |
10 |
Correct |
1 ms |
8796 KB |
correct |
11 |
Correct |
1 ms |
8796 KB |
correct |
12 |
Correct |
1 ms |
8796 KB |
correct |
13 |
Correct |
1 ms |
8796 KB |
correct |
14 |
Correct |
2 ms |
8796 KB |
correct |
15 |
Correct |
2 ms |
8796 KB |
correct |
16 |
Correct |
2 ms |
8796 KB |
correct |
17 |
Correct |
2 ms |
8792 KB |
correct |
18 |
Correct |
1 ms |
8796 KB |
correct |
19 |
Correct |
2 ms |
8796 KB |
correct |
20 |
Correct |
2 ms |
8796 KB |
correct |
21 |
Correct |
2 ms |
8792 KB |
correct |
22 |
Correct |
2 ms |
8796 KB |
correct |
23 |
Correct |
1 ms |
8796 KB |
correct |
24 |
Correct |
1 ms |
8808 KB |
correct |
25 |
Correct |
1 ms |
8796 KB |
correct |
26 |
Correct |
1 ms |
8796 KB |
correct |
27 |
Correct |
1 ms |
8796 KB |
correct |
28 |
Correct |
1 ms |
8796 KB |
correct |
29 |
Correct |
1 ms |
8796 KB |
correct |
30 |
Correct |
2 ms |
8796 KB |
correct |
31 |
Correct |
1 ms |
8796 KB |
correct |
32 |
Correct |
2 ms |
8796 KB |
correct |
33 |
Correct |
2 ms |
8796 KB |
correct |
34 |
Correct |
31 ms |
10076 KB |
correct |
35 |
Correct |
30 ms |
10076 KB |
correct |
36 |
Correct |
29 ms |
9820 KB |
correct |
37 |
Correct |
7 ms |
8796 KB |
correct |
38 |
Correct |
30 ms |
10096 KB |
correct |
39 |
Correct |
28 ms |
10076 KB |
correct |
40 |
Correct |
24 ms |
9820 KB |
correct |
41 |
Correct |
28 ms |
10072 KB |
correct |
42 |
Correct |
27 ms |
10072 KB |
correct |
43 |
Correct |
9 ms |
9564 KB |
correct |
44 |
Correct |
8 ms |
9308 KB |
correct |
45 |
Correct |
8 ms |
9372 KB |
correct |
46 |
Correct |
7 ms |
9476 KB |
correct |
47 |
Correct |
5 ms |
9048 KB |
correct |
48 |
Correct |
3 ms |
8796 KB |
correct |
49 |
Correct |
3 ms |
9012 KB |
correct |
50 |
Correct |
5 ms |
9052 KB |
correct |
51 |
Correct |
8 ms |
9564 KB |
correct |
52 |
Correct |
8 ms |
9308 KB |
correct |
53 |
Correct |
8 ms |
9280 KB |
correct |
54 |
Correct |
9 ms |
9820 KB |
correct |
55 |
Correct |
9 ms |
9380 KB |
correct |
56 |
Correct |
9 ms |
9564 KB |
correct |
57 |
Correct |
10 ms |
9384 KB |
correct |
58 |
Correct |
1 ms |
8796 KB |
correct |
59 |
Correct |
2 ms |
8796 KB |
correct |
60 |
Correct |
94 ms |
12780 KB |
correct |
61 |
Correct |
151 ms |
14568 KB |
correct |
62 |
Correct |
152 ms |
14536 KB |
correct |
63 |
Correct |
130 ms |
14536 KB |
correct |
64 |
Correct |
145 ms |
14536 KB |
correct |
65 |
Correct |
142 ms |
14536 KB |
correct |
66 |
Correct |
150 ms |
14536 KB |
correct |
67 |
Correct |
151 ms |
14560 KB |
correct |
68 |
Correct |
148 ms |
14536 KB |
correct |
69 |
Correct |
163 ms |
14556 KB |
correct |
70 |
Correct |
1 ms |
8792 KB |
correct |
71 |
Correct |
142 ms |
14544 KB |
correct |
72 |
Correct |
152 ms |
14600 KB |
correct |
73 |
Correct |
2 ms |
8792 KB |
correct |
74 |
Correct |
163 ms |
14552 KB |
correct |
75 |
Correct |
150 ms |
15336 KB |
correct |
76 |
Correct |
73 ms |
11328 KB |
correct |
77 |
Correct |
157 ms |
15308 KB |
correct |
78 |
Correct |
157 ms |
15308 KB |
correct |
79 |
Correct |
151 ms |
15556 KB |
correct |
80 |
Correct |
147 ms |
15336 KB |
correct |
81 |
Correct |
132 ms |
14536 KB |
correct |
82 |
Correct |
163 ms |
15308 KB |
correct |
83 |
Correct |
123 ms |
12496 KB |
correct |
84 |
Correct |
42 ms |
13260 KB |
correct |
85 |
Correct |
39 ms |
13008 KB |
correct |
86 |
Correct |
29 ms |
11412 KB |
correct |
87 |
Correct |
24 ms |
10844 KB |
correct |
88 |
Correct |
20 ms |
10332 KB |
correct |
89 |
Correct |
20 ms |
10240 KB |
correct |
90 |
Correct |
20 ms |
10068 KB |
correct |
91 |
Correct |
11 ms |
9052 KB |
correct |
92 |
Correct |
8 ms |
8796 KB |
correct |
93 |
Correct |
38 ms |
12880 KB |
correct |
94 |
Correct |
29 ms |
11480 KB |
correct |
95 |
Correct |
33 ms |
11472 KB |
correct |
96 |
Correct |
19 ms |
10076 KB |
correct |
97 |
Correct |
24 ms |
10332 KB |
correct |
98 |
Correct |
23 ms |
10844 KB |
correct |
99 |
Correct |
21 ms |
10332 KB |
correct |
100 |
Correct |
12 ms |
9184 KB |
correct |
101 |
Correct |
12 ms |
8796 KB |
correct |
102 |
Correct |
41 ms |
12284 KB |
correct |
103 |
Correct |
41 ms |
12240 KB |
correct |
104 |
Correct |
43 ms |
12144 KB |
correct |
105 |
Correct |
43 ms |
12240 KB |
correct |