답안 #238706

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
238706 2020-06-12T11:53:17 Z T0p_ 경주 (Race) (IOI11_race) C++14
100 / 100
2003 ms 78712 KB
#include "race.h"
#include <bits/stdc++.h>
using namespace std;
 
#define N 200002
#define pb push_back
 
struct EDGE
{
	int node, weight;
};
 
int ans = 1e9, K;
int lv[N], jmp[2][20][N], sz[N];
bool visit[N];
queue<pair<int, int>> q;
map<int, int> mp[N];
vector<EDGE> g[N];
 
void dfs(int u, int l)
{
	lv[u] = l;
	for(auto x : g[u])
	{
		if(lv[x.node]) continue ;
		dfs(x.node, l+1);
		jmp[0][0][x.node] = u;
		jmp[1][0][x.node] = x.weight;
	}
}
 
int get_sz(int u, int p)
{
	sz[u] = 1;
	for(auto x : g[u])
	{
		if(visit[x.node] || x.node == p) continue ;
		sz[u] += get_sz(x.node, u);
	}
	return sz[u];
}
 
int get_centroid(int u, int p, int n)
{
	bool ch = (n - sz[u] <= n/2) ? true : false;
	int nn = 0, nf = 0;
	for(auto x : g[u])
	{
		if(visit[x.node] || x.node == p) continue ;
		if(sz[x.node] > n/2)
		{
			ch = false;
			if(sz[x.node] > nf)
			{
				nn = x.node;
				nf = sz[x.node];
			}
		}
	}
	return (ch) ? u : get_centroid(nn, u, n);
}
 
pair<int, int> finddis(int u, int v)
{
	int r1 = 0, r2 = 0;
	if(lv[u] < lv[v]) swap(u, v);
	for(int i=19 ; i>=0 ; i--)
		if(lv[jmp[0][i][u]] >= lv[v])
		{
			r1 += (1<<i);
			r2 += jmp[1][i][u];
			u = jmp[0][i][u];
		}
	if(u == v) return {r1, r2};
	for(int i=19 ; i>=0 ; i--)
		if(jmp[0][i][u] != jmp[0][i][v])
		{
			r1 += (1<<(i+1));
			r2 += jmp[1][i][u] + jmp[1][i][v];
			u = jmp[0][i][u], v = jmp[0][i][v];
		}
	return {r1+2, r2+jmp[1][0][u]+jmp[1][0][v]};
}
 
void compute(int u, int p, int r)
{
	pair<int, int> dis = finddis(u, r);
	q.push(dis);
	if(mp[r].count(K - dis.second)) ans = min(ans, mp[r][K - dis.second] + dis.first);
	for(auto x : g[u])
	{
		if(visit[x.node] || x.node == p) continue ;
		compute(x.node, u, r);
	}
}
 
void build(int u, int p)
{
	int c = get_centroid(u, p, get_sz(u, p));
	visit[c] = true;
	mp[c][0] = 0;
	for(auto x : g[c])
	{
		if(visit[x.node]) continue ;
		compute(x.node, c, c);
		while(!q.empty())
		{
			int f = q.front().first, s = q.front().second;
			q.pop();
			if(mp[c].count(s)) mp[c][s] = min(mp[c][s], f);
			else mp[c][s] = f;
		}
	}
	mp[c].clear();
	for(auto x : g[c])
	{
		if(visit[x.node]) continue ;
		build(x.node, c);
	}
 
}
 
int best_path(int n, int k, int h[][2], int l[])
{
	K = k;
	for(int i=0 ; i<n-1 ; i++)
	{
		g[h[i][0]+1].pb({h[i][1]+1, l[i]});
		g[h[i][1]+1].pb({h[i][0]+1, l[i]});
	}
	dfs(1, 1);
	for(int i=1 ; i<20 ; i++)
		for(int j=1 ; j<=n ; j++)
		{
			jmp[0][i][j] = jmp[0][i-1][jmp[0][i-1][j]];
			jmp[1][i][j] = jmp[1][i-1][j] + jmp[1][i-1][jmp[0][i-1][j]];
		}
	build(1, 0);
	return (ans == 1e9) ? -1 : ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 14720 KB Output is correct
2 Correct 12 ms 14720 KB Output is correct
3 Correct 12 ms 14720 KB Output is correct
4 Correct 12 ms 14720 KB Output is correct
5 Correct 13 ms 14720 KB Output is correct
6 Correct 14 ms 14720 KB Output is correct
7 Correct 13 ms 14720 KB Output is correct
8 Correct 13 ms 14720 KB Output is correct
9 Correct 13 ms 14720 KB Output is correct
10 Correct 16 ms 14720 KB Output is correct
11 Correct 13 ms 14720 KB Output is correct
12 Correct 12 ms 14592 KB Output is correct
13 Correct 13 ms 14720 KB Output is correct
14 Correct 15 ms 14644 KB Output is correct
15 Correct 12 ms 14720 KB Output is correct
16 Correct 12 ms 14720 KB Output is correct
17 Correct 13 ms 14720 KB Output is correct
18 Correct 13 ms 14720 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 14720 KB Output is correct
2 Correct 12 ms 14720 KB Output is correct
3 Correct 12 ms 14720 KB Output is correct
4 Correct 12 ms 14720 KB Output is correct
5 Correct 13 ms 14720 KB Output is correct
6 Correct 14 ms 14720 KB Output is correct
7 Correct 13 ms 14720 KB Output is correct
8 Correct 13 ms 14720 KB Output is correct
9 Correct 13 ms 14720 KB Output is correct
10 Correct 16 ms 14720 KB Output is correct
11 Correct 13 ms 14720 KB Output is correct
12 Correct 12 ms 14592 KB Output is correct
13 Correct 13 ms 14720 KB Output is correct
14 Correct 15 ms 14644 KB Output is correct
15 Correct 12 ms 14720 KB Output is correct
16 Correct 12 ms 14720 KB Output is correct
17 Correct 13 ms 14720 KB Output is correct
18 Correct 13 ms 14720 KB Output is correct
19 Correct 12 ms 14720 KB Output is correct
20 Correct 13 ms 14720 KB Output is correct
21 Correct 14 ms 14848 KB Output is correct
22 Correct 15 ms 14848 KB Output is correct
23 Correct 14 ms 14976 KB Output is correct
24 Correct 15 ms 14848 KB Output is correct
25 Correct 14 ms 14848 KB Output is correct
26 Correct 15 ms 14976 KB Output is correct
27 Correct 13 ms 14848 KB Output is correct
28 Correct 14 ms 14848 KB Output is correct
29 Correct 14 ms 14976 KB Output is correct
30 Correct 15 ms 14848 KB Output is correct
31 Correct 15 ms 14848 KB Output is correct
32 Correct 15 ms 14848 KB Output is correct
33 Correct 15 ms 14848 KB Output is correct
34 Correct 14 ms 14848 KB Output is correct
35 Correct 14 ms 14848 KB Output is correct
36 Correct 15 ms 14848 KB Output is correct
37 Correct 14 ms 14848 KB Output is correct
38 Correct 14 ms 14848 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 14720 KB Output is correct
2 Correct 12 ms 14720 KB Output is correct
3 Correct 12 ms 14720 KB Output is correct
4 Correct 12 ms 14720 KB Output is correct
5 Correct 13 ms 14720 KB Output is correct
6 Correct 14 ms 14720 KB Output is correct
7 Correct 13 ms 14720 KB Output is correct
8 Correct 13 ms 14720 KB Output is correct
9 Correct 13 ms 14720 KB Output is correct
10 Correct 16 ms 14720 KB Output is correct
11 Correct 13 ms 14720 KB Output is correct
12 Correct 12 ms 14592 KB Output is correct
13 Correct 13 ms 14720 KB Output is correct
14 Correct 15 ms 14644 KB Output is correct
15 Correct 12 ms 14720 KB Output is correct
16 Correct 12 ms 14720 KB Output is correct
17 Correct 13 ms 14720 KB Output is correct
18 Correct 13 ms 14720 KB Output is correct
19 Correct 565 ms 36344 KB Output is correct
20 Correct 573 ms 36396 KB Output is correct
21 Correct 565 ms 36488 KB Output is correct
22 Correct 515 ms 36472 KB Output is correct
23 Correct 695 ms 36632 KB Output is correct
24 Correct 345 ms 36344 KB Output is correct
25 Correct 633 ms 41720 KB Output is correct
26 Correct 258 ms 41996 KB Output is correct
27 Correct 481 ms 57632 KB Output is correct
28 Correct 1891 ms 77820 KB Output is correct
29 Correct 1898 ms 78712 KB Output is correct
30 Correct 477 ms 59384 KB Output is correct
31 Correct 470 ms 59512 KB Output is correct
32 Correct 794 ms 59384 KB Output is correct
33 Correct 1486 ms 58744 KB Output is correct
34 Correct 1754 ms 67192 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 14720 KB Output is correct
2 Correct 12 ms 14720 KB Output is correct
3 Correct 12 ms 14720 KB Output is correct
4 Correct 12 ms 14720 KB Output is correct
5 Correct 13 ms 14720 KB Output is correct
6 Correct 14 ms 14720 KB Output is correct
7 Correct 13 ms 14720 KB Output is correct
8 Correct 13 ms 14720 KB Output is correct
9 Correct 13 ms 14720 KB Output is correct
10 Correct 16 ms 14720 KB Output is correct
11 Correct 13 ms 14720 KB Output is correct
12 Correct 12 ms 14592 KB Output is correct
13 Correct 13 ms 14720 KB Output is correct
14 Correct 15 ms 14644 KB Output is correct
15 Correct 12 ms 14720 KB Output is correct
16 Correct 12 ms 14720 KB Output is correct
17 Correct 13 ms 14720 KB Output is correct
18 Correct 13 ms 14720 KB Output is correct
19 Correct 12 ms 14720 KB Output is correct
20 Correct 13 ms 14720 KB Output is correct
21 Correct 14 ms 14848 KB Output is correct
22 Correct 15 ms 14848 KB Output is correct
23 Correct 14 ms 14976 KB Output is correct
24 Correct 15 ms 14848 KB Output is correct
25 Correct 14 ms 14848 KB Output is correct
26 Correct 15 ms 14976 KB Output is correct
27 Correct 13 ms 14848 KB Output is correct
28 Correct 14 ms 14848 KB Output is correct
29 Correct 14 ms 14976 KB Output is correct
30 Correct 15 ms 14848 KB Output is correct
31 Correct 15 ms 14848 KB Output is correct
32 Correct 15 ms 14848 KB Output is correct
33 Correct 15 ms 14848 KB Output is correct
34 Correct 14 ms 14848 KB Output is correct
35 Correct 14 ms 14848 KB Output is correct
36 Correct 15 ms 14848 KB Output is correct
37 Correct 14 ms 14848 KB Output is correct
38 Correct 14 ms 14848 KB Output is correct
39 Correct 565 ms 36344 KB Output is correct
40 Correct 573 ms 36396 KB Output is correct
41 Correct 565 ms 36488 KB Output is correct
42 Correct 515 ms 36472 KB Output is correct
43 Correct 695 ms 36632 KB Output is correct
44 Correct 345 ms 36344 KB Output is correct
45 Correct 633 ms 41720 KB Output is correct
46 Correct 258 ms 41996 KB Output is correct
47 Correct 481 ms 57632 KB Output is correct
48 Correct 1891 ms 77820 KB Output is correct
49 Correct 1898 ms 78712 KB Output is correct
50 Correct 477 ms 59384 KB Output is correct
51 Correct 470 ms 59512 KB Output is correct
52 Correct 794 ms 59384 KB Output is correct
53 Correct 1486 ms 58744 KB Output is correct
54 Correct 1754 ms 67192 KB Output is correct
55 Correct 44 ms 17152 KB Output is correct
56 Correct 36 ms 17016 KB Output is correct
57 Correct 262 ms 37604 KB Output is correct
58 Correct 84 ms 37228 KB Output is correct
59 Correct 453 ms 46328 KB Output is correct
60 Correct 1962 ms 78456 KB Output is correct
61 Correct 534 ms 60664 KB Output is correct
62 Correct 469 ms 59384 KB Output is correct
63 Correct 801 ms 59384 KB Output is correct
64 Correct 1806 ms 63736 KB Output is correct
65 Correct 1537 ms 68776 KB Output is correct
66 Correct 2003 ms 78328 KB Output is correct
67 Correct 295 ms 59748 KB Output is correct
68 Correct 920 ms 67144 KB Output is correct
69 Correct 933 ms 67552 KB Output is correct
70 Correct 850 ms 64888 KB Output is correct