#include <bits/stdc++.h>
#ifndef SKY
#include "simurgh.h"
#endif // SKY
using namespace std;
#define N 510
#define ll long long
#define fs first
#define sc second
#define ii pair<int,int>
#define pb push_back
int tmp[N],n,m,times,ktr[N*N],par_id[N];
ii cnt[N],canh[N*N];
vector<ii>g[N];
vector<int>lis;
struct DSU
{
int lab[N];
void init()
{
memset(lab,-1,sizeof(lab));
}
int root(int u)
{
if(lab[u]<0)
return u;
return(lab[u]=root(lab[u]));
}
bool update(int u,int v)
{
if((u=root(u))==(v=root(v)))
return 0;
if(lab[u]>lab[v])
swap(u,v);
lab[u]+=lab[v];
lab[v]=u;
return 1;
}
}T;
#ifdef SKY
int chon[N*N];
int count_common_roads(vector<int>r)
{
int res=0;
for(auto i:r)
res+=chon[i];
return res;
}
#endif // SKY
int query=0;
int my_ask(vector<int>r)
{
assert(r.size()==n-1);
query++;
assert(query<=8000);
return count_common_roads(r);
}
void DFS(int u,int p)
{
cnt[u].fs=++times;
for(auto v:g[u])
if(v.fs!=p)
{
par_id[v.fs]=v.sc;
DFS(v.fs,u);
}
cnt[u].sc=times;
}
bool tren(int u,int v)
{
return (cnt[u].fs<=cnt[v].fs&&cnt[u].sc>=cnt[v].sc);
}
ii get_cnt(int u,int v)
{
ii res={0,0};
for(int i=0;i<n;i++)
if(tren(i,u)+tren(i,v)==1)
{
if(ktr[par_id[i]]==-1)
res.fs++;
else res.sc++;
}
return res;
}
void solve(vector<int>s)
{
if(s.empty())
return;
vector<int>hoi;
T.init();
for(auto u:s)
T.update(canh[u].fs,canh[u].sc),hoi.pb(u);
int val=0;
for(auto i:lis)
if(T.update(canh[i].fs,canh[i].sc))
val+=ktr[i],hoi.pb(i);
if(val==my_ask(hoi))
{
for(auto u:s)
ktr[u]=0;
return;
}
if(s.size()==1)
{
ktr[s[0]]=1;
return;
}
vector<int>L,R;
for(int i=0;i<s.size();i++)
if(i*2<s.size())
L.pb(s[i]);
else R.pb(s[i]);
solve(L);
solve(R);
}
vector<int> find_roads(int cc, vector<int> canh_fs, vector<int> canh_sc)
{
n=cc;
m=canh_fs.size();
for(int i=0;i<m;i++)
canh[i]={canh_fs[i],canh_sc[i]};
memset(ktr,-1,sizeof(ktr));
T.init();
for(int i=0;i<m;i++)
if(T.update(canh[i].fs,canh[i].sc))
{
lis.pb(i);
// cout<<i<<endl;
g[canh[i].fs].pb({canh[i].sc,i});
g[canh[i].sc].pb({canh[i].fs,i});
}
memset(par_id,-1,sizeof(par_id));
DFS(0,-1);
int val=my_ask(lis);
//cout<<val<<endl;
for(int i=0;i<m;i++)
{
ii cc=get_cnt(canh[i].fs,canh[i].sc);
if(cc.fs>0&&cc.fs+cc.sc>1)
{
int u=canh[i].fs,v=canh[i].sc;
if(cc.sc==0)
{
int kt=0;
for(int j=0;j<n;j++)
if(tren(j,u)+tren(j,v)==1)
{
vector<int>luu=lis;
for(int t=0;t<n-1;t++)
if(luu[t]==par_id[j])
luu[t]=i;
tmp[j]=my_ask(luu);
if(tmp[j]>val)
kt=1;
}
for(int j=0;j<n;j++)
if(tren(j,u)+tren(j,v)==1)
{
if(kt==0)
{
ktr[par_id[j]]=(tmp[j]<val);
}else
{
ktr[par_id[j]]=(tmp[j]==val);
}
}
}else
{
int kt=0;
for(int j=0;j<n;j++)
if(tren(j,u)+tren(j,v)==1&&ktr[par_id[j]]!=-1)
{
vector<int>luu=lis;
for(int t=0;t<n-1;t++)
if(luu[t]==par_id[j])
luu[t]=i;
if(ktr[par_id[j]]==0)
{
kt=(my_ask(luu)>val);
}else
{
kt=(my_ask(luu)==val);
}
break;
}
for(int j=0;j<n;j++)
if(tren(j,u)+tren(j,v)==1&&ktr[par_id[j]]==-1)
{
vector<int>luu=lis;
for(int t=0;t<n-1;t++)
if(luu[t]==par_id[j])
luu[t]=i;
tmp[j]=my_ask(luu);
if(kt==0)
{
ktr[par_id[j]]=(tmp[j]<val);
}else
{
ktr[par_id[j]]=(tmp[j]==val);
}
}
}
}
}
for(auto i:lis)
if(ktr[i]==-1)
ktr[i]=1;
while(1)
{
int kt=0;
for(int i=0;i<m;i++)
if(ktr[i]==-1)
kt=1;
if(kt==0)
break;
T.init();
vector<int>s;
for(int i=0;i<m;i++)
if(ktr[i]==-1&&T.update(canh[i].fs,canh[i].sc))
s.pb(i);
solve(s);
}
vector<int>kq;
for(int i=0;i<m;i++)
if(ktr[i]==1)
kq.pb(i);
return kq;
}
#ifdef SKY
int main()
{
freopen("A.inp","r",stdin);
freopen("A.out","w",stdout);
ios::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
int n,m;
cin>>n>>m;
vector<int>u,v;
for(int i=1;i<=m;i++)
{
int x,y;
cin>>x>>y;
u.pb(x);
v.pb(y);
}
for(int i=1;i<n;i++)
{
int u;
cin>>u;
// cout<<u<<endl;
chon[u]=1;
}
vector<int>kq=find_roads(n,u,v);
cout<<"ASK "<<query<<endl;
for(int i=0;i<n-1;i++)cout<<kq[i]<<"\n";
return 0;
}
#endif
Compilation message
In file included from /usr/include/c++/10/cassert:44,
from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:33,
from simurgh.cpp:1:
simurgh.cpp: In function 'int my_ask(std::vector<int>)':
simurgh.cpp:64:20: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
64 | assert(r.size()==n-1);
| ~~~~~~~~^~~~~
simurgh.cpp: In function 'void solve(std::vector<int>)':
simurgh.cpp:124:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
124 | for(int i=0;i<s.size();i++)
| ~^~~~~~~~~
simurgh.cpp:125:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
125 | if(i*2<s.size())
| ~~~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1236 KB |
correct |
2 |
Correct |
1 ms |
1236 KB |
correct |
3 |
Correct |
1 ms |
1236 KB |
correct |
4 |
Correct |
1 ms |
1236 KB |
correct |
5 |
Correct |
1 ms |
1236 KB |
correct |
6 |
Correct |
1 ms |
1236 KB |
correct |
7 |
Correct |
1 ms |
1236 KB |
correct |
8 |
Correct |
1 ms |
1236 KB |
correct |
9 |
Correct |
1 ms |
1236 KB |
correct |
10 |
Correct |
1 ms |
1236 KB |
correct |
11 |
Correct |
1 ms |
1236 KB |
correct |
12 |
Correct |
1 ms |
1236 KB |
correct |
13 |
Correct |
1 ms |
1236 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1236 KB |
correct |
2 |
Correct |
1 ms |
1236 KB |
correct |
3 |
Correct |
1 ms |
1236 KB |
correct |
4 |
Correct |
1 ms |
1236 KB |
correct |
5 |
Correct |
1 ms |
1236 KB |
correct |
6 |
Correct |
1 ms |
1236 KB |
correct |
7 |
Correct |
1 ms |
1236 KB |
correct |
8 |
Correct |
1 ms |
1236 KB |
correct |
9 |
Correct |
1 ms |
1236 KB |
correct |
10 |
Correct |
1 ms |
1236 KB |
correct |
11 |
Correct |
1 ms |
1236 KB |
correct |
12 |
Correct |
1 ms |
1236 KB |
correct |
13 |
Correct |
1 ms |
1236 KB |
correct |
14 |
Correct |
4 ms |
1364 KB |
correct |
15 |
Correct |
3 ms |
1364 KB |
correct |
16 |
Correct |
4 ms |
1364 KB |
correct |
17 |
Correct |
3 ms |
1364 KB |
correct |
18 |
Correct |
3 ms |
1364 KB |
correct |
19 |
Correct |
4 ms |
1364 KB |
correct |
20 |
Correct |
3 ms |
1364 KB |
correct |
21 |
Correct |
2 ms |
1364 KB |
correct |
22 |
Correct |
2 ms |
1364 KB |
correct |
23 |
Correct |
2 ms |
1364 KB |
correct |
24 |
Correct |
2 ms |
1364 KB |
correct |
25 |
Correct |
1 ms |
1236 KB |
correct |
26 |
Correct |
2 ms |
1364 KB |
correct |
27 |
Correct |
2 ms |
1364 KB |
correct |
28 |
Correct |
1 ms |
1364 KB |
correct |
29 |
Correct |
1 ms |
1364 KB |
correct |
30 |
Correct |
1 ms |
1364 KB |
correct |
31 |
Correct |
2 ms |
1364 KB |
correct |
32 |
Correct |
1 ms |
1364 KB |
correct |
33 |
Correct |
2 ms |
1364 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1236 KB |
correct |
2 |
Correct |
1 ms |
1236 KB |
correct |
3 |
Correct |
1 ms |
1236 KB |
correct |
4 |
Correct |
1 ms |
1236 KB |
correct |
5 |
Correct |
1 ms |
1236 KB |
correct |
6 |
Correct |
1 ms |
1236 KB |
correct |
7 |
Correct |
1 ms |
1236 KB |
correct |
8 |
Correct |
1 ms |
1236 KB |
correct |
9 |
Correct |
1 ms |
1236 KB |
correct |
10 |
Correct |
1 ms |
1236 KB |
correct |
11 |
Correct |
1 ms |
1236 KB |
correct |
12 |
Correct |
1 ms |
1236 KB |
correct |
13 |
Correct |
1 ms |
1236 KB |
correct |
14 |
Correct |
4 ms |
1364 KB |
correct |
15 |
Correct |
3 ms |
1364 KB |
correct |
16 |
Correct |
4 ms |
1364 KB |
correct |
17 |
Correct |
3 ms |
1364 KB |
correct |
18 |
Correct |
3 ms |
1364 KB |
correct |
19 |
Correct |
4 ms |
1364 KB |
correct |
20 |
Correct |
3 ms |
1364 KB |
correct |
21 |
Correct |
2 ms |
1364 KB |
correct |
22 |
Correct |
2 ms |
1364 KB |
correct |
23 |
Correct |
2 ms |
1364 KB |
correct |
24 |
Correct |
2 ms |
1364 KB |
correct |
25 |
Correct |
1 ms |
1236 KB |
correct |
26 |
Correct |
2 ms |
1364 KB |
correct |
27 |
Correct |
2 ms |
1364 KB |
correct |
28 |
Correct |
1 ms |
1364 KB |
correct |
29 |
Correct |
1 ms |
1364 KB |
correct |
30 |
Correct |
1 ms |
1364 KB |
correct |
31 |
Correct |
2 ms |
1364 KB |
correct |
32 |
Correct |
1 ms |
1364 KB |
correct |
33 |
Correct |
2 ms |
1364 KB |
correct |
34 |
Correct |
121 ms |
2016 KB |
correct |
35 |
Correct |
109 ms |
2012 KB |
correct |
36 |
Correct |
80 ms |
1804 KB |
correct |
37 |
Correct |
18 ms |
1408 KB |
correct |
38 |
Correct |
114 ms |
2016 KB |
correct |
39 |
Correct |
98 ms |
1928 KB |
correct |
40 |
Correct |
79 ms |
1804 KB |
correct |
41 |
Correct |
115 ms |
2032 KB |
correct |
42 |
Correct |
123 ms |
2152 KB |
correct |
43 |
Correct |
42 ms |
1716 KB |
correct |
44 |
Correct |
33 ms |
1568 KB |
correct |
45 |
Correct |
38 ms |
1580 KB |
correct |
46 |
Correct |
25 ms |
1604 KB |
correct |
47 |
Correct |
10 ms |
1364 KB |
correct |
48 |
Correct |
3 ms |
1364 KB |
correct |
49 |
Correct |
5 ms |
1364 KB |
correct |
50 |
Correct |
11 ms |
1364 KB |
correct |
51 |
Correct |
30 ms |
1632 KB |
correct |
52 |
Correct |
28 ms |
1640 KB |
correct |
53 |
Correct |
24 ms |
1492 KB |
correct |
54 |
Correct |
35 ms |
1696 KB |
correct |
55 |
Correct |
42 ms |
1620 KB |
correct |
56 |
Correct |
38 ms |
1620 KB |
correct |
57 |
Correct |
36 ms |
1696 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1236 KB |
correct |
2 |
Correct |
1 ms |
1236 KB |
correct |
3 |
Correct |
452 ms |
3280 KB |
correct |
4 |
Runtime error |
785 ms |
8568 KB |
Execution killed with signal 6 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1236 KB |
correct |
2 |
Correct |
1 ms |
1236 KB |
correct |
3 |
Correct |
1 ms |
1236 KB |
correct |
4 |
Correct |
1 ms |
1236 KB |
correct |
5 |
Correct |
1 ms |
1236 KB |
correct |
6 |
Correct |
1 ms |
1236 KB |
correct |
7 |
Correct |
1 ms |
1236 KB |
correct |
8 |
Correct |
1 ms |
1236 KB |
correct |
9 |
Correct |
1 ms |
1236 KB |
correct |
10 |
Correct |
1 ms |
1236 KB |
correct |
11 |
Correct |
1 ms |
1236 KB |
correct |
12 |
Correct |
1 ms |
1236 KB |
correct |
13 |
Correct |
1 ms |
1236 KB |
correct |
14 |
Correct |
4 ms |
1364 KB |
correct |
15 |
Correct |
3 ms |
1364 KB |
correct |
16 |
Correct |
4 ms |
1364 KB |
correct |
17 |
Correct |
3 ms |
1364 KB |
correct |
18 |
Correct |
3 ms |
1364 KB |
correct |
19 |
Correct |
4 ms |
1364 KB |
correct |
20 |
Correct |
3 ms |
1364 KB |
correct |
21 |
Correct |
2 ms |
1364 KB |
correct |
22 |
Correct |
2 ms |
1364 KB |
correct |
23 |
Correct |
2 ms |
1364 KB |
correct |
24 |
Correct |
2 ms |
1364 KB |
correct |
25 |
Correct |
1 ms |
1236 KB |
correct |
26 |
Correct |
2 ms |
1364 KB |
correct |
27 |
Correct |
2 ms |
1364 KB |
correct |
28 |
Correct |
1 ms |
1364 KB |
correct |
29 |
Correct |
1 ms |
1364 KB |
correct |
30 |
Correct |
1 ms |
1364 KB |
correct |
31 |
Correct |
2 ms |
1364 KB |
correct |
32 |
Correct |
1 ms |
1364 KB |
correct |
33 |
Correct |
2 ms |
1364 KB |
correct |
34 |
Correct |
121 ms |
2016 KB |
correct |
35 |
Correct |
109 ms |
2012 KB |
correct |
36 |
Correct |
80 ms |
1804 KB |
correct |
37 |
Correct |
18 ms |
1408 KB |
correct |
38 |
Correct |
114 ms |
2016 KB |
correct |
39 |
Correct |
98 ms |
1928 KB |
correct |
40 |
Correct |
79 ms |
1804 KB |
correct |
41 |
Correct |
115 ms |
2032 KB |
correct |
42 |
Correct |
123 ms |
2152 KB |
correct |
43 |
Correct |
42 ms |
1716 KB |
correct |
44 |
Correct |
33 ms |
1568 KB |
correct |
45 |
Correct |
38 ms |
1580 KB |
correct |
46 |
Correct |
25 ms |
1604 KB |
correct |
47 |
Correct |
10 ms |
1364 KB |
correct |
48 |
Correct |
3 ms |
1364 KB |
correct |
49 |
Correct |
5 ms |
1364 KB |
correct |
50 |
Correct |
11 ms |
1364 KB |
correct |
51 |
Correct |
30 ms |
1632 KB |
correct |
52 |
Correct |
28 ms |
1640 KB |
correct |
53 |
Correct |
24 ms |
1492 KB |
correct |
54 |
Correct |
35 ms |
1696 KB |
correct |
55 |
Correct |
42 ms |
1620 KB |
correct |
56 |
Correct |
38 ms |
1620 KB |
correct |
57 |
Correct |
36 ms |
1696 KB |
correct |
58 |
Correct |
1 ms |
1236 KB |
correct |
59 |
Correct |
1 ms |
1236 KB |
correct |
60 |
Correct |
452 ms |
3280 KB |
correct |
61 |
Runtime error |
785 ms |
8568 KB |
Execution killed with signal 6 |
62 |
Halted |
0 ms |
0 KB |
- |