Submission #363802

# Submission time Handle Problem Language Result Execution time Memory
363802 2021-02-07T10:05:14 Z David_M Race (IOI11_race) C++17
100 / 100
394 ms 103008 KB
#include<bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;

const int RANDOM = chrono::high_resolution_clock::now().time_since_epoch().count();
struct chash {
    int operator()(int x) const { return x ^ RANDOM; }
};

const int N=200005;
int sz[N], e[N], z1[N], z2[N], o, k, K, Ans=N;
vector <pair<int, int> > v[N];
gp_hash_table<int, int, chash> m[N];

void solve(int x=0, int pa=0){
	
	sz[x]=1;
	int mx=0, Y, D;
	
	for (auto [y,d]:v[x]){
		if(y==pa)continue;
		solve(y, x);
		sz[x]+=sz[y];
		if(sz[y]>mx) mx=sz[y], Y=y, D=d;
	}
	
	if(sz[x]==1) e[x]=o++;
	else z1[x]=z1[Y]+D, z2[x]=z2[Y]+1, e[x]=e[Y];
	m[e[x]][0-z1[x]]=0-z2[x];
	
	for (auto [y,d]:v[x]){
		if(y==pa||y==Y)continue;
		for(auto [kk,ans]:m[e[y]]){
			k=kk+d+z1[y]+z1[x];
			ans+=1+z2[y]+z2[x];
			if(m[e[x]].find(K-k)!=m[e[x]].end()) Ans=min(Ans, ans+m[e[x]][K-k]);
		}
		for(auto [kk,ans]:m[e[y]]){
			k=kk+d+z1[y]-z1[x];
			ans+=1+z2[y]-z2[x];
			if(m[e[x]].find(k)==m[e[x]].end() || m[e[x]][k]>ans) m[e[x]][k]=ans;
		}
	}
	if(m[e[x]].find(K-z1[x])!=m[e[x]].end())Ans=min(Ans,z2[x]+m[e[x]][K-z1[x]]);
}
 
int best_path(int n, int k, int H[][2], int L[]){
	K=k;
	for (int i=0; i<n-1; i++)
		v[H[i][0]].push_back({H[i][1],L[i]}),
		v[H[i][1]].push_back({H[i][0],L[i]});
	
	solve();
	
	return (Ans==N)?-1:Ans;
}

Compilation message

race.cpp: In function 'void solve(int, int)':
race.cpp:33:11: warning: 'Y' may be used uninitialized in this function [-Wmaybe-uninitialized]
   33 |   if(y==pa||y==Y)continue;
      |      ~~~~~^~~~~~
race.cpp:29:18: warning: 'D' may be used uninitialized in this function [-Wmaybe-uninitialized]
   29 |  else z1[x]=z1[Y]+D, z2[x]=z2[Y]+1, e[x]=e[Y];
      |             ~~~~~^~
# Verdict Execution time Memory Grader output
1 Correct 53 ms 47340 KB Output is correct
2 Correct 52 ms 47488 KB Output is correct
3 Correct 53 ms 47340 KB Output is correct
4 Correct 49 ms 47468 KB Output is correct
5 Correct 54 ms 47340 KB Output is correct
6 Correct 55 ms 47468 KB Output is correct
7 Correct 52 ms 47340 KB Output is correct
8 Correct 52 ms 47340 KB Output is correct
9 Correct 54 ms 47360 KB Output is correct
10 Correct 52 ms 47360 KB Output is correct
11 Correct 53 ms 47340 KB Output is correct
12 Correct 54 ms 47340 KB Output is correct
13 Correct 52 ms 47340 KB Output is correct
14 Correct 52 ms 47340 KB Output is correct
15 Correct 51 ms 47344 KB Output is correct
16 Correct 53 ms 47468 KB Output is correct
17 Correct 54 ms 47340 KB Output is correct
18 Correct 52 ms 47468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 53 ms 47340 KB Output is correct
2 Correct 52 ms 47488 KB Output is correct
3 Correct 53 ms 47340 KB Output is correct
4 Correct 49 ms 47468 KB Output is correct
5 Correct 54 ms 47340 KB Output is correct
6 Correct 55 ms 47468 KB Output is correct
7 Correct 52 ms 47340 KB Output is correct
8 Correct 52 ms 47340 KB Output is correct
9 Correct 54 ms 47360 KB Output is correct
10 Correct 52 ms 47360 KB Output is correct
11 Correct 53 ms 47340 KB Output is correct
12 Correct 54 ms 47340 KB Output is correct
13 Correct 52 ms 47340 KB Output is correct
14 Correct 52 ms 47340 KB Output is correct
15 Correct 51 ms 47344 KB Output is correct
16 Correct 53 ms 47468 KB Output is correct
17 Correct 54 ms 47340 KB Output is correct
18 Correct 52 ms 47468 KB Output is correct
19 Correct 49 ms 47340 KB Output is correct
20 Correct 54 ms 47340 KB Output is correct
21 Correct 53 ms 47468 KB Output is correct
22 Correct 57 ms 47468 KB Output is correct
23 Correct 54 ms 47596 KB Output is correct
24 Correct 53 ms 47468 KB Output is correct
25 Correct 53 ms 47468 KB Output is correct
26 Correct 53 ms 47468 KB Output is correct
27 Correct 50 ms 47468 KB Output is correct
28 Correct 54 ms 47468 KB Output is correct
29 Correct 53 ms 47468 KB Output is correct
30 Correct 54 ms 47468 KB Output is correct
31 Correct 53 ms 47724 KB Output is correct
32 Correct 53 ms 47468 KB Output is correct
33 Correct 56 ms 47468 KB Output is correct
34 Correct 52 ms 47468 KB Output is correct
35 Correct 52 ms 47468 KB Output is correct
36 Correct 65 ms 47596 KB Output is correct
37 Correct 52 ms 47468 KB Output is correct
38 Correct 52 ms 47468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 53 ms 47340 KB Output is correct
2 Correct 52 ms 47488 KB Output is correct
3 Correct 53 ms 47340 KB Output is correct
4 Correct 49 ms 47468 KB Output is correct
5 Correct 54 ms 47340 KB Output is correct
6 Correct 55 ms 47468 KB Output is correct
7 Correct 52 ms 47340 KB Output is correct
8 Correct 52 ms 47340 KB Output is correct
9 Correct 54 ms 47360 KB Output is correct
10 Correct 52 ms 47360 KB Output is correct
11 Correct 53 ms 47340 KB Output is correct
12 Correct 54 ms 47340 KB Output is correct
13 Correct 52 ms 47340 KB Output is correct
14 Correct 52 ms 47340 KB Output is correct
15 Correct 51 ms 47344 KB Output is correct
16 Correct 53 ms 47468 KB Output is correct
17 Correct 54 ms 47340 KB Output is correct
18 Correct 52 ms 47468 KB Output is correct
19 Correct 163 ms 58860 KB Output is correct
20 Correct 147 ms 58732 KB Output is correct
21 Correct 156 ms 58732 KB Output is correct
22 Correct 156 ms 58476 KB Output is correct
23 Correct 167 ms 64236 KB Output is correct
24 Correct 150 ms 61932 KB Output is correct
25 Correct 123 ms 63148 KB Output is correct
26 Correct 109 ms 70636 KB Output is correct
27 Correct 222 ms 64492 KB Output is correct
28 Correct 251 ms 103008 KB Output is correct
29 Correct 248 ms 100836 KB Output is correct
30 Correct 230 ms 64492 KB Output is correct
31 Correct 228 ms 64604 KB Output is correct
32 Correct 263 ms 64564 KB Output is correct
33 Correct 207 ms 67308 KB Output is correct
34 Correct 299 ms 83576 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 53 ms 47340 KB Output is correct
2 Correct 52 ms 47488 KB Output is correct
3 Correct 53 ms 47340 KB Output is correct
4 Correct 49 ms 47468 KB Output is correct
5 Correct 54 ms 47340 KB Output is correct
6 Correct 55 ms 47468 KB Output is correct
7 Correct 52 ms 47340 KB Output is correct
8 Correct 52 ms 47340 KB Output is correct
9 Correct 54 ms 47360 KB Output is correct
10 Correct 52 ms 47360 KB Output is correct
11 Correct 53 ms 47340 KB Output is correct
12 Correct 54 ms 47340 KB Output is correct
13 Correct 52 ms 47340 KB Output is correct
14 Correct 52 ms 47340 KB Output is correct
15 Correct 51 ms 47344 KB Output is correct
16 Correct 53 ms 47468 KB Output is correct
17 Correct 54 ms 47340 KB Output is correct
18 Correct 52 ms 47468 KB Output is correct
19 Correct 49 ms 47340 KB Output is correct
20 Correct 54 ms 47340 KB Output is correct
21 Correct 53 ms 47468 KB Output is correct
22 Correct 57 ms 47468 KB Output is correct
23 Correct 54 ms 47596 KB Output is correct
24 Correct 53 ms 47468 KB Output is correct
25 Correct 53 ms 47468 KB Output is correct
26 Correct 53 ms 47468 KB Output is correct
27 Correct 50 ms 47468 KB Output is correct
28 Correct 54 ms 47468 KB Output is correct
29 Correct 53 ms 47468 KB Output is correct
30 Correct 54 ms 47468 KB Output is correct
31 Correct 53 ms 47724 KB Output is correct
32 Correct 53 ms 47468 KB Output is correct
33 Correct 56 ms 47468 KB Output is correct
34 Correct 52 ms 47468 KB Output is correct
35 Correct 52 ms 47468 KB Output is correct
36 Correct 65 ms 47596 KB Output is correct
37 Correct 52 ms 47468 KB Output is correct
38 Correct 52 ms 47468 KB Output is correct
39 Correct 163 ms 58860 KB Output is correct
40 Correct 147 ms 58732 KB Output is correct
41 Correct 156 ms 58732 KB Output is correct
42 Correct 156 ms 58476 KB Output is correct
43 Correct 167 ms 64236 KB Output is correct
44 Correct 150 ms 61932 KB Output is correct
45 Correct 123 ms 63148 KB Output is correct
46 Correct 109 ms 70636 KB Output is correct
47 Correct 222 ms 64492 KB Output is correct
48 Correct 251 ms 103008 KB Output is correct
49 Correct 248 ms 100836 KB Output is correct
50 Correct 230 ms 64492 KB Output is correct
51 Correct 228 ms 64604 KB Output is correct
52 Correct 263 ms 64564 KB Output is correct
53 Correct 207 ms 67308 KB Output is correct
54 Correct 299 ms 83576 KB Output is correct
55 Correct 63 ms 49132 KB Output is correct
56 Correct 58 ms 48364 KB Output is correct
57 Correct 114 ms 57704 KB Output is correct
58 Correct 97 ms 53472 KB Output is correct
59 Correct 115 ms 75104 KB Output is correct
60 Correct 277 ms 101860 KB Output is correct
61 Correct 250 ms 66584 KB Output is correct
62 Correct 226 ms 64620 KB Output is correct
63 Correct 267 ms 64492 KB Output is correct
64 Correct 392 ms 91752 KB Output is correct
65 Correct 394 ms 90640 KB Output is correct
66 Correct 263 ms 96480 KB Output is correct
67 Correct 221 ms 61412 KB Output is correct
68 Correct 314 ms 77340 KB Output is correct
69 Correct 316 ms 77468 KB Output is correct
70 Correct 306 ms 76444 KB Output is correct