Submission #864095

# Submission time Handle Problem Language Result Execution time Memory
864095 2023-10-22T03:24:26 Z 20163070 Race (IOI11_race) C++14
100 / 100
203 ms 51028 KB
#include <bits/stdc++.h>
#include "race.h"
using namespace std;

const int N=1e6+10,INF=1e9;

int n,k;
int h[N],e[N],ne[N],w[N],idx;
int sz[N],mx[N],vis[N],sum,root;
pair<int,int> s[N];
int f[N];
int ans;
int tp;
int q[N];

void add(int a,int b,int c)
{
	e[idx]=b,ne[idx]=h[a],w[idx]=c,h[a]=idx++;
}

void getroot(int u,int fa)
{
	sz[u]=1;
	mx[u]=0;
	for(int i=h[u];~i;i=ne[i])
	{
		int j=e[i];
		if(j==fa||vis[j]) continue;
		getroot(j,u);
		sz[u]+=sz[j];
		mx[u]=max(mx[u],sz[j]);
	}
	mx[u]=max(mx[u],sum-sz[u]);
	if(mx[root]>mx[u]) root=u;
}

void getdis(int u,int fa,int d,int p)
{
	if(d>k) return;
	s[++tp]=make_pair(d,p);
	for(int i=h[u];~i;i=ne[i])
	{
		int j=e[i];
		if(j==fa||vis[j]) continue;
		getdis(j,u,d+w[i],p+1);
	}
}

void solve(int u)
{
	int cnt=0;
	for(int i=h[u];~i;i=ne[i])
	{
		int j=e[i];
		if(vis[j]) continue;
		tp=0;
		getdis(j,u,w[i],1);
		for(int i=1;i<=tp;i++) ans=min(ans,s[i].second+f[k-s[i].first]);
		for(int i=1;i<=tp;i++) f[q[++cnt]=s[i].first]=min(f[s[i].first],s[i].second);
	}
	for(int i=1;i<=cnt;i++) f[q[i]]=INF;
}

void dfs(int u)
{
	vis[u]=1;
	f[0]=0;
	solve(u);
	for(int i=h[u];~i;i=ne[i])
	{
		int j=e[i];
		if(vis[j]) continue;
		root=0;
		sum=sz[j];
		getroot(j,u);
		dfs(root);
	}
}

int best_path(int N, int K, int tt[][2], int l[])
{
	n=N,k=K;
	memset(h,-1,sizeof h);
	for(int i=0;i<n-1;i++)
	{
		++tt[i][0],++tt[i][1];
		add(tt[i][0],tt[i][1],l[i]);
		add(tt[i][1],tt[i][0],l[i]);
	}
	for(int i=0;i<=k;i++) f[i]=INF;
	ans=mx[0]=INF;
	root=0;
	sum=n;
	getroot(1,0);
	dfs(root);
	if(ans==INF) return -1;
	return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 24408 KB Output is correct
2 Correct 3 ms 24412 KB Output is correct
3 Correct 4 ms 24520 KB Output is correct
4 Correct 3 ms 24412 KB Output is correct
5 Correct 3 ms 22364 KB Output is correct
6 Correct 3 ms 24412 KB Output is correct
7 Correct 3 ms 24408 KB Output is correct
8 Correct 3 ms 24412 KB Output is correct
9 Correct 4 ms 24412 KB Output is correct
10 Correct 3 ms 24412 KB Output is correct
11 Correct 3 ms 24412 KB Output is correct
12 Correct 3 ms 24412 KB Output is correct
13 Correct 3 ms 24408 KB Output is correct
14 Correct 3 ms 24412 KB Output is correct
15 Correct 4 ms 24412 KB Output is correct
16 Correct 4 ms 24412 KB Output is correct
17 Correct 3 ms 24412 KB Output is correct
18 Correct 4 ms 24412 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 24408 KB Output is correct
2 Correct 3 ms 24412 KB Output is correct
3 Correct 4 ms 24520 KB Output is correct
4 Correct 3 ms 24412 KB Output is correct
5 Correct 3 ms 22364 KB Output is correct
6 Correct 3 ms 24412 KB Output is correct
7 Correct 3 ms 24408 KB Output is correct
8 Correct 3 ms 24412 KB Output is correct
9 Correct 4 ms 24412 KB Output is correct
10 Correct 3 ms 24412 KB Output is correct
11 Correct 3 ms 24412 KB Output is correct
12 Correct 3 ms 24412 KB Output is correct
13 Correct 3 ms 24408 KB Output is correct
14 Correct 3 ms 24412 KB Output is correct
15 Correct 4 ms 24412 KB Output is correct
16 Correct 4 ms 24412 KB Output is correct
17 Correct 3 ms 24412 KB Output is correct
18 Correct 4 ms 24412 KB Output is correct
19 Correct 3 ms 24664 KB Output is correct
20 Correct 4 ms 24412 KB Output is correct
21 Correct 4 ms 24412 KB Output is correct
22 Correct 4 ms 26460 KB Output is correct
23 Correct 4 ms 26460 KB Output is correct
24 Correct 5 ms 26712 KB Output is correct
25 Correct 4 ms 26460 KB Output is correct
26 Correct 4 ms 26456 KB Output is correct
27 Correct 4 ms 26460 KB Output is correct
28 Correct 4 ms 26456 KB Output is correct
29 Correct 4 ms 26456 KB Output is correct
30 Correct 4 ms 26460 KB Output is correct
31 Correct 6 ms 26460 KB Output is correct
32 Correct 5 ms 26460 KB Output is correct
33 Correct 5 ms 26460 KB Output is correct
34 Correct 5 ms 26460 KB Output is correct
35 Correct 5 ms 26612 KB Output is correct
36 Correct 4 ms 26616 KB Output is correct
37 Correct 4 ms 26460 KB Output is correct
38 Correct 4 ms 26460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 24408 KB Output is correct
2 Correct 3 ms 24412 KB Output is correct
3 Correct 4 ms 24520 KB Output is correct
4 Correct 3 ms 24412 KB Output is correct
5 Correct 3 ms 22364 KB Output is correct
6 Correct 3 ms 24412 KB Output is correct
7 Correct 3 ms 24408 KB Output is correct
8 Correct 3 ms 24412 KB Output is correct
9 Correct 4 ms 24412 KB Output is correct
10 Correct 3 ms 24412 KB Output is correct
11 Correct 3 ms 24412 KB Output is correct
12 Correct 3 ms 24412 KB Output is correct
13 Correct 3 ms 24408 KB Output is correct
14 Correct 3 ms 24412 KB Output is correct
15 Correct 4 ms 24412 KB Output is correct
16 Correct 4 ms 24412 KB Output is correct
17 Correct 3 ms 24412 KB Output is correct
18 Correct 4 ms 24412 KB Output is correct
19 Correct 71 ms 29596 KB Output is correct
20 Correct 70 ms 29384 KB Output is correct
21 Correct 75 ms 29600 KB Output is correct
22 Correct 72 ms 29544 KB Output is correct
23 Correct 44 ms 29560 KB Output is correct
24 Correct 37 ms 29524 KB Output is correct
25 Correct 67 ms 33620 KB Output is correct
26 Correct 67 ms 37424 KB Output is correct
27 Correct 79 ms 30316 KB Output is correct
28 Correct 122 ms 45936 KB Output is correct
29 Correct 135 ms 44828 KB Output is correct
30 Correct 83 ms 30420 KB Output is correct
31 Correct 86 ms 30312 KB Output is correct
32 Correct 89 ms 30328 KB Output is correct
33 Correct 111 ms 30592 KB Output is correct
34 Correct 99 ms 30576 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 24408 KB Output is correct
2 Correct 3 ms 24412 KB Output is correct
3 Correct 4 ms 24520 KB Output is correct
4 Correct 3 ms 24412 KB Output is correct
5 Correct 3 ms 22364 KB Output is correct
6 Correct 3 ms 24412 KB Output is correct
7 Correct 3 ms 24408 KB Output is correct
8 Correct 3 ms 24412 KB Output is correct
9 Correct 4 ms 24412 KB Output is correct
10 Correct 3 ms 24412 KB Output is correct
11 Correct 3 ms 24412 KB Output is correct
12 Correct 3 ms 24412 KB Output is correct
13 Correct 3 ms 24408 KB Output is correct
14 Correct 3 ms 24412 KB Output is correct
15 Correct 4 ms 24412 KB Output is correct
16 Correct 4 ms 24412 KB Output is correct
17 Correct 3 ms 24412 KB Output is correct
18 Correct 4 ms 24412 KB Output is correct
19 Correct 3 ms 24664 KB Output is correct
20 Correct 4 ms 24412 KB Output is correct
21 Correct 4 ms 24412 KB Output is correct
22 Correct 4 ms 26460 KB Output is correct
23 Correct 4 ms 26460 KB Output is correct
24 Correct 5 ms 26712 KB Output is correct
25 Correct 4 ms 26460 KB Output is correct
26 Correct 4 ms 26456 KB Output is correct
27 Correct 4 ms 26460 KB Output is correct
28 Correct 4 ms 26456 KB Output is correct
29 Correct 4 ms 26456 KB Output is correct
30 Correct 4 ms 26460 KB Output is correct
31 Correct 6 ms 26460 KB Output is correct
32 Correct 5 ms 26460 KB Output is correct
33 Correct 5 ms 26460 KB Output is correct
34 Correct 5 ms 26460 KB Output is correct
35 Correct 5 ms 26612 KB Output is correct
36 Correct 4 ms 26616 KB Output is correct
37 Correct 4 ms 26460 KB Output is correct
38 Correct 4 ms 26460 KB Output is correct
39 Correct 71 ms 29596 KB Output is correct
40 Correct 70 ms 29384 KB Output is correct
41 Correct 75 ms 29600 KB Output is correct
42 Correct 72 ms 29544 KB Output is correct
43 Correct 44 ms 29560 KB Output is correct
44 Correct 37 ms 29524 KB Output is correct
45 Correct 67 ms 33620 KB Output is correct
46 Correct 67 ms 37424 KB Output is correct
47 Correct 79 ms 30316 KB Output is correct
48 Correct 122 ms 45936 KB Output is correct
49 Correct 135 ms 44828 KB Output is correct
50 Correct 83 ms 30420 KB Output is correct
51 Correct 86 ms 30312 KB Output is correct
52 Correct 89 ms 30328 KB Output is correct
53 Correct 111 ms 30592 KB Output is correct
54 Correct 99 ms 30576 KB Output is correct
55 Correct 8 ms 24828 KB Output is correct
56 Correct 8 ms 24676 KB Output is correct
57 Correct 51 ms 30624 KB Output is correct
58 Correct 26 ms 30252 KB Output is correct
59 Correct 70 ms 40628 KB Output is correct
60 Correct 182 ms 51028 KB Output is correct
61 Correct 87 ms 32204 KB Output is correct
62 Correct 94 ms 36308 KB Output is correct
63 Correct 106 ms 36344 KB Output is correct
64 Correct 203 ms 36532 KB Output is correct
65 Correct 103 ms 32488 KB Output is correct
66 Correct 164 ms 45932 KB Output is correct
67 Correct 62 ms 36492 KB Output is correct
68 Correct 113 ms 34376 KB Output is correct
69 Correct 127 ms 34656 KB Output is correct
70 Correct 115 ms 34396 KB Output is correct