#include<bits/stdc++.h>
#include "simurgh.h"
#define fi first
#define se second
using namespace std;
const int N=500;
const int M=N*N;
vector<pair<int,int>> e[N+10];
vector<int> ans;
bool wyn[M+10];
bool vis[N+10];
vector<int> alw;
vector<pair<int,int>> edg;
vector<int> base_tree_edg;
bool vis_curr[N+10];
bool vis_curr_e[M+10];
bool vis_e[M+10];
bool vis_solve[N+10];
int fau[N+10];
set<int> st_fau;
int fnd(int x)
{
if(fau[x]!=x)
fau[x]=fnd(fau[x]);
return fau[x];
}
void un(int x,int y)
{
fau[fnd(x)]=fnd(y);
return;
}
bool dfs_cycle(int x,int bg,vector<int> &cycle,vector<int> &cycle_e)
{
vis[x]=true;
st_fau.insert(fnd(x));
cycle.push_back(x);
for(auto v:e[x])
{
if(v.fi==bg && cycle.size()>2)
{
cycle_e.push_back(v.se);
return true;
}
if(vis[v.fi] || st_fau.find(fnd(v.fi))!=st_fau.end())
continue;
cycle_e.push_back(v.se);
if(dfs_cycle(v.fi,bg,cycle,cycle_e))
return true;
cycle_e.pop_back();
}
cycle.pop_back();
st_fau.erase(fnd(x));
return false;
}
int dfs_path(int x,vector<int> &path,vector<int> &path_e)
{
//cerr<<x<<"\n";
vis[x]=true;
path.push_back(x);
for(auto v:e[x])
{
if(vis[v.fi] || (vis_solve[v.fi] && !vis_curr[v.fi]))
continue;
if(vis_curr[v.fi] && path.size()>1)
{
path_e.push_back(v.se);
return v.fi;
}
else if(vis_curr[v.fi])
continue;
path_e.push_back(v.se);
int en=dfs_path(v.fi,path,path_e);
//cerr<<x<<" "<<v.fi<<" "<<en<<"\n";
if(en!=-1)
return en;
path_e.pop_back();
}
path.pop_back();
return -1;
}
void dfs_rest(int x,vector<int> &que)
{
vis[x]=true;
for(auto v:e[x])
{
if(!vis[v.fi])
{
que.push_back(v.se);
dfs_rest(v.fi,que);
}
}
return;
}
bool dfs_curr_rest(int x,int en,vector<int> &que)
{
if(x==en)
return true;
vis[x]=true;
for(auto v:e[x])
{
if(!vis[v.fi] && vis_curr_e[v.se])
{
que.push_back(v.se);
if(dfs_curr_rest(v.fi,en,que))
return true;
que.pop_back();
}
}
return false;
}
void add_full(vector<int> &cycle,vector<int> &cycle_e,int n)
{
//for(auto v:cycle)
// cerr<<v<<" ";
//cerr<<"\n";
//for(auto v:cycle_e)
// cerr<<v<<" ";
//cerr<<"\n\n";
int mn=n-1;
vector<int> g;
vector<int> que;
que.reserve(n-1);
for(auto v:cycle_e)
{
que.clear();
for(auto v2:cycle_e)
{
if(v2!=v)
que.push_back(v2);
}
for(int i=0;i<n;i++)
vis[i]=false;
for(auto v2:cycle)
vis[v2]=true;
for(auto v2:cycle)
dfs_rest(v2,que);
int tmp=count_common_roads(que);
//for(auto v:que)
// cerr<<v<<" ";
//cerr<<"\n"<<tmp<<"\n\n";
if(tmp<mn)
{
mn=tmp;
g.clear();
}
if(tmp==mn)
g.push_back(v);
}
if(g.size()==cycle_e.size())
g.clear();
for(auto v:g)
{
wyn[v]=true;
//cerr<<v<<"\n";
}
for(size_t i=0;i<cycle_e.size()-1;i++)
{
//cerr<<cycle_e[i]<<"\n";
base_tree_edg.push_back(cycle_e[i]);
}
return;
}
void add_path(int bg,int en,vector<int> &cycle,vector<int> &cycle_e,
vector<int> &path,vector<int> &path_e,int n)
{
int mn=n-1;
vector<int> g;
vector<int> que;
vector<int> rst;
que.reserve(n-1);
for(int i=0;i<n;i++)
vis[i]=false;
dfs_curr_rest(bg,en,rst);
//cerr<<bg<<" "<<en<<"\n";
//for(auto v:path_e)
// cerr<<v<<" ";
//cerr<<"\n";
//for(auto v:rst)
// cerr<<v<<" ";
//cerr<<"\n\n";
for(auto v:path_e)
{
que.clear();
for(auto v2:path_e)
{
if(v2!=v)
que.push_back(v2);
}
for(auto v2:rst)
que.push_back(v2);
for(int i=0;i<n;i++)
vis[i]=false;
for(auto v2:que)
vis[edg[v2].fi]=vis[edg[v2].se]=true;
for(int i=0;i<n;i++)
{
if(vis[i])
dfs_rest(i,que);
}
//for(auto v:que)
// cerr<<v<<" ";
//cerr<<"\n";
//cerr<<"xd\n";
int tmp=count_common_roads(que);
//cerr<<"xd1\n";
if(tmp<mn)
{
mn=tmp;
g.clear();
}
if(tmp==mn)
g.push_back(v);
}
que.clear();
for(auto v2:path_e)
que.push_back(v2);
for(size_t i=0;i<rst.size()-1;i++)
que.push_back(rst[i]);
for(int i=0;i<n;i++)
vis[i]=false;
for(auto v2:que)
vis[edg[v2].fi]=vis[edg[v2].se]=true;
for(int i=0;i<n;i++)
{
if(vis[i])
dfs_rest(i,que);
}
int tmp=count_common_roads(que);
if(tmp<mn || (tmp==mn && !wyn[rst.back()]))
g.clear();
for(auto v:g)
wyn[v]=true;
for(size_t i=0;i<path_e.size()-1;i++)
base_tree_edg.push_back(path_e[i]);
return;
}
void solve(int x,int n)
{
//cerr<<x<<"\n";
for(int i=0;i<n;i++)
vis_curr[i]=false;
for(size_t i=0;i<edg.size();i++)
vis_curr_e[i]=false;
vector<int> cycle,cycle_e;
for(int i=0;i<n;i++)
vis[i]=false;
st_fau.clear();
if(!dfs_cycle(x,x,cycle,cycle_e))
return;
add_full(cycle,cycle_e,n);
for(auto v:cycle)
vis_curr[v]=vis_solve[v]=true;
for(auto v:cycle_e)
{
vis_e[v]=vis_curr_e[v]=true;
un(edg[v].fi,edg[v].se);
}
//for(auto v:cycle)
// cerr<<v<<" ";
//cerr<<"\n";
vector<int> path,path_e;
while(true)
{
path.clear();
path_e.clear();
for(int i=0;i<n;i++)
vis[i]=false;
int bg,en;
for(bg=0;bg<n;bg++)
{
if(!vis_curr[bg])
continue;
en=dfs_path(bg,path,path_e);
if(en!=-1)
break;
}
if(path.empty())
break;
add_path(bg,en,cycle,cycle_e,path,path_e,n);
for(auto v:path)
vis_curr[v]=vis_solve[v]=true;
for(auto v:path_e)
{
vis_e[v]=vis_curr_e[v]=true;
un(edg[v].fi,edg[v].se);
}
}
return;
}
int ask_interval(int x,int l,int r,int n)
{
int tmp_s=0;
vector<int> tmp;
tmp.reserve(n-1);
for(int i=0;i<n;i++)
fau[i]=i;
for(auto v:e[x])
{
if(l<=v.fi && v.fi<=r)
{
tmp.push_back(v.se);
un(v.fi,x);
}
}
for(auto v:base_tree_edg)
{
if(fnd(edg[v].fi)!=fnd(edg[v].se))
{
un(edg[v].fi,edg[v].se);
tmp.push_back(v);
tmp_s-=wyn[v];
}
}
return tmp_s+count_common_roads(tmp);
}
void bs(int x,int l,int r,int s,int n)
{
if(s==0)
return;
if(l==r)
{
for(auto v:e[x])
{
if(v.fi==l)
{
wyn[v.se]=true;
break;
}
}
return;
}
int mid=(l+r)/2;
int tmp_s=ask_interval(x,l,mid,n);
bs(x,l,mid,tmp_s,n);
bs(x,mid+1,r,s-tmp_s,n);
return;
}
vector<int> find_roads(int n,vector<int> u,vector<int> v)
{
int m=u.size();
edg.resize(m);
for(int i=0;i<m;i++)
{
e[u[i]].emplace_back(v[i],i);
e[v[i]].emplace_back(u[i],i);
edg[i]={u[i],v[i]};
}
for(int i=0;i<n;i++)
fau[i]=i;
for(int i=0;i<n;i++)
solve(i,n);
for(int i=0;i<m;i++)
{
auto vv=edg[i];
if(fnd(vv.fi)!=fnd(vv.se))
{
base_tree_edg.push_back(i);
wyn[i]=true;
un(vv.fi,vv.se);
}
}
//for(auto v:base_tree_edg)
// cerr<<v<<" "<<wyn[v]<<"\n";
for(int i=0;i<n;i++)
bs(i,0,i-1,ask_interval(i,0,i-1,n),n);
for(int i=0;i<m;i++)
{
if(wyn[i])
{
ans.push_back(i);
//cerr<<i<<"\n";
}
}
return ans;
}
Compilation message
simurgh.cpp: In function 'void solve(int, int)':
simurgh.cpp:279:11: warning: 'en' may be used uninitialized in this function [-Wmaybe-uninitialized]
279 | add_path(bg,en,cycle,cycle_e,path,path_e,n);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
correct |
2 |
Correct |
1 ms |
204 KB |
correct |
3 |
Correct |
1 ms |
204 KB |
correct |
4 |
Correct |
1 ms |
204 KB |
correct |
5 |
Correct |
1 ms |
204 KB |
correct |
6 |
Correct |
1 ms |
204 KB |
correct |
7 |
Correct |
1 ms |
204 KB |
correct |
8 |
Correct |
1 ms |
204 KB |
correct |
9 |
Correct |
1 ms |
204 KB |
correct |
10 |
Correct |
1 ms |
204 KB |
correct |
11 |
Correct |
1 ms |
332 KB |
correct |
12 |
Correct |
2 ms |
324 KB |
correct |
13 |
Correct |
1 ms |
316 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
correct |
2 |
Correct |
1 ms |
204 KB |
correct |
3 |
Correct |
1 ms |
204 KB |
correct |
4 |
Correct |
1 ms |
204 KB |
correct |
5 |
Correct |
1 ms |
204 KB |
correct |
6 |
Correct |
1 ms |
204 KB |
correct |
7 |
Correct |
1 ms |
204 KB |
correct |
8 |
Correct |
1 ms |
204 KB |
correct |
9 |
Correct |
1 ms |
204 KB |
correct |
10 |
Correct |
1 ms |
204 KB |
correct |
11 |
Correct |
1 ms |
332 KB |
correct |
12 |
Correct |
2 ms |
324 KB |
correct |
13 |
Correct |
1 ms |
316 KB |
correct |
14 |
Correct |
2 ms |
332 KB |
correct |
15 |
Correct |
3 ms |
332 KB |
correct |
16 |
Correct |
2 ms |
332 KB |
correct |
17 |
Correct |
2 ms |
332 KB |
correct |
18 |
Correct |
2 ms |
332 KB |
correct |
19 |
Correct |
2 ms |
332 KB |
correct |
20 |
Correct |
2 ms |
332 KB |
correct |
21 |
Correct |
2 ms |
332 KB |
correct |
22 |
Correct |
2 ms |
332 KB |
correct |
23 |
Correct |
2 ms |
316 KB |
correct |
24 |
Correct |
2 ms |
332 KB |
correct |
25 |
Correct |
2 ms |
332 KB |
correct |
26 |
Correct |
2 ms |
316 KB |
correct |
27 |
Correct |
2 ms |
320 KB |
correct |
28 |
Correct |
2 ms |
320 KB |
correct |
29 |
Correct |
2 ms |
312 KB |
correct |
30 |
Correct |
2 ms |
332 KB |
correct |
31 |
Correct |
3 ms |
332 KB |
correct |
32 |
Correct |
2 ms |
332 KB |
correct |
33 |
Correct |
2 ms |
332 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
correct |
2 |
Correct |
1 ms |
204 KB |
correct |
3 |
Correct |
1 ms |
204 KB |
correct |
4 |
Correct |
1 ms |
204 KB |
correct |
5 |
Correct |
1 ms |
204 KB |
correct |
6 |
Correct |
1 ms |
204 KB |
correct |
7 |
Correct |
1 ms |
204 KB |
correct |
8 |
Correct |
1 ms |
204 KB |
correct |
9 |
Correct |
1 ms |
204 KB |
correct |
10 |
Correct |
1 ms |
204 KB |
correct |
11 |
Correct |
1 ms |
332 KB |
correct |
12 |
Correct |
2 ms |
324 KB |
correct |
13 |
Correct |
1 ms |
316 KB |
correct |
14 |
Correct |
2 ms |
332 KB |
correct |
15 |
Correct |
3 ms |
332 KB |
correct |
16 |
Correct |
2 ms |
332 KB |
correct |
17 |
Correct |
2 ms |
332 KB |
correct |
18 |
Correct |
2 ms |
332 KB |
correct |
19 |
Correct |
2 ms |
332 KB |
correct |
20 |
Correct |
2 ms |
332 KB |
correct |
21 |
Correct |
2 ms |
332 KB |
correct |
22 |
Correct |
2 ms |
332 KB |
correct |
23 |
Correct |
2 ms |
316 KB |
correct |
24 |
Correct |
2 ms |
332 KB |
correct |
25 |
Correct |
2 ms |
332 KB |
correct |
26 |
Correct |
2 ms |
316 KB |
correct |
27 |
Correct |
2 ms |
320 KB |
correct |
28 |
Correct |
2 ms |
320 KB |
correct |
29 |
Correct |
2 ms |
312 KB |
correct |
30 |
Correct |
2 ms |
332 KB |
correct |
31 |
Correct |
3 ms |
332 KB |
correct |
32 |
Correct |
2 ms |
332 KB |
correct |
33 |
Correct |
2 ms |
332 KB |
correct |
34 |
Correct |
90 ms |
1836 KB |
correct |
35 |
Correct |
54 ms |
1792 KB |
correct |
36 |
Correct |
70 ms |
1536 KB |
correct |
37 |
Correct |
30 ms |
388 KB |
correct |
38 |
Correct |
62 ms |
1824 KB |
correct |
39 |
Correct |
78 ms |
1680 KB |
correct |
40 |
Correct |
48 ms |
1484 KB |
correct |
41 |
Correct |
58 ms |
1816 KB |
correct |
42 |
Correct |
58 ms |
1816 KB |
correct |
43 |
Correct |
52 ms |
1228 KB |
correct |
44 |
Correct |
43 ms |
972 KB |
correct |
45 |
Correct |
47 ms |
972 KB |
correct |
46 |
Correct |
43 ms |
944 KB |
correct |
47 |
Correct |
31 ms |
588 KB |
correct |
48 |
Correct |
21 ms |
364 KB |
correct |
49 |
Correct |
25 ms |
448 KB |
correct |
50 |
Correct |
31 ms |
640 KB |
correct |
51 |
Correct |
51 ms |
1088 KB |
correct |
52 |
Correct |
41 ms |
1008 KB |
correct |
53 |
Correct |
40 ms |
948 KB |
correct |
54 |
Correct |
66 ms |
1280 KB |
correct |
55 |
Correct |
35 ms |
1100 KB |
correct |
56 |
Correct |
36 ms |
980 KB |
correct |
57 |
Correct |
38 ms |
1104 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
correct |
2 |
Correct |
1 ms |
332 KB |
correct |
3 |
Correct |
214 ms |
4232 KB |
correct |
4 |
Correct |
508 ms |
6848 KB |
correct |
5 |
Correct |
470 ms |
6860 KB |
correct |
6 |
Correct |
317 ms |
6828 KB |
correct |
7 |
Correct |
736 ms |
6832 KB |
correct |
8 |
Correct |
418 ms |
6852 KB |
correct |
9 |
Correct |
578 ms |
6920 KB |
correct |
10 |
Correct |
536 ms |
6980 KB |
correct |
11 |
Correct |
463 ms |
6852 KB |
correct |
12 |
Correct |
630 ms |
6744 KB |
correct |
13 |
Correct |
1 ms |
204 KB |
correct |
14 |
Correct |
309 ms |
6740 KB |
correct |
15 |
Correct |
329 ms |
6788 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
correct |
2 |
Correct |
1 ms |
204 KB |
correct |
3 |
Correct |
1 ms |
204 KB |
correct |
4 |
Correct |
1 ms |
204 KB |
correct |
5 |
Correct |
1 ms |
204 KB |
correct |
6 |
Correct |
1 ms |
204 KB |
correct |
7 |
Correct |
1 ms |
204 KB |
correct |
8 |
Correct |
1 ms |
204 KB |
correct |
9 |
Correct |
1 ms |
204 KB |
correct |
10 |
Correct |
1 ms |
204 KB |
correct |
11 |
Correct |
1 ms |
332 KB |
correct |
12 |
Correct |
2 ms |
324 KB |
correct |
13 |
Correct |
1 ms |
316 KB |
correct |
14 |
Correct |
2 ms |
332 KB |
correct |
15 |
Correct |
3 ms |
332 KB |
correct |
16 |
Correct |
2 ms |
332 KB |
correct |
17 |
Correct |
2 ms |
332 KB |
correct |
18 |
Correct |
2 ms |
332 KB |
correct |
19 |
Correct |
2 ms |
332 KB |
correct |
20 |
Correct |
2 ms |
332 KB |
correct |
21 |
Correct |
2 ms |
332 KB |
correct |
22 |
Correct |
2 ms |
332 KB |
correct |
23 |
Correct |
2 ms |
316 KB |
correct |
24 |
Correct |
2 ms |
332 KB |
correct |
25 |
Correct |
2 ms |
332 KB |
correct |
26 |
Correct |
2 ms |
316 KB |
correct |
27 |
Correct |
2 ms |
320 KB |
correct |
28 |
Correct |
2 ms |
320 KB |
correct |
29 |
Correct |
2 ms |
312 KB |
correct |
30 |
Correct |
2 ms |
332 KB |
correct |
31 |
Correct |
3 ms |
332 KB |
correct |
32 |
Correct |
2 ms |
332 KB |
correct |
33 |
Correct |
2 ms |
332 KB |
correct |
34 |
Correct |
90 ms |
1836 KB |
correct |
35 |
Correct |
54 ms |
1792 KB |
correct |
36 |
Correct |
70 ms |
1536 KB |
correct |
37 |
Correct |
30 ms |
388 KB |
correct |
38 |
Correct |
62 ms |
1824 KB |
correct |
39 |
Correct |
78 ms |
1680 KB |
correct |
40 |
Correct |
48 ms |
1484 KB |
correct |
41 |
Correct |
58 ms |
1816 KB |
correct |
42 |
Correct |
58 ms |
1816 KB |
correct |
43 |
Correct |
52 ms |
1228 KB |
correct |
44 |
Correct |
43 ms |
972 KB |
correct |
45 |
Correct |
47 ms |
972 KB |
correct |
46 |
Correct |
43 ms |
944 KB |
correct |
47 |
Correct |
31 ms |
588 KB |
correct |
48 |
Correct |
21 ms |
364 KB |
correct |
49 |
Correct |
25 ms |
448 KB |
correct |
50 |
Correct |
31 ms |
640 KB |
correct |
51 |
Correct |
51 ms |
1088 KB |
correct |
52 |
Correct |
41 ms |
1008 KB |
correct |
53 |
Correct |
40 ms |
948 KB |
correct |
54 |
Correct |
66 ms |
1280 KB |
correct |
55 |
Correct |
35 ms |
1100 KB |
correct |
56 |
Correct |
36 ms |
980 KB |
correct |
57 |
Correct |
38 ms |
1104 KB |
correct |
58 |
Correct |
1 ms |
204 KB |
correct |
59 |
Correct |
1 ms |
332 KB |
correct |
60 |
Correct |
214 ms |
4232 KB |
correct |
61 |
Correct |
508 ms |
6848 KB |
correct |
62 |
Correct |
470 ms |
6860 KB |
correct |
63 |
Correct |
317 ms |
6828 KB |
correct |
64 |
Correct |
736 ms |
6832 KB |
correct |
65 |
Correct |
418 ms |
6852 KB |
correct |
66 |
Correct |
578 ms |
6920 KB |
correct |
67 |
Correct |
536 ms |
6980 KB |
correct |
68 |
Correct |
463 ms |
6852 KB |
correct |
69 |
Correct |
630 ms |
6744 KB |
correct |
70 |
Correct |
1 ms |
204 KB |
correct |
71 |
Correct |
309 ms |
6740 KB |
correct |
72 |
Correct |
329 ms |
6788 KB |
correct |
73 |
Correct |
1 ms |
204 KB |
correct |
74 |
Correct |
479 ms |
6896 KB |
correct |
75 |
Correct |
471 ms |
6628 KB |
correct |
76 |
Correct |
177 ms |
2752 KB |
correct |
77 |
Correct |
336 ms |
6784 KB |
correct |
78 |
Correct |
615 ms |
6852 KB |
correct |
79 |
Correct |
340 ms |
6816 KB |
correct |
80 |
Correct |
505 ms |
6724 KB |
correct |
81 |
Correct |
251 ms |
5920 KB |
correct |
82 |
Correct |
415 ms |
6608 KB |
correct |
83 |
Correct |
287 ms |
3896 KB |
correct |
84 |
Correct |
278 ms |
4548 KB |
correct |
85 |
Correct |
388 ms |
4420 KB |
correct |
86 |
Correct |
307 ms |
2888 KB |
correct |
87 |
Correct |
221 ms |
2404 KB |
correct |
88 |
Correct |
199 ms |
1732 KB |
correct |
89 |
Correct |
193 ms |
1668 KB |
correct |
90 |
Correct |
176 ms |
1484 KB |
correct |
91 |
Correct |
112 ms |
460 KB |
correct |
92 |
Correct |
103 ms |
332 KB |
correct |
93 |
Correct |
382 ms |
4332 KB |
correct |
94 |
Correct |
269 ms |
2804 KB |
correct |
95 |
Correct |
314 ms |
2904 KB |
correct |
96 |
Correct |
210 ms |
1608 KB |
correct |
97 |
Correct |
206 ms |
1740 KB |
correct |
98 |
Correct |
243 ms |
2384 KB |
correct |
99 |
Correct |
192 ms |
1756 KB |
correct |
100 |
Correct |
137 ms |
708 KB |
correct |
101 |
Correct |
108 ms |
452 KB |
correct |
102 |
Correct |
251 ms |
3576 KB |
correct |
103 |
Correct |
230 ms |
3592 KB |
correct |
104 |
Correct |
215 ms |
3500 KB |
correct |
105 |
Correct |
212 ms |
3512 KB |
correct |