답안 #456207

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
456207 2021-08-06T09:06:10 Z sean617 경주 (Race) (IOI11_race) C++
100 / 100
822 ms 44920 KB
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#define SZ 200005
#define A 1000005
#include "race.h"

using namespace std;
int n, k, mn, M = 1e9, s[A], sz[SZ];
bool v2[SZ];
vector<int> a[SZ], b[SZ];
queue<int> qu;



int gs(int p, int q) {
	int i, t;
	sz[p] = 1;
	for (i = 0; i < a[p].size(); i++) {
		t = a[p][i];
		if (t == q || v2[t]) continue;
		sz[p] += gs(t, p);
	}
	return sz[p];
}

int gc(int p, int q, int nn) {
	int i, t;
	for (i = 0; i < a[p].size(); i++) {
		t = a[p][i];
		if (t != q && !v2[t] && sz[t] *2 > nn) return gc(t, p, nn);
	}
	return p;
}

void g(int p, int q, int w, int cnt) {
	int i, t;
	if (w > k) return;
	mn = min(mn, cnt + s[k - w]);
	for (i = 0; i < a[p].size(); i++) {
		t = a[p][i];
		if (t == q || v2[t]) continue;
		g(t, p, w + b[p][i], cnt + 1);
	}
}

void up(int p, int q, int w, int cnt) {
	int i, t;
	if (w > k) return;
	if (s[w] > cnt) {
		s[w] = cnt;
		qu.push(w);
	}
	for (i = 0; i < a[p].size(); i++) {
		t = a[p][i];
		if (t == q || v2[t]) continue;
		up(t, p, w + b[p][i], cnt + 1);
	}
}

void f(int p) {
	int i, t, cen;
	gs(p, -1);
	cen = gc(p, -1, sz[p]);
	v2[cen] = 1;
	while (!qu.empty()) {
		t = qu.front(); qu.pop();
		s[t] = M;
	}
	s[0] = 0;
	for (i = 0; i < a[cen].size(); i++) {
		t = a[cen][i];
		if (v2[t]) continue;
		g(t, -1, b[cen][i], 1);
		up(t, -1, b[cen][i], 1);
	}
	for (i = 0; i < a[cen].size(); i++) {
		t = a[cen][i];
		if (v2[t]) continue;
		f(t);
	}
}

int best_path(int NN, int KK, int H[][2], int L[])
{
	int i, t1, t2, t3;
	n = NN;
	k = KK;
	for (i = 0; i < n - 1; i++) {
		t1 = H[i][0];
		t2 = H[i][1];
		t3 = L[i];
		a[t1].push_back(t2);
		b[t1].push_back(t3);
		a[t2].push_back(t1);
		b[t2].push_back(t3);
	}
	mn = M;
	for (i = 0; i <= k; i++) s[i] = M;
	f(0);
	if (mn == M) mn = -1;

  return mn;
}

Compilation message

race.cpp: In function 'int gs(int, int)':
race.cpp:21:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   21 |  for (i = 0; i < a[p].size(); i++) {
      |              ~~^~~~~~~~~~~~~
race.cpp: In function 'int gc(int, int, int)':
race.cpp:31:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   31 |  for (i = 0; i < a[p].size(); i++) {
      |              ~~^~~~~~~~~~~~~
race.cpp: In function 'void g(int, int, int, int)':
race.cpp:42:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   42 |  for (i = 0; i < a[p].size(); i++) {
      |              ~~^~~~~~~~~~~~~
race.cpp: In function 'void up(int, int, int, int)':
race.cpp:56:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   56 |  for (i = 0; i < a[p].size(); i++) {
      |              ~~^~~~~~~~~~~~~
race.cpp: In function 'void f(int)':
race.cpp:73:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   73 |  for (i = 0; i < a[cen].size(); i++) {
      |              ~~^~~~~~~~~~~~~~~
race.cpp:79:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   79 |  for (i = 0; i < a[cen].size(); i++) {
      |              ~~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9676 KB Output is correct
2 Correct 6 ms 9676 KB Output is correct
3 Correct 6 ms 9732 KB Output is correct
4 Correct 6 ms 9676 KB Output is correct
5 Correct 6 ms 9688 KB Output is correct
6 Correct 6 ms 9676 KB Output is correct
7 Correct 6 ms 9676 KB Output is correct
8 Correct 6 ms 9676 KB Output is correct
9 Correct 6 ms 9676 KB Output is correct
10 Correct 6 ms 9708 KB Output is correct
11 Correct 6 ms 9620 KB Output is correct
12 Correct 7 ms 9676 KB Output is correct
13 Correct 7 ms 9804 KB Output is correct
14 Correct 6 ms 9708 KB Output is correct
15 Correct 6 ms 9736 KB Output is correct
16 Correct 6 ms 9676 KB Output is correct
17 Correct 6 ms 9708 KB Output is correct
18 Correct 6 ms 9676 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9676 KB Output is correct
2 Correct 6 ms 9676 KB Output is correct
3 Correct 6 ms 9732 KB Output is correct
4 Correct 6 ms 9676 KB Output is correct
5 Correct 6 ms 9688 KB Output is correct
6 Correct 6 ms 9676 KB Output is correct
7 Correct 6 ms 9676 KB Output is correct
8 Correct 6 ms 9676 KB Output is correct
9 Correct 6 ms 9676 KB Output is correct
10 Correct 6 ms 9708 KB Output is correct
11 Correct 6 ms 9620 KB Output is correct
12 Correct 7 ms 9676 KB Output is correct
13 Correct 7 ms 9804 KB Output is correct
14 Correct 6 ms 9708 KB Output is correct
15 Correct 6 ms 9736 KB Output is correct
16 Correct 6 ms 9676 KB Output is correct
17 Correct 6 ms 9708 KB Output is correct
18 Correct 6 ms 9676 KB Output is correct
19 Correct 5 ms 9676 KB Output is correct
20 Correct 6 ms 9676 KB Output is correct
21 Correct 7 ms 9804 KB Output is correct
22 Correct 10 ms 13304 KB Output is correct
23 Correct 10 ms 12748 KB Output is correct
24 Correct 15 ms 13132 KB Output is correct
25 Correct 9 ms 13004 KB Output is correct
26 Correct 9 ms 11140 KB Output is correct
27 Correct 10 ms 12920 KB Output is correct
28 Correct 7 ms 10572 KB Output is correct
29 Correct 7 ms 11012 KB Output is correct
30 Correct 7 ms 11252 KB Output is correct
31 Correct 9 ms 12348 KB Output is correct
32 Correct 8 ms 12492 KB Output is correct
33 Correct 11 ms 12876 KB Output is correct
34 Correct 8 ms 12056 KB Output is correct
35 Correct 9 ms 12916 KB Output is correct
36 Correct 9 ms 13388 KB Output is correct
37 Correct 8 ms 12892 KB Output is correct
38 Correct 10 ms 11760 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9676 KB Output is correct
2 Correct 6 ms 9676 KB Output is correct
3 Correct 6 ms 9732 KB Output is correct
4 Correct 6 ms 9676 KB Output is correct
5 Correct 6 ms 9688 KB Output is correct
6 Correct 6 ms 9676 KB Output is correct
7 Correct 6 ms 9676 KB Output is correct
8 Correct 6 ms 9676 KB Output is correct
9 Correct 6 ms 9676 KB Output is correct
10 Correct 6 ms 9708 KB Output is correct
11 Correct 6 ms 9620 KB Output is correct
12 Correct 7 ms 9676 KB Output is correct
13 Correct 7 ms 9804 KB Output is correct
14 Correct 6 ms 9708 KB Output is correct
15 Correct 6 ms 9736 KB Output is correct
16 Correct 6 ms 9676 KB Output is correct
17 Correct 6 ms 9708 KB Output is correct
18 Correct 6 ms 9676 KB Output is correct
19 Correct 189 ms 19012 KB Output is correct
20 Correct 196 ms 19044 KB Output is correct
21 Correct 209 ms 19132 KB Output is correct
22 Correct 215 ms 19316 KB Output is correct
23 Correct 140 ms 19068 KB Output is correct
24 Correct 98 ms 19524 KB Output is correct
25 Correct 179 ms 22276 KB Output is correct
26 Correct 119 ms 25236 KB Output is correct
27 Correct 321 ms 29684 KB Output is correct
28 Correct 354 ms 41044 KB Output is correct
29 Correct 345 ms 40208 KB Output is correct
30 Correct 304 ms 29684 KB Output is correct
31 Correct 297 ms 29676 KB Output is correct
32 Correct 351 ms 29776 KB Output is correct
33 Correct 313 ms 28544 KB Output is correct
34 Correct 291 ms 29420 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9676 KB Output is correct
2 Correct 6 ms 9676 KB Output is correct
3 Correct 6 ms 9732 KB Output is correct
4 Correct 6 ms 9676 KB Output is correct
5 Correct 6 ms 9688 KB Output is correct
6 Correct 6 ms 9676 KB Output is correct
7 Correct 6 ms 9676 KB Output is correct
8 Correct 6 ms 9676 KB Output is correct
9 Correct 6 ms 9676 KB Output is correct
10 Correct 6 ms 9708 KB Output is correct
11 Correct 6 ms 9620 KB Output is correct
12 Correct 7 ms 9676 KB Output is correct
13 Correct 7 ms 9804 KB Output is correct
14 Correct 6 ms 9708 KB Output is correct
15 Correct 6 ms 9736 KB Output is correct
16 Correct 6 ms 9676 KB Output is correct
17 Correct 6 ms 9708 KB Output is correct
18 Correct 6 ms 9676 KB Output is correct
19 Correct 5 ms 9676 KB Output is correct
20 Correct 6 ms 9676 KB Output is correct
21 Correct 7 ms 9804 KB Output is correct
22 Correct 10 ms 13304 KB Output is correct
23 Correct 10 ms 12748 KB Output is correct
24 Correct 15 ms 13132 KB Output is correct
25 Correct 9 ms 13004 KB Output is correct
26 Correct 9 ms 11140 KB Output is correct
27 Correct 10 ms 12920 KB Output is correct
28 Correct 7 ms 10572 KB Output is correct
29 Correct 7 ms 11012 KB Output is correct
30 Correct 7 ms 11252 KB Output is correct
31 Correct 9 ms 12348 KB Output is correct
32 Correct 8 ms 12492 KB Output is correct
33 Correct 11 ms 12876 KB Output is correct
34 Correct 8 ms 12056 KB Output is correct
35 Correct 9 ms 12916 KB Output is correct
36 Correct 9 ms 13388 KB Output is correct
37 Correct 8 ms 12892 KB Output is correct
38 Correct 10 ms 11760 KB Output is correct
39 Correct 189 ms 19012 KB Output is correct
40 Correct 196 ms 19044 KB Output is correct
41 Correct 209 ms 19132 KB Output is correct
42 Correct 215 ms 19316 KB Output is correct
43 Correct 140 ms 19068 KB Output is correct
44 Correct 98 ms 19524 KB Output is correct
45 Correct 179 ms 22276 KB Output is correct
46 Correct 119 ms 25236 KB Output is correct
47 Correct 321 ms 29684 KB Output is correct
48 Correct 354 ms 41044 KB Output is correct
49 Correct 345 ms 40208 KB Output is correct
50 Correct 304 ms 29684 KB Output is correct
51 Correct 297 ms 29676 KB Output is correct
52 Correct 351 ms 29776 KB Output is correct
53 Correct 313 ms 28544 KB Output is correct
54 Correct 291 ms 29420 KB Output is correct
55 Correct 16 ms 10700 KB Output is correct
56 Correct 17 ms 10640 KB Output is correct
57 Correct 104 ms 18888 KB Output is correct
58 Correct 53 ms 19412 KB Output is correct
59 Correct 128 ms 26320 KB Output is correct
60 Correct 608 ms 44920 KB Output is correct
61 Correct 324 ms 29896 KB Output is correct
62 Correct 317 ms 33696 KB Output is correct
63 Correct 442 ms 33712 KB Output is correct
64 Correct 822 ms 31840 KB Output is correct
65 Correct 417 ms 29764 KB Output is correct
66 Correct 522 ms 42228 KB Output is correct
67 Correct 211 ms 34344 KB Output is correct
68 Correct 411 ms 34536 KB Output is correct
69 Correct 442 ms 34736 KB Output is correct
70 Correct 414 ms 33780 KB Output is correct