답안 #136567

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
136567 2019-07-25T14:17:25 Z baluteshih Simurgh (IOI17_simurgh) C++14
100 / 100
613 ms 5624 KB
#include "simurgh.h"
#include <bits/stdc++.h>
#define pb push_back
#define MP make_pair
#define F first
#define S second
using namespace std;
typedef pair<int,int> pii;

vector<int> U,V;
vector<pii> G[505];
pii pa[505];
queue<int> q;
int type[505],dep[505],deg[505],boss[505],n;
set<int> s;
bitset<505> vis,ed;
bitset<250000> used;

int ccr()
{
	return count_common_roads(vector<int>(s.begin(),s.end()));
}

void dfs(int u,int f,int num,int d)
{
	pa[u]=MP(f,num),dep[u]=d,vis[u]=1;
	if(~num) used[num]=1,s.insert(num);
	for(pii i:G[u])
		if(!vis[i.F])
			dfs(i.F,u,i.S,d+1);
}

int finds(int x)
{
	if(x==boss[x]) return x;
	return boss[x]=finds(boss[x]);
}

int Union(int a,int b)
{
	a=finds(a),b=finds(b);
	if(a==b) return 0;
	return boss[a]=b,1; 
}

int ccr_forest(vector<int> f)
{
	int cnt=0;
	for(int i=0;i<n;++i)
		boss[i]=i;
	set<int> tmp;
	for(int i:f)
		tmp.insert(i),Union(U[i],V[i]);
	for(int i=1;i<n;++i)
		if(Union(i,pa[i].F))
			cnt+=type[i],tmp.insert(pa[i].S);
	tmp.swap(s),cnt=ccr()-cnt,tmp.swap(s);
	return cnt;
}

int leaf_finding(int u)
{
	vector<int> candi;
	for(pii i:G[u])
		if(!ed[i.F])
			candi.pb(i.S);
	int l=0,r=candi.size()-1;
	while(l<r)
	{
		vector<int> v;
		int m=l+r>>1;
		for(int i=l;i<=m;++i)
			v.pb(candi[i]);
		if(ccr_forest(v)>=1) r=m;
		else l=m+1; 
	}
	return candi[l];
}

vector<int> find_roads(int N, vector<int> u, vector<int> v)
{
	n=N,U=u,V=v;
	vector<int> ans;
	for(int i=0;i<u.size();++i)
		G[u[i]].pb(MP(v[i],i)),G[v[i]].pb(MP(u[i],i));
	dfs(0,-1,-1,0),fill(type,type+N,-1);
	int cur=ccr();
	for(int i=0;i<u.size();++i)
	{
		if(used[i]) continue;
		int cnt=0,sz=0,tp=-1,a=u[i],b=v[i];
		pii sw;
		while(a!=b)
		{
			if(dep[a]<dep[b]) swap(a,b);
			if(~type[a]) sw=MP(pa[a].S,type[a]);
			cnt+=!~type[a],++sz,a=pa[a].F;
		}
		if(!cnt) continue;
		a=u[i],b=v[i];
		if(cnt==sz)
		{
			vector<int> v;
			while(a!=b)
			{
				if(dep[a]<dep[b]) swap(a,b);
				s.erase(pa[a].S),s.insert(i);
				int tmp=ccr();
				if(tmp==cur) v.pb(a);
				else
				{
					if(tmp>cur) tp=1,type[a]=0;
					else tp=0,type[a]=1;
					for(int j:v)
						type[j]=tp;
					s.erase(i),s.insert(pa[a].S),a=pa[a].F;
					break;
				}
				s.erase(i),s.insert(pa[a].S),a=pa[a].F;
			}
			if(a==b&&!~tp)
				for(int j:v)
					type[j]=0;
		}
		else
			s.erase(sw.F),s.insert(i),tp=(sw.S==0)^(ccr()==cur),s.erase(i),s.insert(sw.F);
		while(a!=b)
		{
			if(dep[a]<dep[b]) swap(a,b);
			s.erase(pa[a].S),s.insert(i);
			if(!~type[a]) 
				type[a]=tp^(ccr()!=cur);
			s.erase(i),s.insert(pa[a].S),a=pa[a].F;
		}
	}
	for(int i=1;i<n;++i)
		if(!~type[i]) type[i]=1;
	for(int i=0;i<n;++i)
	{
		vector<int> v;
		for(pii j:G[i])
			v.pb(j.S);
		deg[i]=ccr_forest(v);
		if(deg[i]==1)
			q.push(i);
	}
	while(q.size()>1)
	{
		int u=q.front();
		q.pop(),ed[u]=1,ans.pb(leaf_finding(u)),u^=U[ans.back()]^V[ans.back()];
		if(--deg[u]==1)
			q.push(u);
	}
	return ans;
}

Compilation message

simurgh.cpp: In function 'int leaf_finding(int)':
simurgh.cpp:71:10: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   int m=l+r>>1;
         ~^~
simurgh.cpp: In function 'std::vector<int> find_roads(int, std::vector<int>, std::vector<int>)':
simurgh.cpp:84:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<u.size();++i)
              ~^~~~~~~~~
simurgh.cpp:88:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<u.size();++i)
              ~^~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB correct
2 Correct 2 ms 376 KB correct
3 Correct 2 ms 428 KB correct
4 Correct 2 ms 376 KB correct
5 Correct 2 ms 376 KB correct
6 Correct 2 ms 376 KB correct
7 Correct 2 ms 376 KB correct
8 Correct 2 ms 376 KB correct
9 Correct 2 ms 376 KB correct
10 Correct 2 ms 376 KB correct
11 Correct 2 ms 376 KB correct
12 Correct 2 ms 376 KB correct
13 Correct 2 ms 380 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB correct
2 Correct 2 ms 376 KB correct
3 Correct 2 ms 428 KB correct
4 Correct 2 ms 376 KB correct
5 Correct 2 ms 376 KB correct
6 Correct 2 ms 376 KB correct
7 Correct 2 ms 376 KB correct
8 Correct 2 ms 376 KB correct
9 Correct 2 ms 376 KB correct
10 Correct 2 ms 376 KB correct
11 Correct 2 ms 376 KB correct
12 Correct 2 ms 376 KB correct
13 Correct 2 ms 380 KB correct
14 Correct 5 ms 504 KB correct
15 Correct 5 ms 376 KB correct
16 Correct 5 ms 376 KB correct
17 Correct 5 ms 376 KB correct
18 Correct 4 ms 376 KB correct
19 Correct 5 ms 376 KB correct
20 Correct 5 ms 376 KB correct
21 Correct 5 ms 376 KB correct
22 Correct 4 ms 380 KB correct
23 Correct 4 ms 376 KB correct
24 Correct 4 ms 376 KB correct
25 Correct 3 ms 376 KB correct
26 Correct 4 ms 376 KB correct
27 Correct 4 ms 376 KB correct
28 Correct 4 ms 376 KB correct
29 Correct 4 ms 376 KB correct
30 Correct 5 ms 376 KB correct
31 Correct 5 ms 376 KB correct
32 Correct 5 ms 376 KB correct
33 Correct 4 ms 376 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB correct
2 Correct 2 ms 376 KB correct
3 Correct 2 ms 428 KB correct
4 Correct 2 ms 376 KB correct
5 Correct 2 ms 376 KB correct
6 Correct 2 ms 376 KB correct
7 Correct 2 ms 376 KB correct
8 Correct 2 ms 376 KB correct
9 Correct 2 ms 376 KB correct
10 Correct 2 ms 376 KB correct
11 Correct 2 ms 376 KB correct
12 Correct 2 ms 376 KB correct
13 Correct 2 ms 380 KB correct
14 Correct 5 ms 504 KB correct
15 Correct 5 ms 376 KB correct
16 Correct 5 ms 376 KB correct
17 Correct 5 ms 376 KB correct
18 Correct 4 ms 376 KB correct
19 Correct 5 ms 376 KB correct
20 Correct 5 ms 376 KB correct
21 Correct 5 ms 376 KB correct
22 Correct 4 ms 380 KB correct
23 Correct 4 ms 376 KB correct
24 Correct 4 ms 376 KB correct
25 Correct 3 ms 376 KB correct
26 Correct 4 ms 376 KB correct
27 Correct 4 ms 376 KB correct
28 Correct 4 ms 376 KB correct
29 Correct 4 ms 376 KB correct
30 Correct 5 ms 376 KB correct
31 Correct 5 ms 376 KB correct
32 Correct 5 ms 376 KB correct
33 Correct 4 ms 376 KB correct
34 Correct 110 ms 1608 KB correct
35 Correct 112 ms 1592 KB correct
36 Correct 96 ms 1400 KB correct
37 Correct 38 ms 376 KB correct
38 Correct 111 ms 1528 KB correct
39 Correct 103 ms 1632 KB correct
40 Correct 91 ms 1272 KB correct
41 Correct 108 ms 1528 KB correct
42 Correct 106 ms 1500 KB correct
43 Correct 89 ms 1144 KB correct
44 Correct 84 ms 888 KB correct
45 Correct 88 ms 1144 KB correct
46 Correct 80 ms 888 KB correct
47 Correct 63 ms 648 KB correct
48 Correct 22 ms 376 KB correct
49 Correct 41 ms 504 KB correct
50 Correct 65 ms 632 KB correct
51 Correct 86 ms 1016 KB correct
52 Correct 84 ms 888 KB correct
53 Correct 80 ms 1028 KB correct
54 Correct 91 ms 1272 KB correct
55 Correct 89 ms 888 KB correct
56 Correct 87 ms 888 KB correct
57 Correct 89 ms 1016 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB correct
2 Correct 2 ms 376 KB correct
3 Correct 354 ms 4064 KB correct
4 Correct 595 ms 5596 KB correct
5 Correct 598 ms 5608 KB correct
6 Correct 593 ms 5500 KB correct
7 Correct 595 ms 5596 KB correct
8 Correct 596 ms 5496 KB correct
9 Correct 600 ms 5624 KB correct
10 Correct 603 ms 5624 KB correct
11 Correct 606 ms 5624 KB correct
12 Correct 600 ms 5624 KB correct
13 Correct 2 ms 376 KB correct
14 Correct 589 ms 5624 KB correct
15 Correct 598 ms 5496 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB correct
2 Correct 2 ms 376 KB correct
3 Correct 2 ms 428 KB correct
4 Correct 2 ms 376 KB correct
5 Correct 2 ms 376 KB correct
6 Correct 2 ms 376 KB correct
7 Correct 2 ms 376 KB correct
8 Correct 2 ms 376 KB correct
9 Correct 2 ms 376 KB correct
10 Correct 2 ms 376 KB correct
11 Correct 2 ms 376 KB correct
12 Correct 2 ms 376 KB correct
13 Correct 2 ms 380 KB correct
14 Correct 5 ms 504 KB correct
15 Correct 5 ms 376 KB correct
16 Correct 5 ms 376 KB correct
17 Correct 5 ms 376 KB correct
18 Correct 4 ms 376 KB correct
19 Correct 5 ms 376 KB correct
20 Correct 5 ms 376 KB correct
21 Correct 5 ms 376 KB correct
22 Correct 4 ms 380 KB correct
23 Correct 4 ms 376 KB correct
24 Correct 4 ms 376 KB correct
25 Correct 3 ms 376 KB correct
26 Correct 4 ms 376 KB correct
27 Correct 4 ms 376 KB correct
28 Correct 4 ms 376 KB correct
29 Correct 4 ms 376 KB correct
30 Correct 5 ms 376 KB correct
31 Correct 5 ms 376 KB correct
32 Correct 5 ms 376 KB correct
33 Correct 4 ms 376 KB correct
34 Correct 110 ms 1608 KB correct
35 Correct 112 ms 1592 KB correct
36 Correct 96 ms 1400 KB correct
37 Correct 38 ms 376 KB correct
38 Correct 111 ms 1528 KB correct
39 Correct 103 ms 1632 KB correct
40 Correct 91 ms 1272 KB correct
41 Correct 108 ms 1528 KB correct
42 Correct 106 ms 1500 KB correct
43 Correct 89 ms 1144 KB correct
44 Correct 84 ms 888 KB correct
45 Correct 88 ms 1144 KB correct
46 Correct 80 ms 888 KB correct
47 Correct 63 ms 648 KB correct
48 Correct 22 ms 376 KB correct
49 Correct 41 ms 504 KB correct
50 Correct 65 ms 632 KB correct
51 Correct 86 ms 1016 KB correct
52 Correct 84 ms 888 KB correct
53 Correct 80 ms 1028 KB correct
54 Correct 91 ms 1272 KB correct
55 Correct 89 ms 888 KB correct
56 Correct 87 ms 888 KB correct
57 Correct 89 ms 1016 KB correct
58 Correct 2 ms 376 KB correct
59 Correct 2 ms 376 KB correct
60 Correct 354 ms 4064 KB correct
61 Correct 595 ms 5596 KB correct
62 Correct 598 ms 5608 KB correct
63 Correct 593 ms 5500 KB correct
64 Correct 595 ms 5596 KB correct
65 Correct 596 ms 5496 KB correct
66 Correct 600 ms 5624 KB correct
67 Correct 603 ms 5624 KB correct
68 Correct 606 ms 5624 KB correct
69 Correct 600 ms 5624 KB correct
70 Correct 2 ms 376 KB correct
71 Correct 589 ms 5624 KB correct
72 Correct 598 ms 5496 KB correct
73 Correct 2 ms 376 KB correct
74 Correct 613 ms 5496 KB correct
75 Correct 600 ms 5496 KB correct
76 Correct 310 ms 2372 KB correct
77 Correct 599 ms 5496 KB correct
78 Correct 604 ms 5496 KB correct
79 Correct 602 ms 5568 KB correct
80 Correct 593 ms 5496 KB correct
81 Correct 587 ms 5116 KB correct
82 Correct 593 ms 5496 KB correct
83 Correct 479 ms 3312 KB correct
84 Correct 506 ms 3932 KB correct
85 Correct 487 ms 3832 KB correct
86 Correct 438 ms 2552 KB correct
87 Correct 396 ms 2140 KB correct
88 Correct 366 ms 1784 KB correct
89 Correct 355 ms 1656 KB correct
90 Correct 347 ms 1528 KB correct
91 Correct 168 ms 568 KB correct
92 Correct 97 ms 504 KB correct
93 Correct 491 ms 3800 KB correct
94 Correct 436 ms 2552 KB correct
95 Correct 433 ms 2428 KB correct
96 Correct 349 ms 1400 KB correct
97 Correct 368 ms 1684 KB correct
98 Correct 398 ms 2168 KB correct
99 Correct 364 ms 1656 KB correct
100 Correct 250 ms 892 KB correct
101 Correct 99 ms 504 KB correct
102 Correct 503 ms 2940 KB correct
103 Correct 495 ms 3196 KB correct
104 Correct 496 ms 3064 KB correct
105 Correct 497 ms 3064 KB correct