답안 #199209

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
199209 2020-01-30T08:58:36 Z dennisstar 경주 (Race) (IOI11_race) C++17
100 / 100
1172 ms 35576 KB
#include <bits/stdc++.h>
#define fi first
#define se second
#define em emplace
#define eb emplace_back
#define sq(X) ((X)*(X))
#define all(V) (V).begin(), (V).end()
#define chk_init memset(chk, 0, sizeof(chk))
#define unq(V) (V).erase(unique(all(V)), (V).end())
using namespace std;
typedef long long ll;
typedef vector<ll> vlm;
typedef vector<int> vim;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
const int INF=(1<<30);
#include "race.h"

int N, K;
vector<pii> adj[200010];
int fin[200010], sz[200010], im, ans;
set<pii> S;
pii vis[200010]; int tp;

void subsz(int now, int par) {
	sz[now]=1;
	for (auto &i:adj[now]) if (i.fi!=par&&!fin[i.fi]) {
		subsz(i.fi, now);
		sz[now]+=sz[i.fi];
	}
}

int get_cent(int now, int par) {
	for (auto &i:adj[now]) if (i.fi!=par&&!fin[i.fi]&&sz[i.fi]>=im/2) return get_cent(i.fi, now);
	return now;
}

void visit(int now, int par, int d, int dep) {
	vis[tp++]=pii(d, dep);
	for (auto &i:adj[now]) if (i.fi!=par&&!fin[i.fi]) visit(i.fi, now, d+i.se, dep+1);
}

void dfs(int now) {
	subsz(now, 0); im=sz[now];
	int cent=get_cent(now, 0); fin[cent]=1;
	S.clear(); set<pii>::iterator it;
	for (auto &i:adj[cent]) if (!fin[i.fi]) {
		tp=0; visit(i.fi, cent, i.se, 1);
		for (int j=0; j<tp; j++) {
			it=S.lower_bound(pii(K-vis[j].fi, 0));
			if (it==S.end()||it->fi!=K-vis[j].fi) continue;
			ans=min(ans, it->se+vis[j].se);
		}
		for (int j=0; j<tp; j++) S.insert(vis[j]);
	}
	it=S.lower_bound(pii(K, 0));
	if (!(it==S.end()||it->fi!=K)) ans=min(ans, it->se);
	for (auto &i:adj[cent]) if (!fin[i.fi]) dfs(i.fi);
}

int best_path(int N_, int K_, int H[][2], int L[]) {
	N=N_; K=K_;
	for (int i=0; i<N-1; i++) adj[H[i][0]+1].eb(H[i][1]+1, L[i]), adj[H[i][1]+1].eb(H[i][0]+1, L[i]);
	ans=INF; dfs(1);
	return ans==INF?-1:ans;
}

# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 4984 KB Output is correct
2 Correct 8 ms 4984 KB Output is correct
3 Correct 8 ms 4984 KB Output is correct
4 Correct 8 ms 4984 KB Output is correct
5 Correct 8 ms 4984 KB Output is correct
6 Correct 8 ms 5112 KB Output is correct
7 Correct 8 ms 5112 KB Output is correct
8 Correct 8 ms 4984 KB Output is correct
9 Correct 8 ms 4984 KB Output is correct
10 Correct 8 ms 4984 KB Output is correct
11 Correct 8 ms 5116 KB Output is correct
12 Correct 8 ms 4984 KB Output is correct
13 Correct 8 ms 4984 KB Output is correct
14 Correct 9 ms 5112 KB Output is correct
15 Correct 8 ms 4984 KB Output is correct
16 Correct 8 ms 4984 KB Output is correct
17 Correct 8 ms 4984 KB Output is correct
18 Correct 8 ms 4984 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 4984 KB Output is correct
2 Correct 8 ms 4984 KB Output is correct
3 Correct 8 ms 4984 KB Output is correct
4 Correct 8 ms 4984 KB Output is correct
5 Correct 8 ms 4984 KB Output is correct
6 Correct 8 ms 5112 KB Output is correct
7 Correct 8 ms 5112 KB Output is correct
8 Correct 8 ms 4984 KB Output is correct
9 Correct 8 ms 4984 KB Output is correct
10 Correct 8 ms 4984 KB Output is correct
11 Correct 8 ms 5116 KB Output is correct
12 Correct 8 ms 4984 KB Output is correct
13 Correct 8 ms 4984 KB Output is correct
14 Correct 9 ms 5112 KB Output is correct
15 Correct 8 ms 4984 KB Output is correct
16 Correct 8 ms 4984 KB Output is correct
17 Correct 8 ms 4984 KB Output is correct
18 Correct 8 ms 4984 KB Output is correct
19 Correct 8 ms 5112 KB Output is correct
20 Correct 8 ms 5112 KB Output is correct
21 Correct 10 ms 5112 KB Output is correct
22 Correct 10 ms 5112 KB Output is correct
23 Correct 10 ms 5112 KB Output is correct
24 Correct 9 ms 5112 KB Output is correct
25 Correct 9 ms 5112 KB Output is correct
26 Correct 9 ms 5112 KB Output is correct
27 Correct 9 ms 5112 KB Output is correct
28 Correct 10 ms 5240 KB Output is correct
29 Correct 9 ms 5112 KB Output is correct
30 Correct 11 ms 5240 KB Output is correct
31 Correct 10 ms 5112 KB Output is correct
32 Correct 9 ms 5112 KB Output is correct
33 Correct 10 ms 5112 KB Output is correct
34 Correct 10 ms 5112 KB Output is correct
35 Correct 10 ms 5112 KB Output is correct
36 Correct 9 ms 5112 KB Output is correct
37 Correct 9 ms 5112 KB Output is correct
38 Correct 9 ms 5112 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 4984 KB Output is correct
2 Correct 8 ms 4984 KB Output is correct
3 Correct 8 ms 4984 KB Output is correct
4 Correct 8 ms 4984 KB Output is correct
5 Correct 8 ms 4984 KB Output is correct
6 Correct 8 ms 5112 KB Output is correct
7 Correct 8 ms 5112 KB Output is correct
8 Correct 8 ms 4984 KB Output is correct
9 Correct 8 ms 4984 KB Output is correct
10 Correct 8 ms 4984 KB Output is correct
11 Correct 8 ms 5116 KB Output is correct
12 Correct 8 ms 4984 KB Output is correct
13 Correct 8 ms 4984 KB Output is correct
14 Correct 9 ms 5112 KB Output is correct
15 Correct 8 ms 4984 KB Output is correct
16 Correct 8 ms 4984 KB Output is correct
17 Correct 8 ms 4984 KB Output is correct
18 Correct 8 ms 4984 KB Output is correct
19 Correct 295 ms 11360 KB Output is correct
20 Correct 282 ms 11896 KB Output is correct
21 Correct 312 ms 12104 KB Output is correct
22 Correct 244 ms 11896 KB Output is correct
23 Correct 249 ms 12280 KB Output is correct
24 Correct 142 ms 11848 KB Output is correct
25 Correct 364 ms 19064 KB Output is correct
26 Correct 249 ms 20472 KB Output is correct
27 Correct 374 ms 19832 KB Output is correct
28 Correct 1000 ms 35576 KB Output is correct
29 Correct 999 ms 34808 KB Output is correct
30 Correct 379 ms 19832 KB Output is correct
31 Correct 369 ms 19700 KB Output is correct
32 Correct 473 ms 19832 KB Output is correct
33 Correct 539 ms 17016 KB Output is correct
34 Correct 928 ms 26136 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 4984 KB Output is correct
2 Correct 8 ms 4984 KB Output is correct
3 Correct 8 ms 4984 KB Output is correct
4 Correct 8 ms 4984 KB Output is correct
5 Correct 8 ms 4984 KB Output is correct
6 Correct 8 ms 5112 KB Output is correct
7 Correct 8 ms 5112 KB Output is correct
8 Correct 8 ms 4984 KB Output is correct
9 Correct 8 ms 4984 KB Output is correct
10 Correct 8 ms 4984 KB Output is correct
11 Correct 8 ms 5116 KB Output is correct
12 Correct 8 ms 4984 KB Output is correct
13 Correct 8 ms 4984 KB Output is correct
14 Correct 9 ms 5112 KB Output is correct
15 Correct 8 ms 4984 KB Output is correct
16 Correct 8 ms 4984 KB Output is correct
17 Correct 8 ms 4984 KB Output is correct
18 Correct 8 ms 4984 KB Output is correct
19 Correct 8 ms 5112 KB Output is correct
20 Correct 8 ms 5112 KB Output is correct
21 Correct 10 ms 5112 KB Output is correct
22 Correct 10 ms 5112 KB Output is correct
23 Correct 10 ms 5112 KB Output is correct
24 Correct 9 ms 5112 KB Output is correct
25 Correct 9 ms 5112 KB Output is correct
26 Correct 9 ms 5112 KB Output is correct
27 Correct 9 ms 5112 KB Output is correct
28 Correct 10 ms 5240 KB Output is correct
29 Correct 9 ms 5112 KB Output is correct
30 Correct 11 ms 5240 KB Output is correct
31 Correct 10 ms 5112 KB Output is correct
32 Correct 9 ms 5112 KB Output is correct
33 Correct 10 ms 5112 KB Output is correct
34 Correct 10 ms 5112 KB Output is correct
35 Correct 10 ms 5112 KB Output is correct
36 Correct 9 ms 5112 KB Output is correct
37 Correct 9 ms 5112 KB Output is correct
38 Correct 9 ms 5112 KB Output is correct
39 Correct 295 ms 11360 KB Output is correct
40 Correct 282 ms 11896 KB Output is correct
41 Correct 312 ms 12104 KB Output is correct
42 Correct 244 ms 11896 KB Output is correct
43 Correct 249 ms 12280 KB Output is correct
44 Correct 142 ms 11848 KB Output is correct
45 Correct 364 ms 19064 KB Output is correct
46 Correct 249 ms 20472 KB Output is correct
47 Correct 374 ms 19832 KB Output is correct
48 Correct 1000 ms 35576 KB Output is correct
49 Correct 999 ms 34808 KB Output is correct
50 Correct 379 ms 19832 KB Output is correct
51 Correct 369 ms 19700 KB Output is correct
52 Correct 473 ms 19832 KB Output is correct
53 Correct 539 ms 17016 KB Output is correct
54 Correct 928 ms 26136 KB Output is correct
55 Correct 28 ms 6136 KB Output is correct
56 Correct 23 ms 5752 KB Output is correct
57 Correct 144 ms 11896 KB Output is correct
58 Correct 57 ms 11628 KB Output is correct
59 Correct 283 ms 20728 KB Output is correct
60 Correct 1016 ms 35192 KB Output is correct
61 Correct 427 ms 22904 KB Output is correct
62 Correct 366 ms 19832 KB Output is correct
63 Correct 462 ms 19832 KB Output is correct
64 Correct 1172 ms 26524 KB Output is correct
65 Correct 980 ms 27384 KB Output is correct
66 Correct 1010 ms 33400 KB Output is correct
67 Correct 168 ms 17636 KB Output is correct
68 Correct 590 ms 27256 KB Output is correct
69 Correct 605 ms 27768 KB Output is correct
70 Correct 550 ms 26360 KB Output is correct