답안 #16584

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
16584 2015-08-28T12:41:44 Z comet 경주 (Race) (IOI11_race) C++
100 / 100
923 ms 29424 KB
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <cstring>
using namespace std;
#define MAX_N 200000
#define MAX_K 1000000

struct edge{int u,c;};

typedef vector<edge> vec;
typedef vector<vec> mat;
mat path;

int n,comet,SubSz[MAX_N],MaxSz[MAX_N];
bool killed[MAX_N];
bool chk[MAX_N];
vector<int> s;

int dfs(int v){
	chk[v]=1;
	s.push_back(v);
	int ret=0;
	SubSz[v]=MaxSz[v]=1;
	for(int i=0;i<path[v].size();i++){
		int u=path[v][i].u;
		if(killed[u]||chk[u])continue;
		ret=dfs(u);
		SubSz[v]+=ret;
		MaxSz[v]=max(MaxSz[v],ret+1);
	}
	chk[v]=0;
	return SubSz[v];
}

int centroid(int v){
	//printf("centroid %d\n",v);
	s.clear();
	dfs(v);
	int x,ret=v,Max=0;
	for(int i=0;i<s.size();i++){
		x=s[i];
		//printf("%d : %d %d\n",x,MaxSz[x],SubSz[x]);
		if(Max<min(SubSz[v]-MaxSz[x],MaxSz[x])){
			Max=min(SubSz[v]-MaxSz[x],MaxSz[x]);
			ret=x;
		}
	}
	return ret;
}

int stamp[MAX_K+1],a[MAX_K+1];
struct state{int v,c,len;};

int f(int v){
	//printf("%d \n",v);
	//for(int i=0;i<n;i++)printf("%d,%d ",killed[i],chk[i]);
	//puts("");
	int root=centroid(v);
	killed[root]=1;

	//printf("root=%d\n",root);

	int ret=1e9;

	for(int i=0;i<path[root].size();i++){
		int u=path[root][i].u;
		if(killed[u])continue;

		//printf("u=%d\n",u);

		queue<state> Q;
		Q.push(state{u,path[root][i].c,1});
		chk[u]=1;

		while(!Q.empty()){
			state h=Q.front();
			Q.pop();
			if(h.c==comet)ret=min(ret,h.len);
			if(h.c>=comet)continue;
			if(stamp[comet-h.c]==root)ret=min(ret,a[comet-h.c]+h.len);
			for(int j=0;j<path[h.v].size();j++){
				int uu=path[h.v][j].u;
				if(killed[uu]||chk[uu])continue;
				chk[uu]=1;
				Q.push(state{uu,h.c+path[h.v][j].c,h.len+1});
			}
		}
		Q.push(state{u,path[root][i].c,1});
		chk[u]=0;
		while(!Q.empty()){
			state h=Q.front();
			Q.pop();
			if(h.c>=comet)continue;
			if(stamp[h.c]==root)
				a[h.c]=min(a[h.c],h.len);
			else
				stamp[h.c]=root,a[h.c]=h.len;
			for(int j=0;j<path[h.v].size();j++){
				int uu=path[h.v][j].u;
				if(killed[uu]||chk[uu]==0)continue;
				chk[uu]=0;
				Q.push(state{uu,h.c+path[h.v][j].c,h.len+1});
			}
		}
	}

	for(int i=0;i<path[root].size();i++){
		int u=path[root][i].u;
		if(killed[u])continue;
		ret=min(ret,f(u));
	}

	killed[root]=0;
	return ret;
}

int best_path(int N, int K, int H[][2], int L[]){
	memset(stamp,-1,sizeof(stamp));
	n=N;
	comet=K;
	path.assign(n,vec());
	for(int i=0;i<n;i++){
		path[H[i][0]].push_back(edge{H[i][1],L[i]});
		path[H[i][1]].push_back(edge{H[i][0],L[i]});
	}
	int ret=f(0);
	return ret==1e9?-1:ret;
}






















Compilation message

race.cpp: In function 'int dfs(int)':
race.cpp:27:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<path[v].size();i++){
              ~^~~~~~~~~~~~~~~
race.cpp: In function 'int centroid(int)':
race.cpp:43:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<s.size();i++){
              ~^~~~~~~~~
race.cpp: In function 'int f(int)':
race.cpp:68:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<path[root].size();i++){
              ~^~~~~~~~~~~~~~~~~~
race.cpp:84:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    for(int j=0;j<path[h.v].size();j++){
                ~^~~~~~~~~~~~~~~~~
race.cpp:101:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    for(int j=0;j<path[h.v].size();j++){
                ~^~~~~~~~~~~~~~~~~
race.cpp:110:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<path[root].size();i++){
              ~^~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 4216 KB Output is correct
2 Correct 5 ms 4324 KB Output is correct
3 Correct 5 ms 4400 KB Output is correct
4 Correct 5 ms 4400 KB Output is correct
5 Correct 5 ms 4400 KB Output is correct
6 Correct 5 ms 4436 KB Output is correct
7 Correct 5 ms 4512 KB Output is correct
8 Correct 5 ms 4540 KB Output is correct
9 Correct 5 ms 4540 KB Output is correct
10 Correct 6 ms 4540 KB Output is correct
11 Correct 5 ms 4540 KB Output is correct
12 Correct 5 ms 4540 KB Output is correct
13 Correct 7 ms 4540 KB Output is correct
14 Correct 5 ms 4540 KB Output is correct
15 Correct 5 ms 4540 KB Output is correct
16 Correct 6 ms 4540 KB Output is correct
17 Correct 6 ms 4540 KB Output is correct
18 Correct 5 ms 4540 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 4216 KB Output is correct
2 Correct 5 ms 4324 KB Output is correct
3 Correct 5 ms 4400 KB Output is correct
4 Correct 5 ms 4400 KB Output is correct
5 Correct 5 ms 4400 KB Output is correct
6 Correct 5 ms 4436 KB Output is correct
7 Correct 5 ms 4512 KB Output is correct
8 Correct 5 ms 4540 KB Output is correct
9 Correct 5 ms 4540 KB Output is correct
10 Correct 6 ms 4540 KB Output is correct
11 Correct 5 ms 4540 KB Output is correct
12 Correct 5 ms 4540 KB Output is correct
13 Correct 7 ms 4540 KB Output is correct
14 Correct 5 ms 4540 KB Output is correct
15 Correct 5 ms 4540 KB Output is correct
16 Correct 6 ms 4540 KB Output is correct
17 Correct 6 ms 4540 KB Output is correct
18 Correct 5 ms 4540 KB Output is correct
19 Correct 4 ms 4540 KB Output is correct
20 Correct 5 ms 4540 KB Output is correct
21 Correct 7 ms 4604 KB Output is correct
22 Correct 8 ms 7036 KB Output is correct
23 Correct 8 ms 7036 KB Output is correct
24 Correct 8 ms 7420 KB Output is correct
25 Correct 8 ms 7420 KB Output is correct
26 Correct 7 ms 7420 KB Output is correct
27 Correct 6 ms 7420 KB Output is correct
28 Correct 7 ms 7420 KB Output is correct
29 Correct 7 ms 7420 KB Output is correct
30 Correct 7 ms 7420 KB Output is correct
31 Correct 9 ms 7420 KB Output is correct
32 Correct 8 ms 7420 KB Output is correct
33 Correct 8 ms 7420 KB Output is correct
34 Correct 8 ms 7420 KB Output is correct
35 Correct 9 ms 7548 KB Output is correct
36 Correct 9 ms 7932 KB Output is correct
37 Correct 10 ms 7932 KB Output is correct
38 Correct 7 ms 7932 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 4216 KB Output is correct
2 Correct 5 ms 4324 KB Output is correct
3 Correct 5 ms 4400 KB Output is correct
4 Correct 5 ms 4400 KB Output is correct
5 Correct 5 ms 4400 KB Output is correct
6 Correct 5 ms 4436 KB Output is correct
7 Correct 5 ms 4512 KB Output is correct
8 Correct 5 ms 4540 KB Output is correct
9 Correct 5 ms 4540 KB Output is correct
10 Correct 6 ms 4540 KB Output is correct
11 Correct 5 ms 4540 KB Output is correct
12 Correct 5 ms 4540 KB Output is correct
13 Correct 7 ms 4540 KB Output is correct
14 Correct 5 ms 4540 KB Output is correct
15 Correct 5 ms 4540 KB Output is correct
16 Correct 6 ms 4540 KB Output is correct
17 Correct 6 ms 4540 KB Output is correct
18 Correct 5 ms 4540 KB Output is correct
19 Correct 283 ms 13220 KB Output is correct
20 Correct 258 ms 13308 KB Output is correct
21 Correct 284 ms 13308 KB Output is correct
22 Correct 242 ms 13308 KB Output is correct
23 Correct 185 ms 13436 KB Output is correct
24 Correct 117 ms 13436 KB Output is correct
25 Correct 294 ms 14464 KB Output is correct
26 Correct 176 ms 15732 KB Output is correct
27 Correct 319 ms 21928 KB Output is correct
28 Correct 469 ms 26864 KB Output is correct
29 Correct 378 ms 26864 KB Output is correct
30 Correct 307 ms 26864 KB Output is correct
31 Correct 312 ms 26864 KB Output is correct
32 Correct 399 ms 26864 KB Output is correct
33 Correct 437 ms 26864 KB Output is correct
34 Correct 457 ms 26864 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 4216 KB Output is correct
2 Correct 5 ms 4324 KB Output is correct
3 Correct 5 ms 4400 KB Output is correct
4 Correct 5 ms 4400 KB Output is correct
5 Correct 5 ms 4400 KB Output is correct
6 Correct 5 ms 4436 KB Output is correct
7 Correct 5 ms 4512 KB Output is correct
8 Correct 5 ms 4540 KB Output is correct
9 Correct 5 ms 4540 KB Output is correct
10 Correct 6 ms 4540 KB Output is correct
11 Correct 5 ms 4540 KB Output is correct
12 Correct 5 ms 4540 KB Output is correct
13 Correct 7 ms 4540 KB Output is correct
14 Correct 5 ms 4540 KB Output is correct
15 Correct 5 ms 4540 KB Output is correct
16 Correct 6 ms 4540 KB Output is correct
17 Correct 6 ms 4540 KB Output is correct
18 Correct 5 ms 4540 KB Output is correct
19 Correct 4 ms 4540 KB Output is correct
20 Correct 5 ms 4540 KB Output is correct
21 Correct 7 ms 4604 KB Output is correct
22 Correct 8 ms 7036 KB Output is correct
23 Correct 8 ms 7036 KB Output is correct
24 Correct 8 ms 7420 KB Output is correct
25 Correct 8 ms 7420 KB Output is correct
26 Correct 7 ms 7420 KB Output is correct
27 Correct 6 ms 7420 KB Output is correct
28 Correct 7 ms 7420 KB Output is correct
29 Correct 7 ms 7420 KB Output is correct
30 Correct 7 ms 7420 KB Output is correct
31 Correct 9 ms 7420 KB Output is correct
32 Correct 8 ms 7420 KB Output is correct
33 Correct 8 ms 7420 KB Output is correct
34 Correct 8 ms 7420 KB Output is correct
35 Correct 9 ms 7548 KB Output is correct
36 Correct 9 ms 7932 KB Output is correct
37 Correct 10 ms 7932 KB Output is correct
38 Correct 7 ms 7932 KB Output is correct
39 Correct 283 ms 13220 KB Output is correct
40 Correct 258 ms 13308 KB Output is correct
41 Correct 284 ms 13308 KB Output is correct
42 Correct 242 ms 13308 KB Output is correct
43 Correct 185 ms 13436 KB Output is correct
44 Correct 117 ms 13436 KB Output is correct
45 Correct 294 ms 14464 KB Output is correct
46 Correct 176 ms 15732 KB Output is correct
47 Correct 319 ms 21928 KB Output is correct
48 Correct 469 ms 26864 KB Output is correct
49 Correct 378 ms 26864 KB Output is correct
50 Correct 307 ms 26864 KB Output is correct
51 Correct 312 ms 26864 KB Output is correct
52 Correct 399 ms 26864 KB Output is correct
53 Correct 437 ms 26864 KB Output is correct
54 Correct 457 ms 26864 KB Output is correct
55 Correct 20 ms 26864 KB Output is correct
56 Correct 22 ms 26864 KB Output is correct
57 Correct 145 ms 26864 KB Output is correct
58 Correct 70 ms 26864 KB Output is correct
59 Correct 178 ms 26864 KB Output is correct
60 Correct 878 ms 29044 KB Output is correct
61 Correct 278 ms 29044 KB Output is correct
62 Correct 364 ms 29044 KB Output is correct
63 Correct 422 ms 29044 KB Output is correct
64 Correct 827 ms 29044 KB Output is correct
65 Correct 397 ms 29044 KB Output is correct
66 Correct 923 ms 29424 KB Output is correct
67 Correct 234 ms 29424 KB Output is correct
68 Correct 587 ms 29424 KB Output is correct
69 Correct 587 ms 29424 KB Output is correct
70 Correct 433 ms 29424 KB Output is correct