답안 #136564

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
136564 2019-07-25T14:11:57 Z baluteshih Simurgh (IOI17_simurgh) C++14
100 / 100
624 ms 6620 KB
#include "simurgh.h"
#include <bits/stdc++.h>
#define jizz ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define pb push_back
#define ET cout << "\n"
#define ALL(v) v.begin(),v.end()
#define MP make_pair
#define F first
#define S second
#define MEM(i,j) memset(i,j,sizeof i)
#define DB(a,s,e) {for(int i=s;i<e;++i) cout << a[i] << " ";ET;}
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

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()
{
	vector<int> r;
	for(auto i:s)
		r.pb(i);
	return count_common_roads(r);
}

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),MEM(type,-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:83: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:96:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<u.size();++i)
              ~^~~~~~~~~
simurgh.cpp:100: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 376 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 376 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB correct
2 Correct 2 ms 376 KB correct
3 Correct 2 ms 376 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 376 KB correct
14 Correct 5 ms 376 KB correct
15 Correct 3 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 380 KB correct
20 Correct 5 ms 376 KB correct
21 Correct 5 ms 376 KB correct
22 Correct 5 ms 376 KB correct
23 Correct 4 ms 504 KB correct
24 Correct 4 ms 376 KB correct
25 Correct 3 ms 376 KB correct
26 Correct 5 ms 376 KB correct
27 Correct 4 ms 376 KB correct
28 Correct 4 ms 376 KB correct
29 Correct 3 ms 376 KB correct
30 Correct 4 ms 376 KB correct
31 Correct 4 ms 376 KB correct
32 Correct 4 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 376 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 376 KB correct
14 Correct 5 ms 376 KB correct
15 Correct 3 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 380 KB correct
20 Correct 5 ms 376 KB correct
21 Correct 5 ms 376 KB correct
22 Correct 5 ms 376 KB correct
23 Correct 4 ms 504 KB correct
24 Correct 4 ms 376 KB correct
25 Correct 3 ms 376 KB correct
26 Correct 5 ms 376 KB correct
27 Correct 4 ms 376 KB correct
28 Correct 4 ms 376 KB correct
29 Correct 3 ms 376 KB correct
30 Correct 4 ms 376 KB correct
31 Correct 4 ms 376 KB correct
32 Correct 4 ms 376 KB correct
33 Correct 4 ms 376 KB correct
34 Correct 109 ms 1528 KB correct
35 Correct 105 ms 1528 KB correct
36 Correct 94 ms 1400 KB correct
37 Correct 37 ms 376 KB correct
38 Correct 111 ms 1628 KB correct
39 Correct 103 ms 1544 KB correct
40 Correct 93 ms 1272 KB correct
41 Correct 107 ms 1528 KB correct
42 Correct 96 ms 1528 KB correct
43 Correct 91 ms 1272 KB correct
44 Correct 99 ms 888 KB correct
45 Correct 88 ms 1016 KB correct
46 Correct 79 ms 892 KB correct
47 Correct 61 ms 632 KB correct
48 Correct 19 ms 376 KB correct
49 Correct 40 ms 632 KB correct
50 Correct 67 ms 632 KB correct
51 Correct 86 ms 1144 KB correct
52 Correct 85 ms 888 KB correct
53 Correct 77 ms 1016 KB correct
54 Correct 91 ms 1144 KB correct
55 Correct 87 ms 1016 KB correct
56 Correct 86 ms 888 KB correct
57 Correct 88 ms 1144 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB correct
2 Correct 2 ms 376 KB correct
3 Correct 356 ms 4188 KB correct
4 Correct 595 ms 5624 KB correct
5 Correct 589 ms 5624 KB correct
6 Correct 585 ms 6392 KB correct
7 Correct 586 ms 6620 KB correct
8 Correct 619 ms 6600 KB correct
9 Correct 600 ms 6596 KB correct
10 Correct 624 ms 6620 KB correct
11 Correct 621 ms 6520 KB correct
12 Correct 598 ms 6620 KB correct
13 Correct 8 ms 376 KB correct
14 Correct 581 ms 6520 KB correct
15 Correct 597 ms 6528 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB correct
2 Correct 2 ms 376 KB correct
3 Correct 2 ms 376 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 376 KB correct
14 Correct 5 ms 376 KB correct
15 Correct 3 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 380 KB correct
20 Correct 5 ms 376 KB correct
21 Correct 5 ms 376 KB correct
22 Correct 5 ms 376 KB correct
23 Correct 4 ms 504 KB correct
24 Correct 4 ms 376 KB correct
25 Correct 3 ms 376 KB correct
26 Correct 5 ms 376 KB correct
27 Correct 4 ms 376 KB correct
28 Correct 4 ms 376 KB correct
29 Correct 3 ms 376 KB correct
30 Correct 4 ms 376 KB correct
31 Correct 4 ms 376 KB correct
32 Correct 4 ms 376 KB correct
33 Correct 4 ms 376 KB correct
34 Correct 109 ms 1528 KB correct
35 Correct 105 ms 1528 KB correct
36 Correct 94 ms 1400 KB correct
37 Correct 37 ms 376 KB correct
38 Correct 111 ms 1628 KB correct
39 Correct 103 ms 1544 KB correct
40 Correct 93 ms 1272 KB correct
41 Correct 107 ms 1528 KB correct
42 Correct 96 ms 1528 KB correct
43 Correct 91 ms 1272 KB correct
44 Correct 99 ms 888 KB correct
45 Correct 88 ms 1016 KB correct
46 Correct 79 ms 892 KB correct
47 Correct 61 ms 632 KB correct
48 Correct 19 ms 376 KB correct
49 Correct 40 ms 632 KB correct
50 Correct 67 ms 632 KB correct
51 Correct 86 ms 1144 KB correct
52 Correct 85 ms 888 KB correct
53 Correct 77 ms 1016 KB correct
54 Correct 91 ms 1144 KB correct
55 Correct 87 ms 1016 KB correct
56 Correct 86 ms 888 KB correct
57 Correct 88 ms 1144 KB correct
58 Correct 2 ms 376 KB correct
59 Correct 2 ms 376 KB correct
60 Correct 356 ms 4188 KB correct
61 Correct 595 ms 5624 KB correct
62 Correct 589 ms 5624 KB correct
63 Correct 585 ms 6392 KB correct
64 Correct 586 ms 6620 KB correct
65 Correct 619 ms 6600 KB correct
66 Correct 600 ms 6596 KB correct
67 Correct 624 ms 6620 KB correct
68 Correct 621 ms 6520 KB correct
69 Correct 598 ms 6620 KB correct
70 Correct 8 ms 376 KB correct
71 Correct 581 ms 6520 KB correct
72 Correct 597 ms 6528 KB correct
73 Correct 2 ms 376 KB correct
74 Correct 600 ms 6532 KB correct
75 Correct 597 ms 6392 KB correct
76 Correct 315 ms 2808 KB correct
77 Correct 601 ms 6464 KB correct
78 Correct 593 ms 6520 KB correct
79 Correct 585 ms 6520 KB correct
80 Correct 579 ms 6392 KB correct
81 Correct 556 ms 5884 KB correct
82 Correct 586 ms 6392 KB correct
83 Correct 469 ms 3780 KB correct
84 Correct 487 ms 4600 KB correct
85 Correct 474 ms 4344 KB correct
86 Correct 423 ms 2808 KB correct
87 Correct 377 ms 2296 KB correct
88 Correct 350 ms 1656 KB correct
89 Correct 340 ms 1784 KB correct
90 Correct 326 ms 1532 KB correct
91 Correct 157 ms 504 KB correct
92 Correct 91 ms 376 KB correct
93 Correct 482 ms 4240 KB correct
94 Correct 444 ms 2852 KB correct
95 Correct 420 ms 2724 KB correct
96 Correct 334 ms 1656 KB correct
97 Correct 354 ms 1912 KB correct
98 Correct 374 ms 2296 KB correct
99 Correct 349 ms 1660 KB correct
100 Correct 237 ms 888 KB correct
101 Correct 92 ms 504 KB correct
102 Correct 480 ms 3576 KB correct
103 Correct 479 ms 3448 KB correct
104 Correct 477 ms 3576 KB correct
105 Correct 476 ms 3576 KB correct