답안 #704306

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
704306 2023-03-02T03:21:16 Z jamezzz Simurgh (IOI17_simurgh) C++17
100 / 100
525 ms 7492 KB
#include "simurgh.h"
#include <bits/stdc++.h>
using namespace std;
 
#define fi first
#define se second
#define pb push_back
typedef pair<int,int> ii;
 
#define maxn 505
 
struct UFDS{
	int par[maxn],rnk[maxn];
	void init(int n){
		for(int i=0;i<n;++i)par[i]=i,rnk[i]=1;
	}
	int fp(int i){
		return (par[i]==i)?i:par[i]=fp(par[i]);
	}
	void join(int x,int y){
		x=fp(x),y=fp(y);
		if(x==y)return;
		if(rnk[x]<rnk[y])par[x]=y;
		else par[y]=x;
		if(rnk[x]==rnk[y])++rnk[x];
	}
}ufds;
 
int n,m,good[maxn*maxn],edge[maxn][maxn];
int par[maxn],depth[maxn],deg[maxn],done[maxn];
vector<int> AL[maxn];
vector<ii> EL,back_edge,tree_edge;
 
void dfs(int u){
	int far=u;
	for(int v:AL[u]){
		if(depth[v]==0){
			par[v]=u;
			depth[v]=depth[u]+1;
			dfs(v);
			tree_edge.pb({u,v});
		}
		else if(v!=par[u]&&depth[far]>depth[v])far=v;
	}
	back_edge.pb({u,far});
}
 
int count(vector<int> e){
	int num=0;
	ufds.init(n);
	vector<int> ask;
	for(int i:e){
		int u,v;
		tie(u,v)=EL[i];
		ufds.join(u,v);
		ask.pb(edge[u][v]);
	}
	for(auto[u,v]:tree_edge){
		if(ufds.fp(u)!=ufds.fp(v)){
			ufds.join(u,v);
			ask.pb(edge[u][v]);
			num+=good[edge[u][v]];
		}
	}
	return count_common_roads(ask)-num;
}
 
vector<int> find_roads(int _n,vector<int> u,vector<int> v){
	n=_n;m=u.size();
	memset(edge,-1,sizeof edge);
	memset(good,-1,sizeof good);
	for(int i=0;i<m;++i){
		AL[u[i]].pb(v[i]);
		AL[v[i]].pb(u[i]);
		edge[u[i]][v[i]]=edge[v[i]][u[i]]=i;
		EL.pb({u[i],v[i]});
	}
	
	depth[0]=1;
	dfs(0);
	
	for(auto[u,v]:back_edge){
		if(u==v)continue;
		vector<ii> edges;
		edges.pb({u,v});
		ufds.init(n);
		while(u!=v){
			edges.pb({u,par[u]});
			ufds.join(u,par[u]);
			u=par[u];
		}
		
		vector<ii> others;
		for(auto[u,v]:EL){
			if(ufds.fp(u)!=ufds.fp(v)){
				others.pb({u,v});
				ufds.join(u,v);
			}
		}
		
		vector<ii> res;
		bool have_ref=false;
		int ref_type=-1,ref_val=-1;
		for(auto[u,v]:edges){
			int id=edge[u][v];
			if(good[id]!=-1&&have_ref)continue;
			if(good[id]!=-1)have_ref=true;
			vector<int> ask;
			for(auto[u,v]:others)ask.pb(edge[u][v]);
			for(auto[x,y]:edges){
				if(x!=u||y!=v)ask.pb(edge[x][y]);
			}
			res.pb({count_common_roads(ask),id});
			
			if(good[id]!=-1)ref_type=good[id],ref_val=res.back().fi;
		}
		if(res.size()==0)continue;
		sort(res.begin(),res.end());
		int small=res[0].fi,big=res.back().fi;
		for(auto[x,i]:res){
			if(x==ref_val)good[i]=ref_type;
			else if(x==big)good[i]=0;
			else if(x==small)good[i]=1;
		}
	}
	
	for(auto[u,v]:tree_edge){
		int id=edge[u][v];
		if(good[id]==-1)good[id]=1;
	}
	
	for(int u=0;u<n;++u){
		vector<int> tmp;
		for(int v:AL[u])tmp.pb(edge[u][v]);
		deg[u]=count(tmp);
	}
	
	while(true){
		int u=-1;
		for(int i=0;i<n;++i){
			if(done[i]==0&&deg[i]==1)u=i;
		}
		if(u==-1)break;
		vector<int> e;
		for(int v:AL[u]){
			if(!done[v])e.pb(edge[u][v]);
		}
		int lo=0,hi=e.size()-1,mid,res;
		while(lo<=hi){
			mid=(lo+hi)>>1;
			vector<int> tmp;
			for(int i=0;i<=mid;++i)tmp.pb(e[i]);
			if(count(tmp)==1)res=mid,hi=mid-1;
			else lo=mid+1;
		}
		good[e[res]]=1;
		int v=EL[e[res]].fi;
		if(u==v)v=EL[e[res]].se;
		--deg[v];
		done[u]=1;
	}
	
	vector<int> ans;
	for(int i=0;i<m;++i){
		if(good[i]==1)ans.pb(i);
	}
	assert(count_common_roads(ans)==n-1);
	return ans;
}

Compilation message

simurgh.cpp: In function 'std::vector<int> find_roads(int, std::vector<int>, std::vector<int>)':
simurgh.cpp:156:13: warning: 'res' may be used uninitialized in this function [-Wmaybe-uninitialized]
  156 |   good[e[res]]=1;
      |             ^
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2260 KB correct
2 Correct 1 ms 2260 KB correct
3 Correct 1 ms 2236 KB correct
4 Correct 1 ms 2260 KB correct
5 Correct 2 ms 2260 KB correct
6 Correct 2 ms 2260 KB correct
7 Correct 2 ms 2260 KB correct
8 Correct 1 ms 2244 KB correct
9 Correct 1 ms 2260 KB correct
10 Correct 2 ms 2260 KB correct
11 Correct 1 ms 2240 KB correct
12 Correct 1 ms 2260 KB correct
13 Correct 1 ms 2260 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2260 KB correct
2 Correct 1 ms 2260 KB correct
3 Correct 1 ms 2236 KB correct
4 Correct 1 ms 2260 KB correct
5 Correct 2 ms 2260 KB correct
6 Correct 2 ms 2260 KB correct
7 Correct 2 ms 2260 KB correct
8 Correct 1 ms 2244 KB correct
9 Correct 1 ms 2260 KB correct
10 Correct 2 ms 2260 KB correct
11 Correct 1 ms 2240 KB correct
12 Correct 1 ms 2260 KB correct
13 Correct 1 ms 2260 KB correct
14 Correct 3 ms 2372 KB correct
15 Correct 3 ms 2388 KB correct
16 Correct 3 ms 2388 KB correct
17 Correct 2 ms 2260 KB correct
18 Correct 2 ms 2236 KB correct
19 Correct 3 ms 2260 KB correct
20 Correct 3 ms 2260 KB correct
21 Correct 3 ms 2260 KB correct
22 Correct 2 ms 2260 KB correct
23 Correct 3 ms 2260 KB correct
24 Correct 2 ms 2260 KB correct
25 Correct 1 ms 2260 KB correct
26 Correct 2 ms 2260 KB correct
27 Correct 2 ms 2260 KB correct
28 Correct 2 ms 2260 KB correct
29 Correct 2 ms 2336 KB correct
30 Correct 2 ms 2236 KB correct
31 Correct 2 ms 2260 KB correct
32 Correct 2 ms 2260 KB correct
33 Correct 2 ms 2260 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2260 KB correct
2 Correct 1 ms 2260 KB correct
3 Correct 1 ms 2236 KB correct
4 Correct 1 ms 2260 KB correct
5 Correct 2 ms 2260 KB correct
6 Correct 2 ms 2260 KB correct
7 Correct 2 ms 2260 KB correct
8 Correct 1 ms 2244 KB correct
9 Correct 1 ms 2260 KB correct
10 Correct 2 ms 2260 KB correct
11 Correct 1 ms 2240 KB correct
12 Correct 1 ms 2260 KB correct
13 Correct 1 ms 2260 KB correct
14 Correct 3 ms 2372 KB correct
15 Correct 3 ms 2388 KB correct
16 Correct 3 ms 2388 KB correct
17 Correct 2 ms 2260 KB correct
18 Correct 2 ms 2236 KB correct
19 Correct 3 ms 2260 KB correct
20 Correct 3 ms 2260 KB correct
21 Correct 3 ms 2260 KB correct
22 Correct 2 ms 2260 KB correct
23 Correct 3 ms 2260 KB correct
24 Correct 2 ms 2260 KB correct
25 Correct 1 ms 2260 KB correct
26 Correct 2 ms 2260 KB correct
27 Correct 2 ms 2260 KB correct
28 Correct 2 ms 2260 KB correct
29 Correct 2 ms 2336 KB correct
30 Correct 2 ms 2236 KB correct
31 Correct 2 ms 2260 KB correct
32 Correct 2 ms 2260 KB correct
33 Correct 2 ms 2260 KB correct
34 Correct 72 ms 3516 KB correct
35 Correct 68 ms 3492 KB correct
36 Correct 54 ms 3284 KB correct
37 Correct 14 ms 2408 KB correct
38 Correct 68 ms 3516 KB correct
39 Correct 65 ms 3412 KB correct
40 Correct 54 ms 3284 KB correct
41 Correct 68 ms 3536 KB correct
42 Correct 68 ms 3412 KB correct
43 Correct 43 ms 3028 KB correct
44 Correct 35 ms 2900 KB correct
45 Correct 40 ms 2900 KB correct
46 Correct 36 ms 2856 KB correct
47 Correct 24 ms 2504 KB correct
48 Correct 8 ms 2260 KB correct
49 Correct 13 ms 2420 KB correct
50 Correct 23 ms 2516 KB correct
51 Correct 42 ms 2900 KB correct
52 Correct 38 ms 2804 KB correct
53 Correct 34 ms 2772 KB correct
54 Correct 43 ms 3028 KB correct
55 Correct 41 ms 2900 KB correct
56 Correct 40 ms 2968 KB correct
57 Correct 40 ms 2900 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2260 KB correct
2 Correct 1 ms 2260 KB correct
3 Correct 263 ms 6032 KB correct
4 Correct 491 ms 7420 KB correct
5 Correct 501 ms 7424 KB correct
6 Correct 497 ms 7436 KB correct
7 Correct 488 ms 7424 KB correct
8 Correct 498 ms 7416 KB correct
9 Correct 495 ms 7492 KB correct
10 Correct 477 ms 7428 KB correct
11 Correct 525 ms 7420 KB correct
12 Correct 486 ms 7436 KB correct
13 Correct 1 ms 2236 KB correct
14 Correct 469 ms 7408 KB correct
15 Correct 502 ms 7424 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2260 KB correct
2 Correct 1 ms 2260 KB correct
3 Correct 1 ms 2236 KB correct
4 Correct 1 ms 2260 KB correct
5 Correct 2 ms 2260 KB correct
6 Correct 2 ms 2260 KB correct
7 Correct 2 ms 2260 KB correct
8 Correct 1 ms 2244 KB correct
9 Correct 1 ms 2260 KB correct
10 Correct 2 ms 2260 KB correct
11 Correct 1 ms 2240 KB correct
12 Correct 1 ms 2260 KB correct
13 Correct 1 ms 2260 KB correct
14 Correct 3 ms 2372 KB correct
15 Correct 3 ms 2388 KB correct
16 Correct 3 ms 2388 KB correct
17 Correct 2 ms 2260 KB correct
18 Correct 2 ms 2236 KB correct
19 Correct 3 ms 2260 KB correct
20 Correct 3 ms 2260 KB correct
21 Correct 3 ms 2260 KB correct
22 Correct 2 ms 2260 KB correct
23 Correct 3 ms 2260 KB correct
24 Correct 2 ms 2260 KB correct
25 Correct 1 ms 2260 KB correct
26 Correct 2 ms 2260 KB correct
27 Correct 2 ms 2260 KB correct
28 Correct 2 ms 2260 KB correct
29 Correct 2 ms 2336 KB correct
30 Correct 2 ms 2236 KB correct
31 Correct 2 ms 2260 KB correct
32 Correct 2 ms 2260 KB correct
33 Correct 2 ms 2260 KB correct
34 Correct 72 ms 3516 KB correct
35 Correct 68 ms 3492 KB correct
36 Correct 54 ms 3284 KB correct
37 Correct 14 ms 2408 KB correct
38 Correct 68 ms 3516 KB correct
39 Correct 65 ms 3412 KB correct
40 Correct 54 ms 3284 KB correct
41 Correct 68 ms 3536 KB correct
42 Correct 68 ms 3412 KB correct
43 Correct 43 ms 3028 KB correct
44 Correct 35 ms 2900 KB correct
45 Correct 40 ms 2900 KB correct
46 Correct 36 ms 2856 KB correct
47 Correct 24 ms 2504 KB correct
48 Correct 8 ms 2260 KB correct
49 Correct 13 ms 2420 KB correct
50 Correct 23 ms 2516 KB correct
51 Correct 42 ms 2900 KB correct
52 Correct 38 ms 2804 KB correct
53 Correct 34 ms 2772 KB correct
54 Correct 43 ms 3028 KB correct
55 Correct 41 ms 2900 KB correct
56 Correct 40 ms 2968 KB correct
57 Correct 40 ms 2900 KB correct
58 Correct 1 ms 2260 KB correct
59 Correct 1 ms 2260 KB correct
60 Correct 263 ms 6032 KB correct
61 Correct 491 ms 7420 KB correct
62 Correct 501 ms 7424 KB correct
63 Correct 497 ms 7436 KB correct
64 Correct 488 ms 7424 KB correct
65 Correct 498 ms 7416 KB correct
66 Correct 495 ms 7492 KB correct
67 Correct 477 ms 7428 KB correct
68 Correct 525 ms 7420 KB correct
69 Correct 486 ms 7436 KB correct
70 Correct 1 ms 2236 KB correct
71 Correct 469 ms 7408 KB correct
72 Correct 502 ms 7424 KB correct
73 Correct 2 ms 2260 KB correct
74 Correct 492 ms 7420 KB correct
75 Correct 469 ms 7272 KB correct
76 Correct 177 ms 4304 KB correct
77 Correct 475 ms 7424 KB correct
78 Correct 509 ms 7412 KB correct
79 Correct 492 ms 7412 KB correct
80 Correct 478 ms 7288 KB correct
81 Correct 418 ms 6732 KB correct
82 Correct 462 ms 7296 KB correct
83 Correct 285 ms 4940 KB correct
84 Correct 292 ms 5904 KB correct
85 Correct 280 ms 5364 KB correct
86 Correct 205 ms 4432 KB correct
87 Correct 161 ms 3896 KB correct
88 Correct 147 ms 3412 KB correct
89 Correct 136 ms 3284 KB correct
90 Correct 121 ms 3284 KB correct
91 Correct 50 ms 2388 KB correct
92 Correct 29 ms 2388 KB correct
93 Correct 272 ms 5324 KB correct
94 Correct 204 ms 4432 KB correct
95 Correct 202 ms 4304 KB correct
96 Correct 128 ms 3272 KB correct
97 Correct 141 ms 3436 KB correct
98 Correct 172 ms 3784 KB correct
99 Correct 144 ms 3400 KB correct
100 Correct 69 ms 2644 KB correct
101 Correct 30 ms 2388 KB correct
102 Correct 271 ms 4940 KB correct
103 Correct 263 ms 4960 KB correct
104 Correct 270 ms 4812 KB correct
105 Correct 262 ms 4912 KB correct