Submission #1054400

# Submission time Handle Problem Language Result Execution time Memory
1054400 2024-08-12T09:38:20 Z ihceker Race (IOI11_race) C++14
100 / 100
237 ms 34384 KB
#include<bits/stdc++.h>
#include"race.h"
#define ll long long
#define MOD 1000000007
#define all(x) x.begin(),x.end()
#define ff first
#define ss second
#define pb push_back
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

using namespace std;

int best_path(int n,int k,int h[][2],int l[]){
	vector<vector<pair<int,int>>>adj(n);
	for(int i=0;i<n-1;i++){
		adj[h[i][0]].pb({h[i][1],l[i]});
		adj[h[i][1]].pb({h[i][0],l[i]});
	}
	vector<int>vis(n),sub(n);
	auto dfs=[&](int node,int p,auto&& dfs)->void{
		sub[node]=1;
		for(auto i:adj[node]){
			if(vis[i.ff] || i.ff==p)continue;
			dfs(i.ff,node,dfs);
			sub[node]+=sub[i.ff];
		}
		return;
	};
	vector<int>cnt(k+1,-1);
	auto dfs2=[&](int node,int p,int dep,int len,vector<pair<int,int>>&depths,auto&& dfs2)->void{
		depths.pb({len,dep});
		for(auto i:adj[node]){
			if(vis[i.ff] || i.ff==p)continue;
			dfs2(i.ff,node,dep+1,len+i.ss,depths,dfs2);
		}
		return;
	};
	auto get_centroid=[&](int node)->int{
		bool end=false;
		int p=-1,sz=sub[node];
		while(!end){
			end=true;
			for(auto i:adj[node]){
				if(vis[i.ff] || i.ff==p || sub[i.ff]<=sz/2)continue;
				p=node;
				node=i.ff;
				end=false;
				break;
			}
		}
		return node;
	};
	int ans=2e9;
	vector<vector<pair<int,int>>>depths;
	auto calc=[&](int node,auto&& calc)->void{
		vis[node]=1;
		depths.clear();
		for(auto i:adj[node]){
			if(!vis[i.ff]){
				depths.pb(vector<pair<int,int>>{});
				dfs2(i.ff,node,1,i.ss,depths.back(),dfs2);
			}
		}
		cnt[0]=0;
		for(auto i:depths){
			for(auto j:i){
				if(j.ff>k)continue;
				if(cnt[k-j.ff]!=-1){
					ans=min(ans,j.ss+cnt[k-j.ff]);
				}
			}
			for(auto j:i){
				if(j.ff>k)continue;
				if(cnt[j.ff]==-1)cnt[j.ff]=j.ss;
				else cnt[j.ff]=min(cnt[j.ff],j.ss);
			}
		}
		for(auto i:depths){
			for(auto j:i){
				if(j.ff>k)continue;
				cnt[j.ff]=-1;
			}
		}
		for(auto i:adj[node]){
			if(vis[i.ff])continue;
			dfs(i.ff,node,dfs);
			int v=get_centroid(i.ff);
			calc(v,calc);
		}
	};
	dfs(0,-1,dfs);
	int s=get_centroid(0);
	calc(s,calc);
	return (ans==2e9?-1:ans);
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 0 ms 2396 KB Output is correct
3 Correct 0 ms 2396 KB Output is correct
4 Correct 0 ms 2396 KB Output is correct
5 Correct 0 ms 2396 KB Output is correct
6 Correct 0 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 0 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 0 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 0 ms 2396 KB Output is correct
13 Correct 0 ms 2396 KB Output is correct
14 Correct 0 ms 2396 KB Output is correct
15 Correct 0 ms 2396 KB Output is correct
16 Correct 0 ms 2396 KB Output is correct
17 Correct 0 ms 2396 KB Output is correct
18 Correct 0 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 0 ms 2396 KB Output is correct
3 Correct 0 ms 2396 KB Output is correct
4 Correct 0 ms 2396 KB Output is correct
5 Correct 0 ms 2396 KB Output is correct
6 Correct 0 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 0 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 0 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 0 ms 2396 KB Output is correct
13 Correct 0 ms 2396 KB Output is correct
14 Correct 0 ms 2396 KB Output is correct
15 Correct 0 ms 2396 KB Output is correct
16 Correct 0 ms 2396 KB Output is correct
17 Correct 0 ms 2396 KB Output is correct
18 Correct 0 ms 2396 KB Output is correct
19 Correct 1 ms 2648 KB Output is correct
20 Correct 0 ms 2396 KB Output is correct
21 Correct 1 ms 2396 KB Output is correct
22 Correct 2 ms 5980 KB Output is correct
23 Correct 2 ms 5468 KB Output is correct
24 Correct 2 ms 5724 KB Output is correct
25 Correct 2 ms 5724 KB Output is correct
26 Correct 2 ms 3676 KB Output is correct
27 Correct 3 ms 5536 KB Output is correct
28 Correct 1 ms 3252 KB Output is correct
29 Correct 1 ms 3676 KB Output is correct
30 Correct 1 ms 3932 KB Output is correct
31 Correct 2 ms 4956 KB Output is correct
32 Correct 2 ms 5212 KB Output is correct
33 Correct 1 ms 5468 KB Output is correct
34 Correct 1 ms 4700 KB Output is correct
35 Correct 2 ms 5724 KB Output is correct
36 Correct 2 ms 6072 KB Output is correct
37 Correct 1 ms 5468 KB Output is correct
38 Correct 1 ms 4444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 0 ms 2396 KB Output is correct
3 Correct 0 ms 2396 KB Output is correct
4 Correct 0 ms 2396 KB Output is correct
5 Correct 0 ms 2396 KB Output is correct
6 Correct 0 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 0 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 0 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 0 ms 2396 KB Output is correct
13 Correct 0 ms 2396 KB Output is correct
14 Correct 0 ms 2396 KB Output is correct
15 Correct 0 ms 2396 KB Output is correct
16 Correct 0 ms 2396 KB Output is correct
17 Correct 0 ms 2396 KB Output is correct
18 Correct 0 ms 2396 KB Output is correct
19 Correct 83 ms 12632 KB Output is correct
20 Correct 77 ms 12756 KB Output is correct
21 Correct 83 ms 12748 KB Output is correct
22 Correct 66 ms 12744 KB Output is correct
23 Correct 63 ms 13148 KB Output is correct
24 Correct 40 ms 12628 KB Output is correct
25 Correct 85 ms 14176 KB Output is correct
26 Correct 56 ms 16220 KB Output is correct
27 Correct 99 ms 20576 KB Output is correct
28 Correct 190 ms 27484 KB Output is correct
29 Correct 211 ms 26956 KB Output is correct
30 Correct 96 ms 20564 KB Output is correct
31 Correct 110 ms 20556 KB Output is correct
32 Correct 127 ms 20560 KB Output is correct
33 Correct 141 ms 19912 KB Output is correct
34 Correct 142 ms 20184 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 0 ms 2396 KB Output is correct
3 Correct 0 ms 2396 KB Output is correct
4 Correct 0 ms 2396 KB Output is correct
5 Correct 0 ms 2396 KB Output is correct
6 Correct 0 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 0 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 0 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 0 ms 2396 KB Output is correct
13 Correct 0 ms 2396 KB Output is correct
14 Correct 0 ms 2396 KB Output is correct
15 Correct 0 ms 2396 KB Output is correct
16 Correct 0 ms 2396 KB Output is correct
17 Correct 0 ms 2396 KB Output is correct
18 Correct 0 ms 2396 KB Output is correct
19 Correct 1 ms 2648 KB Output is correct
20 Correct 0 ms 2396 KB Output is correct
21 Correct 1 ms 2396 KB Output is correct
22 Correct 2 ms 5980 KB Output is correct
23 Correct 2 ms 5468 KB Output is correct
24 Correct 2 ms 5724 KB Output is correct
25 Correct 2 ms 5724 KB Output is correct
26 Correct 2 ms 3676 KB Output is correct
27 Correct 3 ms 5536 KB Output is correct
28 Correct 1 ms 3252 KB Output is correct
29 Correct 1 ms 3676 KB Output is correct
30 Correct 1 ms 3932 KB Output is correct
31 Correct 2 ms 4956 KB Output is correct
32 Correct 2 ms 5212 KB Output is correct
33 Correct 1 ms 5468 KB Output is correct
34 Correct 1 ms 4700 KB Output is correct
35 Correct 2 ms 5724 KB Output is correct
36 Correct 2 ms 6072 KB Output is correct
37 Correct 1 ms 5468 KB Output is correct
38 Correct 1 ms 4444 KB Output is correct
39 Correct 83 ms 12632 KB Output is correct
40 Correct 77 ms 12756 KB Output is correct
41 Correct 83 ms 12748 KB Output is correct
42 Correct 66 ms 12744 KB Output is correct
43 Correct 63 ms 13148 KB Output is correct
44 Correct 40 ms 12628 KB Output is correct
45 Correct 85 ms 14176 KB Output is correct
46 Correct 56 ms 16220 KB Output is correct
47 Correct 99 ms 20576 KB Output is correct
48 Correct 190 ms 27484 KB Output is correct
49 Correct 211 ms 26956 KB Output is correct
50 Correct 96 ms 20564 KB Output is correct
51 Correct 110 ms 20556 KB Output is correct
52 Correct 127 ms 20560 KB Output is correct
53 Correct 141 ms 19912 KB Output is correct
54 Correct 142 ms 20184 KB Output is correct
55 Correct 5 ms 3164 KB Output is correct
56 Correct 6 ms 3164 KB Output is correct
57 Correct 49 ms 13012 KB Output is correct
58 Correct 26 ms 17084 KB Output is correct
59 Correct 65 ms 16992 KB Output is correct
60 Correct 190 ms 31108 KB Output is correct
61 Correct 98 ms 20440 KB Output is correct
62 Correct 98 ms 24400 KB Output is correct
63 Correct 110 ms 24472 KB Output is correct
64 Correct 189 ms 23276 KB Output is correct
65 Correct 146 ms 21196 KB Output is correct
66 Correct 237 ms 29524 KB Output is correct
67 Correct 73 ms 34384 KB Output is correct
68 Correct 122 ms 25040 KB Output is correct
69 Correct 119 ms 25052 KB Output is correct
70 Correct 116 ms 24316 KB Output is correct