#include<bits/stdc++.h>
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#include "simurgh.h"
#define MAXN 507
#define MAXM 300007
using namespace std;
int n,m,dep[MAXN],edges;
vector< pair<int,int> > v[MAXN],tree[MAXN];
bool intree[MAXM],vis[MAXN];
pair<int,int> edge[MAXM],parent[MAXN];
int value[MAXM],st[MAXM],sumtree;
vector<int> ans;
unordered_set<int> s;
int questions;
int calc(vector<int> rem,vector<int> add){
questions++;
for(int i:rem)s.erase(i);
for(int i:add)s.insert(i);
vector<int> w;
for(int i:s){
w.push_back(i-1);
}
for(int i:rem)s.insert(i);
for(int i:add)s.erase(i);
return count_common_roads(w);
}
void dfs(int x){
vis[x]=true;
for(int i=0;i<v[x].size();i++){
if(vis[v[x][i].first])continue;
dep[v[x][i].first]=dep[x]+1;
parent[v[x][i].first]={x,v[x][i].second};
tree[x].push_back({v[x][i].first,v[x][i].second});
tree[v[x][i].first].push_back({x,v[x][i].second});
dfs(v[x][i].first);
intree[v[x][i].second]=true;
s.insert(v[x][i].second);
}
}
vector<int> path,toask;
int total;
void dfs2(int x){
vis[x]=true;
for(int i=0;i<tree[x].size();i++){
if(vis[tree[x][i].first])continue;
toask.push_back(tree[x][i].second-1);
total-=(value[tree[x][i].second]-1);
dfs2(tree[x][i].first);
}
}
bool ok;
void up(int x,int y){
if(x==y)return;
if(value[parent[x].second]==0 or ok){
if(value[parent[x].second])ok=false;
path.push_back(parent[x].second);
}
up(parent[x].first,y);
}
int howmany(int root,int l,int r){
toask.clear();
for(int i=1;i<=n;i++)vis[i]=false;
vis[root]=true;
for(int i=l;i<=r;i++)vis[v[root][i].first]=true;
for(int i=l;i<=r;i++){
toask.push_back(v[root][i].second-1);
}
total=0;
dfs2(root);
for(int i=l;i<=r;i++){
dfs2(v[root][i].first);
}
questions++;
total+=count_common_roads(toask);
return total;
}
void solve(int root,int l,int r,int s){
if(l==r and s>0){
value[v[root][l].second]=2;
return;
}
int mid=(l+r)/2;
int lt=howmany(root,l,mid);
if(lt>0){
solve(root,l,mid,lt);
}
if(s-lt){
solve(root,mid+1,r,s-lt);
}
}
vector<int> find_roads(int N,vector<int> from,vector<int> to){
n=N; m=int(from.size());
double start=clock();
for(int i=1;i<=m;i++){
from[i-1]++; to[i-1]++;
edge[i]={from[i-1],to[i-1]};
v[from[i-1]].push_back({to[i-1],i});
v[to[i-1]].push_back({from[i-1],i});
}
dfs(1);
sumtree=calc({},{});
for(int i=1;i<=m;i++){
if(intree[i])continue;
int u=from[i-1],w=to[i-1];
if(dep[u]>dep[w])swap(u,w);
path.clear(); ok=true;
up(w,u);
int mins=sumtree,maxs=sumtree,br=0,ask=0;
for(int f:path){
if(value[f]==0){
st[f]=calc({f},{i});
br++;
mins=min(mins,st[f]);
maxs=max(maxs,st[f]);
}
}
if(br==0)continue;
if(mins==maxs){
for(int f:path){
if(value[f]==0)continue;
st[f]=calc({f},{i});
ask=value[f];
mins=min(mins,st[f]);
maxs=max(maxs,st[f]);
break;
}
}
if(ask==0)ask=1;
for(int f:path){
if(value[f]!=0)continue;
if(mins==maxs)value[f]=ask;
else{
if(st[f]==mins)value[f]=2;
else value[f]=1;
}
}
}
if(questions>2*n)cout<<1/0;
for(int i=1;i<=m;i++){
if(intree[i] and value[i]==0)value[i]=2;
}
for(int i=1;i<=n;i++){
sort(v[i].begin(),v[i].end());
for(int f=0;f<v[i].size();f++){
if(v[i][f].first>i){
solve(i,f,v[i].size()-1,howmany(i,f,v[i].size()-1));
break;
}
}
}
for(int i=1;i<=m;i++){
if(value[i]==2){
ans.push_back(i-1);
}
}
return ans;
}
Compilation message
simurgh.cpp: In function 'void dfs(int)':
simurgh.cpp:46:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
46 | for(int i=0;i<v[x].size();i++){
| ~^~~~~~~~~~~~
simurgh.cpp: In function 'void dfs2(int)':
simurgh.cpp:68:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
68 | for(int i=0;i<tree[x].size();i++){
| ~^~~~~~~~~~~~~~~
simurgh.cpp: In function 'std::vector<int> find_roads(int, std::vector<int>, std::vector<int>)':
simurgh.cpp:203:26: warning: division by zero [-Wdiv-by-zero]
203 | if(questions>2*n)cout<<1/0;
| ~^~
simurgh.cpp:212:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
212 | for(int f=0;f<v[i].size();f++){
| ~^~~~~~~~~~~~
simurgh.cpp:139:9: warning: unused variable 'start' [-Wunused-variable]
139 | double start=clock();
| ^~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2384 KB |
correct |
2 |
Correct |
1 ms |
2384 KB |
correct |
3 |
Correct |
1 ms |
2384 KB |
correct |
4 |
Correct |
1 ms |
2384 KB |
correct |
5 |
Correct |
1 ms |
2384 KB |
correct |
6 |
Correct |
1 ms |
2384 KB |
correct |
7 |
Correct |
1 ms |
2384 KB |
correct |
8 |
Correct |
1 ms |
2552 KB |
correct |
9 |
Correct |
1 ms |
2384 KB |
correct |
10 |
Correct |
1 ms |
2384 KB |
correct |
11 |
Correct |
1 ms |
2384 KB |
correct |
12 |
Correct |
1 ms |
2384 KB |
correct |
13 |
Correct |
1 ms |
2384 KB |
correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2384 KB |
correct |
2 |
Correct |
1 ms |
2384 KB |
correct |
3 |
Correct |
1 ms |
2384 KB |
correct |
4 |
Correct |
1 ms |
2384 KB |
correct |
5 |
Correct |
1 ms |
2384 KB |
correct |
6 |
Correct |
1 ms |
2384 KB |
correct |
7 |
Correct |
1 ms |
2384 KB |
correct |
8 |
Correct |
1 ms |
2552 KB |
correct |
9 |
Correct |
1 ms |
2384 KB |
correct |
10 |
Correct |
1 ms |
2384 KB |
correct |
11 |
Correct |
1 ms |
2384 KB |
correct |
12 |
Correct |
1 ms |
2384 KB |
correct |
13 |
Correct |
1 ms |
2384 KB |
correct |
14 |
Correct |
2 ms |
2384 KB |
correct |
15 |
Correct |
2 ms |
2384 KB |
correct |
16 |
Correct |
2 ms |
2384 KB |
correct |
17 |
Correct |
2 ms |
2384 KB |
correct |
18 |
Correct |
2 ms |
2384 KB |
correct |
19 |
Correct |
2 ms |
2384 KB |
correct |
20 |
Correct |
2 ms |
2384 KB |
correct |
21 |
Correct |
2 ms |
2384 KB |
correct |
22 |
Correct |
2 ms |
2556 KB |
correct |
23 |
Correct |
1 ms |
2384 KB |
correct |
24 |
Correct |
2 ms |
2552 KB |
correct |
25 |
Correct |
1 ms |
2384 KB |
correct |
26 |
Correct |
2 ms |
2384 KB |
correct |
27 |
Correct |
2 ms |
2384 KB |
correct |
28 |
Correct |
1 ms |
2384 KB |
correct |
29 |
Correct |
1 ms |
2384 KB |
correct |
30 |
Correct |
2 ms |
2384 KB |
correct |
31 |
Correct |
2 ms |
2384 KB |
correct |
32 |
Correct |
2 ms |
2384 KB |
correct |
33 |
Correct |
1 ms |
2384 KB |
correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2384 KB |
correct |
2 |
Correct |
1 ms |
2384 KB |
correct |
3 |
Correct |
1 ms |
2384 KB |
correct |
4 |
Correct |
1 ms |
2384 KB |
correct |
5 |
Correct |
1 ms |
2384 KB |
correct |
6 |
Correct |
1 ms |
2384 KB |
correct |
7 |
Correct |
1 ms |
2384 KB |
correct |
8 |
Correct |
1 ms |
2552 KB |
correct |
9 |
Correct |
1 ms |
2384 KB |
correct |
10 |
Correct |
1 ms |
2384 KB |
correct |
11 |
Correct |
1 ms |
2384 KB |
correct |
12 |
Correct |
1 ms |
2384 KB |
correct |
13 |
Correct |
1 ms |
2384 KB |
correct |
14 |
Correct |
2 ms |
2384 KB |
correct |
15 |
Correct |
2 ms |
2384 KB |
correct |
16 |
Correct |
2 ms |
2384 KB |
correct |
17 |
Correct |
2 ms |
2384 KB |
correct |
18 |
Correct |
2 ms |
2384 KB |
correct |
19 |
Correct |
2 ms |
2384 KB |
correct |
20 |
Correct |
2 ms |
2384 KB |
correct |
21 |
Correct |
2 ms |
2384 KB |
correct |
22 |
Correct |
2 ms |
2556 KB |
correct |
23 |
Correct |
1 ms |
2384 KB |
correct |
24 |
Correct |
2 ms |
2552 KB |
correct |
25 |
Correct |
1 ms |
2384 KB |
correct |
26 |
Correct |
2 ms |
2384 KB |
correct |
27 |
Correct |
2 ms |
2384 KB |
correct |
28 |
Correct |
1 ms |
2384 KB |
correct |
29 |
Correct |
1 ms |
2384 KB |
correct |
30 |
Correct |
2 ms |
2384 KB |
correct |
31 |
Correct |
2 ms |
2384 KB |
correct |
32 |
Correct |
2 ms |
2384 KB |
correct |
33 |
Correct |
1 ms |
2384 KB |
correct |
34 |
Correct |
23 ms |
3664 KB |
correct |
35 |
Correct |
24 ms |
3664 KB |
correct |
36 |
Correct |
20 ms |
3420 KB |
correct |
37 |
Correct |
7 ms |
2384 KB |
correct |
38 |
Correct |
29 ms |
3744 KB |
correct |
39 |
Correct |
20 ms |
3664 KB |
correct |
40 |
Correct |
18 ms |
3408 KB |
correct |
41 |
Correct |
23 ms |
3508 KB |
correct |
42 |
Correct |
23 ms |
3608 KB |
correct |
43 |
Correct |
15 ms |
3152 KB |
correct |
44 |
Correct |
14 ms |
3064 KB |
correct |
45 |
Correct |
18 ms |
2896 KB |
correct |
46 |
Correct |
16 ms |
3052 KB |
correct |
47 |
Correct |
11 ms |
2640 KB |
correct |
48 |
Correct |
5 ms |
2384 KB |
correct |
49 |
Correct |
7 ms |
2384 KB |
correct |
50 |
Correct |
10 ms |
2676 KB |
correct |
51 |
Correct |
15 ms |
2896 KB |
correct |
52 |
Correct |
14 ms |
2896 KB |
correct |
53 |
Correct |
13 ms |
2896 KB |
correct |
54 |
Correct |
16 ms |
3332 KB |
correct |
55 |
Correct |
17 ms |
2896 KB |
correct |
56 |
Correct |
17 ms |
2896 KB |
correct |
57 |
Correct |
16 ms |
3064 KB |
correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2384 KB |
correct |
2 |
Correct |
1 ms |
2384 KB |
correct |
3 |
Correct |
79 ms |
5788 KB |
correct |
4 |
Correct |
132 ms |
7240 KB |
correct |
5 |
Correct |
132 ms |
7244 KB |
correct |
6 |
Correct |
127 ms |
7240 KB |
correct |
7 |
Correct |
115 ms |
7240 KB |
correct |
8 |
Correct |
113 ms |
7240 KB |
correct |
9 |
Correct |
130 ms |
7376 KB |
correct |
10 |
Correct |
134 ms |
7364 KB |
correct |
11 |
Correct |
142 ms |
7260 KB |
correct |
12 |
Correct |
131 ms |
7240 KB |
correct |
13 |
Correct |
1 ms |
2384 KB |
correct |
14 |
Correct |
129 ms |
7252 KB |
correct |
15 |
Correct |
132 ms |
7240 KB |
correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2384 KB |
correct |
2 |
Correct |
1 ms |
2384 KB |
correct |
3 |
Correct |
1 ms |
2384 KB |
correct |
4 |
Correct |
1 ms |
2384 KB |
correct |
5 |
Correct |
1 ms |
2384 KB |
correct |
6 |
Correct |
1 ms |
2384 KB |
correct |
7 |
Correct |
1 ms |
2384 KB |
correct |
8 |
Correct |
1 ms |
2552 KB |
correct |
9 |
Correct |
1 ms |
2384 KB |
correct |
10 |
Correct |
1 ms |
2384 KB |
correct |
11 |
Correct |
1 ms |
2384 KB |
correct |
12 |
Correct |
1 ms |
2384 KB |
correct |
13 |
Correct |
1 ms |
2384 KB |
correct |
14 |
Correct |
2 ms |
2384 KB |
correct |
15 |
Correct |
2 ms |
2384 KB |
correct |
16 |
Correct |
2 ms |
2384 KB |
correct |
17 |
Correct |
2 ms |
2384 KB |
correct |
18 |
Correct |
2 ms |
2384 KB |
correct |
19 |
Correct |
2 ms |
2384 KB |
correct |
20 |
Correct |
2 ms |
2384 KB |
correct |
21 |
Correct |
2 ms |
2384 KB |
correct |
22 |
Correct |
2 ms |
2556 KB |
correct |
23 |
Correct |
1 ms |
2384 KB |
correct |
24 |
Correct |
2 ms |
2552 KB |
correct |
25 |
Correct |
1 ms |
2384 KB |
correct |
26 |
Correct |
2 ms |
2384 KB |
correct |
27 |
Correct |
2 ms |
2384 KB |
correct |
28 |
Correct |
1 ms |
2384 KB |
correct |
29 |
Correct |
1 ms |
2384 KB |
correct |
30 |
Correct |
2 ms |
2384 KB |
correct |
31 |
Correct |
2 ms |
2384 KB |
correct |
32 |
Correct |
2 ms |
2384 KB |
correct |
33 |
Correct |
1 ms |
2384 KB |
correct |
34 |
Correct |
23 ms |
3664 KB |
correct |
35 |
Correct |
24 ms |
3664 KB |
correct |
36 |
Correct |
20 ms |
3420 KB |
correct |
37 |
Correct |
7 ms |
2384 KB |
correct |
38 |
Correct |
29 ms |
3744 KB |
correct |
39 |
Correct |
20 ms |
3664 KB |
correct |
40 |
Correct |
18 ms |
3408 KB |
correct |
41 |
Correct |
23 ms |
3508 KB |
correct |
42 |
Correct |
23 ms |
3608 KB |
correct |
43 |
Correct |
15 ms |
3152 KB |
correct |
44 |
Correct |
14 ms |
3064 KB |
correct |
45 |
Correct |
18 ms |
2896 KB |
correct |
46 |
Correct |
16 ms |
3052 KB |
correct |
47 |
Correct |
11 ms |
2640 KB |
correct |
48 |
Correct |
5 ms |
2384 KB |
correct |
49 |
Correct |
7 ms |
2384 KB |
correct |
50 |
Correct |
10 ms |
2676 KB |
correct |
51 |
Correct |
15 ms |
2896 KB |
correct |
52 |
Correct |
14 ms |
2896 KB |
correct |
53 |
Correct |
13 ms |
2896 KB |
correct |
54 |
Correct |
16 ms |
3332 KB |
correct |
55 |
Correct |
17 ms |
2896 KB |
correct |
56 |
Correct |
17 ms |
2896 KB |
correct |
57 |
Correct |
16 ms |
3064 KB |
correct |
58 |
Correct |
1 ms |
2384 KB |
correct |
59 |
Correct |
1 ms |
2384 KB |
correct |
60 |
Correct |
79 ms |
5788 KB |
correct |
61 |
Correct |
132 ms |
7240 KB |
correct |
62 |
Correct |
132 ms |
7244 KB |
correct |
63 |
Correct |
127 ms |
7240 KB |
correct |
64 |
Correct |
115 ms |
7240 KB |
correct |
65 |
Correct |
113 ms |
7240 KB |
correct |
66 |
Correct |
130 ms |
7376 KB |
correct |
67 |
Correct |
134 ms |
7364 KB |
correct |
68 |
Correct |
142 ms |
7260 KB |
correct |
69 |
Correct |
131 ms |
7240 KB |
correct |
70 |
Correct |
1 ms |
2384 KB |
correct |
71 |
Correct |
129 ms |
7252 KB |
correct |
72 |
Correct |
132 ms |
7240 KB |
correct |
73 |
Correct |
1 ms |
2384 KB |
correct |
74 |
Correct |
134 ms |
7240 KB |
correct |
75 |
Correct |
136 ms |
8008 KB |
correct |
76 |
Correct |
57 ms |
4688 KB |
correct |
77 |
Correct |
139 ms |
8184 KB |
correct |
78 |
Correct |
131 ms |
8280 KB |
correct |
79 |
Correct |
131 ms |
8152 KB |
correct |
80 |
Correct |
128 ms |
8236 KB |
correct |
81 |
Correct |
102 ms |
7496 KB |
correct |
82 |
Correct |
144 ms |
8176 KB |
correct |
83 |
Correct |
90 ms |
5616 KB |
correct |
84 |
Correct |
86 ms |
6276 KB |
correct |
85 |
Correct |
79 ms |
5968 KB |
correct |
86 |
Correct |
60 ms |
4700 KB |
correct |
87 |
Correct |
52 ms |
4432 KB |
correct |
88 |
Correct |
45 ms |
3852 KB |
correct |
89 |
Correct |
49 ms |
3820 KB |
correct |
90 |
Correct |
45 ms |
3560 KB |
correct |
91 |
Correct |
20 ms |
2640 KB |
correct |
92 |
Correct |
12 ms |
2808 KB |
correct |
93 |
Correct |
73 ms |
6136 KB |
correct |
94 |
Correct |
59 ms |
4928 KB |
correct |
95 |
Correct |
59 ms |
4688 KB |
correct |
96 |
Correct |
45 ms |
3776 KB |
correct |
97 |
Correct |
48 ms |
3892 KB |
correct |
98 |
Correct |
50 ms |
4600 KB |
correct |
99 |
Correct |
47 ms |
3664 KB |
correct |
100 |
Correct |
29 ms |
2928 KB |
correct |
101 |
Correct |
13 ms |
2640 KB |
correct |
102 |
Correct |
87 ms |
5176 KB |
correct |
103 |
Correct |
85 ms |
5200 KB |
correct |
104 |
Correct |
84 ms |
5200 KB |
correct |
105 |
Correct |
82 ms |
5200 KB |
correct |