답안 #13481

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
13481 2015-02-21T21:32:08 Z ainta 경주 (Race) (IOI11_race) C++
100 / 100
680 ms 33632 KB
#include "race.h"
#include<stdio.h>
#include<algorithm>
#include<vector>
using namespace std;
vector<int>E[201000], Len[201000];
int n, LL, Res, D[201000], Dep[201000], C[201000], Q[201000], par[201000];
bool chk[201000];

int BFS(int a){
	int h = 0, t = 0, x, i;
	Q[++t] = a;
	par[t] = D[t] = Dep[t] = 0, C[t] = 1;
	while (h < t){
		x = Q[++h];
		for (i = 0; i < E[x].size(); i++){
			if (!chk[E[x][i]] && Q[par[h]] != E[x][i]){
				Q[++t] = E[x][i];
				par[t] = h, D[t] = D[h] + Len[x][i], Dep[t] = Dep[h] + 1, C[t] = 1;
			}
		}
	}
	for (i = t; i>1; i--)C[par[i]] += C[i];
	return t;
}

int get_mid(int SZ){
	int i, M = SZ + 1, x;
	for (i = 1; i <= SZ; i++){
		if (M > C[i] && C[i] > SZ / 2)M = C[i], x = Q[i];
	}
	return x;
}

int w[1010000], Li[201000];
void Do(int a){
	int SZ = BFS(a);
	if (SZ == 1)return;
	int m = get_mid(SZ), i, j, cnt = 0;
	chk[m] = true;
	for (i = 0; i < E[m].size(); i++){
		if (!chk[E[m][i]])Do(E[m][i]);
	}
	for (i = 0; i < E[m].size(); i++){
		if (chk[E[m][i]])continue;
		SZ = BFS(E[m][i]);
		for (j = 1; j <= SZ; j++){
			D[j]+= Len[m][i], Dep[j]++;
			if (D[j]>LL)continue;
			if (D[j] == LL)Res = min(Res, Dep[j]);
			else if (w[LL - D[j]])Res = min(Res, Dep[j] + w[LL - D[j]]);
		}
		for (j = 1; j <= SZ; j++){
			if (D[j] <= LL && (w[D[j]] == 0 || w[D[j]] > Dep[j]))w[D[j]] = Dep[j], Li[cnt++] = D[j];
		}
	}
	chk[m] = false;
	for (i = 0; i < cnt; i++)w[Li[i]] = 0;
}

int best_path(int N, int K, int H[][2], int L[])
{
	int i;
	n = N, LL = K;
	for (i = 0; i < n - 1; i++){
		E[H[i][0]].push_back(H[i][1]);
		E[H[i][1]].push_back(H[i][0]);
		Len[H[i][0]].push_back(L[i]);
		Len[H[i][1]].push_back(L[i]);
	}
	Res = n + 1;
	Do(0);
	return Res == n + 1 ? -1 : Res;
}

Compilation message

race.cpp: In function 'int BFS(int)':
race.cpp:16:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (i = 0; i < E[x].size(); i++){
               ~~^~~~~~~~~~~~~
race.cpp: In function 'void Do(int)':
race.cpp:41:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (i = 0; i < E[m].size(); i++){
              ~~^~~~~~~~~~~~~
race.cpp:44:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (i = 0; i < E[m].size(); i++){
              ~~^~~~~~~~~~~~~
race.cpp: In function 'int get_mid(int)':
race.cpp:32:9: warning: 'x' may be used uninitialized in this function [-Wmaybe-uninitialized]
  return x;
         ^
race.cpp: In function 'void Do(int)':
race.cpp:28:21: warning: 'x' may be used uninitialized in this function [-Wmaybe-uninitialized]
  int i, M = SZ + 1, x;
                     ^
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 9816 KB Output is correct
2 Correct 10 ms 9960 KB Output is correct
3 Correct 9 ms 9960 KB Output is correct
4 Correct 8 ms 9960 KB Output is correct
5 Correct 9 ms 9960 KB Output is correct
6 Correct 9 ms 9960 KB Output is correct
7 Correct 9 ms 9960 KB Output is correct
8 Correct 9 ms 9960 KB Output is correct
9 Correct 9 ms 9960 KB Output is correct
10 Correct 9 ms 9960 KB Output is correct
11 Correct 10 ms 9980 KB Output is correct
12 Correct 9 ms 10040 KB Output is correct
13 Correct 9 ms 10152 KB Output is correct
14 Correct 9 ms 10152 KB Output is correct
15 Correct 9 ms 10152 KB Output is correct
16 Correct 9 ms 10152 KB Output is correct
17 Correct 9 ms 10152 KB Output is correct
18 Correct 10 ms 10196 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 9816 KB Output is correct
2 Correct 10 ms 9960 KB Output is correct
3 Correct 9 ms 9960 KB Output is correct
4 Correct 8 ms 9960 KB Output is correct
5 Correct 9 ms 9960 KB Output is correct
6 Correct 9 ms 9960 KB Output is correct
7 Correct 9 ms 9960 KB Output is correct
8 Correct 9 ms 9960 KB Output is correct
9 Correct 9 ms 9960 KB Output is correct
10 Correct 9 ms 9960 KB Output is correct
11 Correct 10 ms 9980 KB Output is correct
12 Correct 9 ms 10040 KB Output is correct
13 Correct 9 ms 10152 KB Output is correct
14 Correct 9 ms 10152 KB Output is correct
15 Correct 9 ms 10152 KB Output is correct
16 Correct 9 ms 10152 KB Output is correct
17 Correct 9 ms 10152 KB Output is correct
18 Correct 10 ms 10196 KB Output is correct
19 Correct 9 ms 10196 KB Output is correct
20 Correct 9 ms 10224 KB Output is correct
21 Correct 10 ms 10224 KB Output is correct
22 Correct 13 ms 12628 KB Output is correct
23 Correct 13 ms 12628 KB Output is correct
24 Correct 14 ms 12976 KB Output is correct
25 Correct 12 ms 12976 KB Output is correct
26 Correct 11 ms 12976 KB Output is correct
27 Correct 10 ms 12976 KB Output is correct
28 Correct 11 ms 12976 KB Output is correct
29 Correct 12 ms 12976 KB Output is correct
30 Correct 13 ms 12976 KB Output is correct
31 Correct 13 ms 12976 KB Output is correct
32 Correct 12 ms 12976 KB Output is correct
33 Correct 13 ms 12976 KB Output is correct
34 Correct 13 ms 12976 KB Output is correct
35 Correct 14 ms 13056 KB Output is correct
36 Correct 14 ms 13436 KB Output is correct
37 Correct 13 ms 13436 KB Output is correct
38 Correct 11 ms 13436 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 9816 KB Output is correct
2 Correct 10 ms 9960 KB Output is correct
3 Correct 9 ms 9960 KB Output is correct
4 Correct 8 ms 9960 KB Output is correct
5 Correct 9 ms 9960 KB Output is correct
6 Correct 9 ms 9960 KB Output is correct
7 Correct 9 ms 9960 KB Output is correct
8 Correct 9 ms 9960 KB Output is correct
9 Correct 9 ms 9960 KB Output is correct
10 Correct 9 ms 9960 KB Output is correct
11 Correct 10 ms 9980 KB Output is correct
12 Correct 9 ms 10040 KB Output is correct
13 Correct 9 ms 10152 KB Output is correct
14 Correct 9 ms 10152 KB Output is correct
15 Correct 9 ms 10152 KB Output is correct
16 Correct 9 ms 10152 KB Output is correct
17 Correct 9 ms 10152 KB Output is correct
18 Correct 10 ms 10196 KB Output is correct
19 Correct 176 ms 19684 KB Output is correct
20 Correct 171 ms 19728 KB Output is correct
21 Correct 157 ms 19728 KB Output is correct
22 Correct 190 ms 19752 KB Output is correct
23 Correct 186 ms 19752 KB Output is correct
24 Correct 149 ms 20016 KB Output is correct
25 Correct 283 ms 20016 KB Output is correct
26 Correct 120 ms 20016 KB Output is correct
27 Correct 326 ms 30332 KB Output is correct
28 Correct 632 ms 30332 KB Output is correct
29 Correct 661 ms 30332 KB Output is correct
30 Correct 390 ms 30332 KB Output is correct
31 Correct 353 ms 30332 KB Output is correct
32 Correct 472 ms 30332 KB Output is correct
33 Correct 403 ms 30332 KB Output is correct
34 Correct 384 ms 30332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 9816 KB Output is correct
2 Correct 10 ms 9960 KB Output is correct
3 Correct 9 ms 9960 KB Output is correct
4 Correct 8 ms 9960 KB Output is correct
5 Correct 9 ms 9960 KB Output is correct
6 Correct 9 ms 9960 KB Output is correct
7 Correct 9 ms 9960 KB Output is correct
8 Correct 9 ms 9960 KB Output is correct
9 Correct 9 ms 9960 KB Output is correct
10 Correct 9 ms 9960 KB Output is correct
11 Correct 10 ms 9980 KB Output is correct
12 Correct 9 ms 10040 KB Output is correct
13 Correct 9 ms 10152 KB Output is correct
14 Correct 9 ms 10152 KB Output is correct
15 Correct 9 ms 10152 KB Output is correct
16 Correct 9 ms 10152 KB Output is correct
17 Correct 9 ms 10152 KB Output is correct
18 Correct 10 ms 10196 KB Output is correct
19 Correct 9 ms 10196 KB Output is correct
20 Correct 9 ms 10224 KB Output is correct
21 Correct 10 ms 10224 KB Output is correct
22 Correct 13 ms 12628 KB Output is correct
23 Correct 13 ms 12628 KB Output is correct
24 Correct 14 ms 12976 KB Output is correct
25 Correct 12 ms 12976 KB Output is correct
26 Correct 11 ms 12976 KB Output is correct
27 Correct 10 ms 12976 KB Output is correct
28 Correct 11 ms 12976 KB Output is correct
29 Correct 12 ms 12976 KB Output is correct
30 Correct 13 ms 12976 KB Output is correct
31 Correct 13 ms 12976 KB Output is correct
32 Correct 12 ms 12976 KB Output is correct
33 Correct 13 ms 12976 KB Output is correct
34 Correct 13 ms 12976 KB Output is correct
35 Correct 14 ms 13056 KB Output is correct
36 Correct 14 ms 13436 KB Output is correct
37 Correct 13 ms 13436 KB Output is correct
38 Correct 11 ms 13436 KB Output is correct
39 Correct 176 ms 19684 KB Output is correct
40 Correct 171 ms 19728 KB Output is correct
41 Correct 157 ms 19728 KB Output is correct
42 Correct 190 ms 19752 KB Output is correct
43 Correct 186 ms 19752 KB Output is correct
44 Correct 149 ms 20016 KB Output is correct
45 Correct 283 ms 20016 KB Output is correct
46 Correct 120 ms 20016 KB Output is correct
47 Correct 326 ms 30332 KB Output is correct
48 Correct 632 ms 30332 KB Output is correct
49 Correct 661 ms 30332 KB Output is correct
50 Correct 390 ms 30332 KB Output is correct
51 Correct 353 ms 30332 KB Output is correct
52 Correct 472 ms 30332 KB Output is correct
53 Correct 403 ms 30332 KB Output is correct
54 Correct 384 ms 30332 KB Output is correct
55 Correct 21 ms 30332 KB Output is correct
56 Correct 23 ms 30332 KB Output is correct
57 Correct 128 ms 30332 KB Output is correct
58 Correct 55 ms 30332 KB Output is correct
59 Correct 144 ms 30332 KB Output is correct
60 Correct 660 ms 32380 KB Output is correct
61 Correct 332 ms 32380 KB Output is correct
62 Correct 370 ms 32380 KB Output is correct
63 Correct 429 ms 32380 KB Output is correct
64 Correct 506 ms 32380 KB Output is correct
65 Correct 431 ms 32380 KB Output is correct
66 Correct 680 ms 33020 KB Output is correct
67 Correct 230 ms 33020 KB Output is correct
68 Correct 494 ms 33160 KB Output is correct
69 Correct 439 ms 33632 KB Output is correct
70 Correct 436 ms 33632 KB Output is correct