답안 #136558

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
136558 2019-07-25T14:01:58 Z baluteshih Simurgh (IOI17_simurgh) C++14
51 / 100
594 ms 5496 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,ti;
set<int> s;
bitset<505> vis,ed;
bitset<250000> used;

int ccr()
{
	++ti;
	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;
			}
		}
		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;
		}
	}
	assert(ti<=12000);
	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:84: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:97:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<u.size();++i)
              ~^~~~~~~~~
simurgh.cpp:101: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 5 ms 376 KB correct
16 Correct 5 ms 380 KB correct
17 Correct 6 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 376 KB correct
23 Correct 4 ms 380 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 5 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 5 ms 376 KB correct
16 Correct 5 ms 380 KB correct
17 Correct 6 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 376 KB correct
23 Correct 4 ms 380 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 5 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 112 ms 1532 KB correct
35 Correct 109 ms 1528 KB correct
36 Correct 95 ms 1400 KB correct
37 Correct 37 ms 496 KB correct
38 Correct 112 ms 1528 KB correct
39 Correct 102 ms 1528 KB correct
40 Correct 91 ms 1400 KB correct
41 Correct 117 ms 1528 KB correct
42 Correct 106 ms 1656 KB correct
43 Correct 87 ms 1144 KB correct
44 Correct 84 ms 988 KB correct
45 Correct 85 ms 996 KB correct
46 Correct 78 ms 888 KB correct
47 Correct 61 ms 760 KB correct
48 Correct 22 ms 376 KB correct
49 Correct 40 ms 504 KB correct
50 Correct 60 ms 632 KB correct
51 Correct 86 ms 1008 KB correct
52 Correct 84 ms 940 KB correct
53 Correct 78 ms 860 KB correct
54 Correct 88 ms 1144 KB correct
55 Correct 86 ms 888 KB correct
56 Correct 86 ms 1016 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 437 ms 4072 KB correct
4 Correct 594 ms 5468 KB correct
5 Incorrect 220 ms 5496 KB WA in grader: NO
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 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 5 ms 376 KB correct
16 Correct 5 ms 380 KB correct
17 Correct 6 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 376 KB correct
23 Correct 4 ms 380 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 5 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 112 ms 1532 KB correct
35 Correct 109 ms 1528 KB correct
36 Correct 95 ms 1400 KB correct
37 Correct 37 ms 496 KB correct
38 Correct 112 ms 1528 KB correct
39 Correct 102 ms 1528 KB correct
40 Correct 91 ms 1400 KB correct
41 Correct 117 ms 1528 KB correct
42 Correct 106 ms 1656 KB correct
43 Correct 87 ms 1144 KB correct
44 Correct 84 ms 988 KB correct
45 Correct 85 ms 996 KB correct
46 Correct 78 ms 888 KB correct
47 Correct 61 ms 760 KB correct
48 Correct 22 ms 376 KB correct
49 Correct 40 ms 504 KB correct
50 Correct 60 ms 632 KB correct
51 Correct 86 ms 1008 KB correct
52 Correct 84 ms 940 KB correct
53 Correct 78 ms 860 KB correct
54 Correct 88 ms 1144 KB correct
55 Correct 86 ms 888 KB correct
56 Correct 86 ms 1016 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 437 ms 4072 KB correct
61 Correct 594 ms 5468 KB correct
62 Incorrect 220 ms 5496 KB WA in grader: NO
63 Halted 0 ms 0 KB -