#include <bits/stdc++.h>
#define ll long long
#define db long double
#define N 505
#define II pair <ll,ll>
#define III pair <ll,II>
#define IV pair <vector <int>,vector <int> >
#define IDB pair <db,int>
#define TII pair <treap*,treap*>
#define fst first
#define snd second
#define BIT(x,i) ((x>>i)&1)
#define pi acos(-1)
#define to_radian(x) (x*pi/180.0)
#define to_degree(x) (x*180.0/pi)
#define Log(x) (31-__builtin_clz((int)x))
#define LogLL(x) (63-__builtin_clzll((ll)x)
using namespace std;
#include "simurgh.h"
//#define count_common_roads(vec) IR.common(vec)
struct Interactive
{
int n;
set <int> s;
void Init(int _n)
{
n=_n;
int x;
for(int i=1;i<n;i++) cin>>x,s.insert(x);
}
int common(vector <int> vec)
{
int res=0;
for(int x:vec)
if(s.find(x)!=s.end()) res++;
return res;
}
} IR;
struct DSU
{
int r[N];
void Init(int n)
{
for(int u=1;u<=n;u++) r[u]=u;
}
int root(int u) { return (u==r[u]) ? u : r[u]=root(r[u]); }
bool join(int u,int v)
{
if(root(u)==root(v)) return 0;
r[root(u)]=root(v);
return 1;
}
} DS;
int n,m,i,j,u,v,in_MyTree[N*N],in_ZalTree[N*N],num[N][N],p[N],done[N],U[N*N],V[N*N],MyTree_score,h[N],deg[N];
vector <int> MyTree,a[N];
int Score(vector <int> vec)
{
DS.Init(n);
vector <int> NowTree;
for(int x:vec)
{
DS.join(U[x],V[x]);
NowTree.push_back(x);
}
int added_score=0;
for(int x:MyTree)
if(DS.join(U[x],V[x]))
{
NowTree.push_back(x);
added_score+=in_ZalTree[x];
}
return count_common_roads(NowTree)-added_score;
}
vector <int> MyTree_Replace(int u,int v)
{
vector <int> res;
for(int x:MyTree)
if(x!=u) res.push_back(x);
res.push_back(v);
return res;
}
void DFS(int u)
{
for(int v:a[u])
if(p[v]==0)
{
p[v]=u; h[v]=h[u]+1;
DFS(v);
MyTree.push_back(num[u][v]);
in_MyTree[num[u][v]]=1;
}
}
vector <int> find_roads(int _n,vector <int> _U,vector <int> _V)
{
n=_n; m=_U.size();
for(i=0;i<m;i++) U[i]=_U[i]+1,V[i]=_V[i]+1;
for(i=0;i<m;i++)
{
u=U[i]; v=V[i];
a[u].push_back(v); a[v].push_back(u);
num[u][v]=num[v][u]=i;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Determine MyTree
p[1]=-1;
DFS(1);
MyTree_score=count_common_roads(MyTree);
memset(in_ZalTree,-1,sizeof(in_ZalTree));
for(i=0;i<m;i++)
if(!in_MyTree[i])
{
vector <int> path;
auto Walk=[&](int u,int v)
{
int res=-1;
if(h[u]<h[v]) swap(u,v);
while(h[u]>h[v])
{
if(in_ZalTree[num[u][p[u]]]==-1)
path.push_back(num[u][p[u]]);
else res=num[u][p[u]];
u=p[u];
}
while(u!=v)
{
if(in_ZalTree[num[u][p[u]]]==-1)
path.push_back(num[u][p[u]]);
else res=num[u][p[u]];
u=p[u];
if(in_ZalTree[num[v][p[v]]]==-1)
path.push_back(num[v][p[v]]);
else res=num[v][p[v]];
v=p[v];
}
return res;
};
int determined_edge=Walk(U[i],V[i]);
if(path.size()==0) continue;
if(determined_edge==-1)
{
vector <int> Bigger,Smaller,Equal;
for(int x:path)
{
int k=count_common_roads(MyTree_Replace(x,i));
if(MyTree_score>k)
Bigger.push_back(x);
else if(MyTree_score<k)
Smaller.push_back(x);
else Equal.push_back(x);
}
if(Bigger.size()>0)
{
for(int x:Bigger) in_ZalTree[x]=1;
for(int x:Equal) in_ZalTree[x]=0;
}
else if(Smaller.size()>0)
{
for(int x:Smaller) in_ZalTree[x]=0;
for(int x:Equal) in_ZalTree[x]=1;
}
else
for(int x:Equal) in_ZalTree[x]=0;
}
else
{
int k=count_common_roads(MyTree_Replace(determined_edge,i));
int i_score=in_ZalTree[determined_edge];
if(k>MyTree_score) i_score++;
if(k<MyTree_score) i_score--;
for(int x:path)
{
int k=count_common_roads(MyTree_Replace(x,i));
in_ZalTree[x]=i_score;
if(MyTree_score>k) in_ZalTree[x]++;
if(MyTree_score<k) in_ZalTree[x]--;
}
}
}
for(int x:MyTree)
if(in_ZalTree[x]==-1) in_ZalTree[x]=1;
//////////////////////////////////////////////////////////////////////////////////////////////////
//Determine ZalTree
queue <int> q;
for(u=1;u<=n;u++)
{
vector <int> vec;
for(int v:a[u]) vec.push_back(num[u][v]);
deg[u]=Score(vec);
if(deg[u]==1) q.push(u);
}
vector <int> res;
for(i=1;i<n;i++)
{
int u=q.front(); q.pop();
done[u]=1;
vector <int> candidate;
for(int v:a[u])
if(done[v]==0)
candidate.push_back(num[u][v]);
int l=0,r=candidate.size()-1;
while(l<r)
{
int mid=(l+r)>>1;
vector <int> vec;
for(j=0;j<=mid;j++) vec.push_back(candidate[j]);
if(Score(vec)>0) r=mid; else l=mid+1;
}
int x=candidate[l];
res.push_back(x);
if(U[x]==u) v=V[x]; else v=U[x];
deg[v]--;
if(deg[v]==1) q.push(v);
}
return res;
}
/*
int main()
{
freopen("simurgh.inp","r",stdin);
freopen("simurgh.out","w",stdout);
int n,k,u,v;
cin>>n;
IR.Init(n);
vector <int> U,V;
while(cin>>u>>v) U.push_back(u),V.push_back(v);
vector <int> vec=find_roads(n,U,V);
for(int x:vec) cout<<x<<" ";
}
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
correct |
2 |
Correct |
1 ms |
4444 KB |
correct |
3 |
Correct |
1 ms |
4440 KB |
correct |
4 |
Correct |
1 ms |
4444 KB |
correct |
5 |
Correct |
1 ms |
4444 KB |
correct |
6 |
Correct |
1 ms |
4444 KB |
correct |
7 |
Correct |
1 ms |
4444 KB |
correct |
8 |
Correct |
1 ms |
4440 KB |
correct |
9 |
Correct |
1 ms |
4440 KB |
correct |
10 |
Correct |
1 ms |
4696 KB |
correct |
11 |
Correct |
1 ms |
4444 KB |
correct |
12 |
Correct |
1 ms |
4440 KB |
correct |
13 |
Correct |
1 ms |
4440 KB |
correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
correct |
2 |
Correct |
1 ms |
4444 KB |
correct |
3 |
Correct |
1 ms |
4440 KB |
correct |
4 |
Correct |
1 ms |
4444 KB |
correct |
5 |
Correct |
1 ms |
4444 KB |
correct |
6 |
Correct |
1 ms |
4444 KB |
correct |
7 |
Correct |
1 ms |
4444 KB |
correct |
8 |
Correct |
1 ms |
4440 KB |
correct |
9 |
Correct |
1 ms |
4440 KB |
correct |
10 |
Correct |
1 ms |
4696 KB |
correct |
11 |
Correct |
1 ms |
4444 KB |
correct |
12 |
Correct |
1 ms |
4440 KB |
correct |
13 |
Correct |
1 ms |
4440 KB |
correct |
14 |
Correct |
2 ms |
4444 KB |
correct |
15 |
Correct |
2 ms |
4444 KB |
correct |
16 |
Correct |
2 ms |
4440 KB |
correct |
17 |
Correct |
2 ms |
4440 KB |
correct |
18 |
Correct |
1 ms |
4444 KB |
correct |
19 |
Correct |
2 ms |
4444 KB |
correct |
20 |
Correct |
2 ms |
4440 KB |
correct |
21 |
Correct |
2 ms |
4444 KB |
correct |
22 |
Correct |
1 ms |
4444 KB |
correct |
23 |
Correct |
1 ms |
4444 KB |
correct |
24 |
Correct |
2 ms |
4560 KB |
correct |
25 |
Correct |
1 ms |
4444 KB |
correct |
26 |
Correct |
1 ms |
4444 KB |
correct |
27 |
Correct |
1 ms |
4444 KB |
correct |
28 |
Correct |
1 ms |
4444 KB |
correct |
29 |
Correct |
1 ms |
4444 KB |
correct |
30 |
Correct |
1 ms |
4444 KB |
correct |
31 |
Correct |
1 ms |
4440 KB |
correct |
32 |
Correct |
1 ms |
4444 KB |
correct |
33 |
Correct |
1 ms |
4440 KB |
correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
correct |
2 |
Correct |
1 ms |
4444 KB |
correct |
3 |
Correct |
1 ms |
4440 KB |
correct |
4 |
Correct |
1 ms |
4444 KB |
correct |
5 |
Correct |
1 ms |
4444 KB |
correct |
6 |
Correct |
1 ms |
4444 KB |
correct |
7 |
Correct |
1 ms |
4444 KB |
correct |
8 |
Correct |
1 ms |
4440 KB |
correct |
9 |
Correct |
1 ms |
4440 KB |
correct |
10 |
Correct |
1 ms |
4696 KB |
correct |
11 |
Correct |
1 ms |
4444 KB |
correct |
12 |
Correct |
1 ms |
4440 KB |
correct |
13 |
Correct |
1 ms |
4440 KB |
correct |
14 |
Correct |
2 ms |
4444 KB |
correct |
15 |
Correct |
2 ms |
4444 KB |
correct |
16 |
Correct |
2 ms |
4440 KB |
correct |
17 |
Correct |
2 ms |
4440 KB |
correct |
18 |
Correct |
1 ms |
4444 KB |
correct |
19 |
Correct |
2 ms |
4444 KB |
correct |
20 |
Correct |
2 ms |
4440 KB |
correct |
21 |
Correct |
2 ms |
4444 KB |
correct |
22 |
Correct |
1 ms |
4444 KB |
correct |
23 |
Correct |
1 ms |
4444 KB |
correct |
24 |
Correct |
2 ms |
4560 KB |
correct |
25 |
Correct |
1 ms |
4444 KB |
correct |
26 |
Correct |
1 ms |
4444 KB |
correct |
27 |
Correct |
1 ms |
4444 KB |
correct |
28 |
Correct |
1 ms |
4444 KB |
correct |
29 |
Correct |
1 ms |
4444 KB |
correct |
30 |
Correct |
1 ms |
4444 KB |
correct |
31 |
Correct |
1 ms |
4440 KB |
correct |
32 |
Correct |
1 ms |
4444 KB |
correct |
33 |
Correct |
1 ms |
4440 KB |
correct |
34 |
Correct |
27 ms |
5468 KB |
correct |
35 |
Correct |
28 ms |
5616 KB |
correct |
36 |
Correct |
22 ms |
5464 KB |
correct |
37 |
Correct |
5 ms |
4444 KB |
correct |
38 |
Correct |
27 ms |
5468 KB |
correct |
39 |
Correct |
25 ms |
5464 KB |
correct |
40 |
Correct |
22 ms |
5212 KB |
correct |
41 |
Correct |
30 ms |
5628 KB |
correct |
42 |
Correct |
27 ms |
5464 KB |
correct |
43 |
Correct |
15 ms |
5208 KB |
correct |
44 |
Correct |
13 ms |
4956 KB |
correct |
45 |
Correct |
14 ms |
4956 KB |
correct |
46 |
Correct |
12 ms |
4956 KB |
correct |
47 |
Correct |
11 ms |
4956 KB |
correct |
48 |
Correct |
3 ms |
4440 KB |
correct |
49 |
Correct |
5 ms |
4644 KB |
correct |
50 |
Correct |
8 ms |
4700 KB |
correct |
51 |
Correct |
15 ms |
4956 KB |
correct |
52 |
Correct |
13 ms |
4956 KB |
correct |
53 |
Correct |
12 ms |
4956 KB |
correct |
54 |
Correct |
16 ms |
5236 KB |
correct |
55 |
Correct |
17 ms |
4956 KB |
correct |
56 |
Correct |
17 ms |
4956 KB |
correct |
57 |
Correct |
18 ms |
5124 KB |
correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
correct |
2 |
Correct |
1 ms |
4444 KB |
correct |
3 |
Correct |
90 ms |
7516 KB |
correct |
4 |
Correct |
152 ms |
8816 KB |
correct |
5 |
Correct |
154 ms |
8792 KB |
correct |
6 |
Correct |
150 ms |
8784 KB |
correct |
7 |
Correct |
153 ms |
9060 KB |
correct |
8 |
Correct |
155 ms |
8784 KB |
correct |
9 |
Correct |
152 ms |
8776 KB |
correct |
10 |
Correct |
152 ms |
8800 KB |
correct |
11 |
Correct |
152 ms |
8808 KB |
correct |
12 |
Correct |
153 ms |
8796 KB |
correct |
13 |
Correct |
1 ms |
4444 KB |
correct |
14 |
Correct |
151 ms |
8796 KB |
correct |
15 |
Correct |
153 ms |
8788 KB |
correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
correct |
2 |
Correct |
1 ms |
4444 KB |
correct |
3 |
Correct |
1 ms |
4440 KB |
correct |
4 |
Correct |
1 ms |
4444 KB |
correct |
5 |
Correct |
1 ms |
4444 KB |
correct |
6 |
Correct |
1 ms |
4444 KB |
correct |
7 |
Correct |
1 ms |
4444 KB |
correct |
8 |
Correct |
1 ms |
4440 KB |
correct |
9 |
Correct |
1 ms |
4440 KB |
correct |
10 |
Correct |
1 ms |
4696 KB |
correct |
11 |
Correct |
1 ms |
4444 KB |
correct |
12 |
Correct |
1 ms |
4440 KB |
correct |
13 |
Correct |
1 ms |
4440 KB |
correct |
14 |
Correct |
2 ms |
4444 KB |
correct |
15 |
Correct |
2 ms |
4444 KB |
correct |
16 |
Correct |
2 ms |
4440 KB |
correct |
17 |
Correct |
2 ms |
4440 KB |
correct |
18 |
Correct |
1 ms |
4444 KB |
correct |
19 |
Correct |
2 ms |
4444 KB |
correct |
20 |
Correct |
2 ms |
4440 KB |
correct |
21 |
Correct |
2 ms |
4444 KB |
correct |
22 |
Correct |
1 ms |
4444 KB |
correct |
23 |
Correct |
1 ms |
4444 KB |
correct |
24 |
Correct |
2 ms |
4560 KB |
correct |
25 |
Correct |
1 ms |
4444 KB |
correct |
26 |
Correct |
1 ms |
4444 KB |
correct |
27 |
Correct |
1 ms |
4444 KB |
correct |
28 |
Correct |
1 ms |
4444 KB |
correct |
29 |
Correct |
1 ms |
4444 KB |
correct |
30 |
Correct |
1 ms |
4444 KB |
correct |
31 |
Correct |
1 ms |
4440 KB |
correct |
32 |
Correct |
1 ms |
4444 KB |
correct |
33 |
Correct |
1 ms |
4440 KB |
correct |
34 |
Correct |
27 ms |
5468 KB |
correct |
35 |
Correct |
28 ms |
5616 KB |
correct |
36 |
Correct |
22 ms |
5464 KB |
correct |
37 |
Correct |
5 ms |
4444 KB |
correct |
38 |
Correct |
27 ms |
5468 KB |
correct |
39 |
Correct |
25 ms |
5464 KB |
correct |
40 |
Correct |
22 ms |
5212 KB |
correct |
41 |
Correct |
30 ms |
5628 KB |
correct |
42 |
Correct |
27 ms |
5464 KB |
correct |
43 |
Correct |
15 ms |
5208 KB |
correct |
44 |
Correct |
13 ms |
4956 KB |
correct |
45 |
Correct |
14 ms |
4956 KB |
correct |
46 |
Correct |
12 ms |
4956 KB |
correct |
47 |
Correct |
11 ms |
4956 KB |
correct |
48 |
Correct |
3 ms |
4440 KB |
correct |
49 |
Correct |
5 ms |
4644 KB |
correct |
50 |
Correct |
8 ms |
4700 KB |
correct |
51 |
Correct |
15 ms |
4956 KB |
correct |
52 |
Correct |
13 ms |
4956 KB |
correct |
53 |
Correct |
12 ms |
4956 KB |
correct |
54 |
Correct |
16 ms |
5236 KB |
correct |
55 |
Correct |
17 ms |
4956 KB |
correct |
56 |
Correct |
17 ms |
4956 KB |
correct |
57 |
Correct |
18 ms |
5124 KB |
correct |
58 |
Correct |
1 ms |
4444 KB |
correct |
59 |
Correct |
1 ms |
4444 KB |
correct |
60 |
Correct |
90 ms |
7516 KB |
correct |
61 |
Correct |
152 ms |
8816 KB |
correct |
62 |
Correct |
154 ms |
8792 KB |
correct |
63 |
Correct |
150 ms |
8784 KB |
correct |
64 |
Correct |
153 ms |
9060 KB |
correct |
65 |
Correct |
155 ms |
8784 KB |
correct |
66 |
Correct |
152 ms |
8776 KB |
correct |
67 |
Correct |
152 ms |
8800 KB |
correct |
68 |
Correct |
152 ms |
8808 KB |
correct |
69 |
Correct |
153 ms |
8796 KB |
correct |
70 |
Correct |
1 ms |
4444 KB |
correct |
71 |
Correct |
151 ms |
8796 KB |
correct |
72 |
Correct |
153 ms |
8788 KB |
correct |
73 |
Correct |
1 ms |
4444 KB |
correct |
74 |
Correct |
153 ms |
8800 KB |
correct |
75 |
Correct |
151 ms |
8532 KB |
correct |
76 |
Correct |
64 ms |
6240 KB |
correct |
77 |
Correct |
156 ms |
8880 KB |
correct |
78 |
Correct |
156 ms |
8772 KB |
correct |
79 |
Correct |
152 ms |
8804 KB |
correct |
80 |
Correct |
161 ms |
8536 KB |
correct |
81 |
Correct |
136 ms |
8480 KB |
correct |
82 |
Correct |
153 ms |
8708 KB |
correct |
83 |
Correct |
100 ms |
6744 KB |
correct |
84 |
Correct |
82 ms |
7256 KB |
correct |
85 |
Correct |
74 ms |
7000 KB |
correct |
86 |
Correct |
56 ms |
6236 KB |
correct |
87 |
Correct |
45 ms |
5964 KB |
correct |
88 |
Correct |
40 ms |
5464 KB |
correct |
89 |
Correct |
38 ms |
5468 KB |
correct |
90 |
Correct |
36 ms |
5208 KB |
correct |
91 |
Correct |
16 ms |
4696 KB |
correct |
92 |
Correct |
10 ms |
4440 KB |
correct |
93 |
Correct |
73 ms |
7004 KB |
correct |
94 |
Correct |
57 ms |
6232 KB |
correct |
95 |
Correct |
54 ms |
6236 KB |
correct |
96 |
Correct |
37 ms |
5208 KB |
correct |
97 |
Correct |
42 ms |
5556 KB |
correct |
98 |
Correct |
45 ms |
5720 KB |
correct |
99 |
Correct |
42 ms |
5560 KB |
correct |
100 |
Correct |
25 ms |
4952 KB |
correct |
101 |
Correct |
10 ms |
4444 KB |
correct |
102 |
Correct |
91 ms |
6796 KB |
correct |
103 |
Correct |
91 ms |
6796 KB |
correct |
104 |
Correct |
96 ms |
6788 KB |
correct |
105 |
Correct |
104 ms |
6748 KB |
correct |