Submission #254544

# Submission time Handle Problem Language Result Execution time Memory
254544 2020-07-30T08:30:25 Z tinjyu Race (IOI11_race) C++14
100 / 100
650 ms 47984 KB
#include "race.h"
#include <iostream>
using namespace std;
long long int ans=99999999999,a[1000005],t[1000005][2],p,num,f[1000005],tag[1000005],son[1000005],n,k,map[1000005][3],road[1000005];
int build(int x,int fa)
{
	son[x]=1;
	f[x]=fa;
	long long int g=road[x];
	while(g!=-1)
	{
		long long int now=map[g][0];
		if(now!=fa && tag[now]==0)
		{
			build(now,x);
			son[x]+=son[now];
		}
		g=map[g][1];
	}
}
int find(int x,int fa,long long int sum)
{
	long long int g=road[x];
	long long int can=1;
	if(sum>num/2)can=0;
	while(g!=-1)
	{
		long long int now=map[g][0];
		if(now!=fa && tag[now]==0)
		{
			long long int tmp=find(now,x,sum+son[x]-son[now]);
			if(tmp!=-1)return tmp;
			if(son[now]>num/2)can=0;
		}
		g=map[g][1];
	}
	if(can==0)return -1;
	return x;
}
int dfs(int x,long long int len,int fa,int cnt)
{
	if(len<=k)
	{
		p++;
		t[p][0]=len;
		t[p][1]=cnt;
	}
	long long int g=road[x];
	while(g!=-1)
	{
		long long int now=map[g][0];
		if(now!=fa && tag[now]==0)
		{
			dfs(now,len+map[g][2],x,cnt+1);
		}
		g=map[g][1];
	}
}
int solve(int x)
{
	build(x,-1);
	num=son[x];
	long long int st=find(x,-1,0);
	long long int g=road[st],pp=1;	
	//cout<<"solve"<<st<<endl;
	while(g!=-1)
	{
		long long int now=map[g][0];
	//	cout<<now<<endl;
		if(tag[now]==0)
		{
			dfs(now,map[g][2],st,1);
			for(int i=pp;i<=p;i++)
			{
				ans=min(ans,a[k-t[i][0]]+t[i][1]);
				//cout<<t[i][0]<<" "<<t[i][1]<<"  ";
			}
			//cout<<endl;
			for(int i=pp;i<=p;i++)
			{
				a[t[i][0]]=min(a[t[i][0]],t[i][1]);
			}
			ans=min(ans,a[k]);
			pp=p+1;
		}
		g=map[g][1];
	}
	for(int i=1;i<=p;i++)a[t[i][0]]=9999999999999999;
	g=road[st];
	tag[st]=1;
	p=0;
	while(g!=-1)
	{
		long long int now=map[g][0];
		if(tag[now]==0)
		{
			solve(now);
		}
		g=map[g][1];
	}
}
int best_path(int N, int K, int H[][2], int L[])
{
	n=N;
	k=K;
	for(int i=1;i<=k;i++)a[i]=9999999999999999;
	for(int i=0;i<n;i++)road[i]=-1;
	for(int i=0;i<n-1;i++)
	{
		map[i*2][0]=H[i][0];
		map[i*2][1]=road[H[i][1]];
		map[i*2][2]=L[i];
		road[H[i][1]]=i*2;
		map[i*2+1][0]=H[i][1];
		map[i*2+1][1]=road[H[i][0]];
		map[i*2+1][2]=L[i];
		road[H[i][0]]=i*2+1;
	}
	solve(0);
	if(ans==99999999999)return -1;
  	return ans;
}

Compilation message

race.cpp: In function 'int build(int, int)':
race.cpp:20:1: warning: no return statement in function returning non-void [-Wreturn-type]
 }
 ^
race.cpp: In function 'int dfs(int, long long int, int, int)':
race.cpp:58:1: warning: no return statement in function returning non-void [-Wreturn-type]
 }
 ^
race.cpp: In function 'int solve(int)':
race.cpp:101:1: warning: no return statement in function returning non-void [-Wreturn-type]
 }
 ^
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 0 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 0 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 0 ms 384 KB Output is correct
18 Correct 0 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 0 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 0 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 0 ms 384 KB Output is correct
18 Correct 0 ms 384 KB Output is correct
19 Correct 0 ms 384 KB Output is correct
20 Correct 0 ms 384 KB Output is correct
21 Correct 1 ms 512 KB Output is correct
22 Correct 5 ms 7680 KB Output is correct
23 Correct 4 ms 6400 KB Output is correct
24 Correct 5 ms 7296 KB Output is correct
25 Correct 4 ms 7040 KB Output is correct
26 Correct 2 ms 3072 KB Output is correct
27 Correct 4 ms 6784 KB Output is correct
28 Correct 2 ms 2048 KB Output is correct
29 Correct 3 ms 3072 KB Output is correct
30 Correct 4 ms 3328 KB Output is correct
31 Correct 5 ms 5632 KB Output is correct
32 Correct 4 ms 6016 KB Output is correct
33 Correct 4 ms 6656 KB Output is correct
34 Correct 3 ms 5120 KB Output is correct
35 Correct 4 ms 6784 KB Output is correct
36 Correct 4 ms 7808 KB Output is correct
37 Correct 4 ms 6656 KB Output is correct
38 Correct 3 ms 4480 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 0 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 0 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 0 ms 384 KB Output is correct
18 Correct 0 ms 384 KB Output is correct
19 Correct 157 ms 9592 KB Output is correct
20 Correct 170 ms 9592 KB Output is correct
21 Correct 188 ms 10104 KB Output is correct
22 Correct 167 ms 10872 KB Output is correct
23 Correct 115 ms 9592 KB Output is correct
24 Correct 71 ms 9592 KB Output is correct
25 Correct 155 ms 14584 KB Output is correct
26 Correct 115 ms 20472 KB Output is correct
27 Correct 217 ms 18680 KB Output is correct
28 Correct 431 ms 37240 KB Output is correct
29 Correct 476 ms 35932 KB Output is correct
30 Correct 225 ms 18632 KB Output is correct
31 Correct 232 ms 18616 KB Output is correct
32 Correct 321 ms 18532 KB Output is correct
33 Correct 356 ms 19408 KB Output is correct
34 Correct 350 ms 19320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 0 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 0 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 0 ms 384 KB Output is correct
18 Correct 0 ms 384 KB Output is correct
19 Correct 0 ms 384 KB Output is correct
20 Correct 0 ms 384 KB Output is correct
21 Correct 1 ms 512 KB Output is correct
22 Correct 5 ms 7680 KB Output is correct
23 Correct 4 ms 6400 KB Output is correct
24 Correct 5 ms 7296 KB Output is correct
25 Correct 4 ms 7040 KB Output is correct
26 Correct 2 ms 3072 KB Output is correct
27 Correct 4 ms 6784 KB Output is correct
28 Correct 2 ms 2048 KB Output is correct
29 Correct 3 ms 3072 KB Output is correct
30 Correct 4 ms 3328 KB Output is correct
31 Correct 5 ms 5632 KB Output is correct
32 Correct 4 ms 6016 KB Output is correct
33 Correct 4 ms 6656 KB Output is correct
34 Correct 3 ms 5120 KB Output is correct
35 Correct 4 ms 6784 KB Output is correct
36 Correct 4 ms 7808 KB Output is correct
37 Correct 4 ms 6656 KB Output is correct
38 Correct 3 ms 4480 KB Output is correct
39 Correct 157 ms 9592 KB Output is correct
40 Correct 170 ms 9592 KB Output is correct
41 Correct 188 ms 10104 KB Output is correct
42 Correct 167 ms 10872 KB Output is correct
43 Correct 115 ms 9592 KB Output is correct
44 Correct 71 ms 9592 KB Output is correct
45 Correct 155 ms 14584 KB Output is correct
46 Correct 115 ms 20472 KB Output is correct
47 Correct 217 ms 18680 KB Output is correct
48 Correct 431 ms 37240 KB Output is correct
49 Correct 476 ms 35932 KB Output is correct
50 Correct 225 ms 18632 KB Output is correct
51 Correct 232 ms 18616 KB Output is correct
52 Correct 321 ms 18532 KB Output is correct
53 Correct 356 ms 19408 KB Output is correct
54 Correct 350 ms 19320 KB Output is correct
55 Correct 10 ms 1536 KB Output is correct
56 Correct 11 ms 1536 KB Output is correct
57 Correct 100 ms 11660 KB Output is correct
58 Correct 42 ms 11768 KB Output is correct
59 Correct 119 ms 22520 KB Output is correct
60 Correct 469 ms 47984 KB Output is correct
61 Correct 220 ms 19808 KB Output is correct
62 Correct 258 ms 29984 KB Output is correct
63 Correct 346 ms 30080 KB Output is correct
64 Correct 650 ms 26396 KB Output is correct
65 Correct 405 ms 19204 KB Output is correct
66 Correct 555 ms 41188 KB Output is correct
67 Correct 112 ms 30108 KB Output is correct
68 Correct 347 ms 28024 KB Output is correct
69 Correct 430 ms 28164 KB Output is correct
70 Correct 344 ms 27184 KB Output is correct