Submission #898723

# Submission time Handle Problem Language Result Execution time Memory
898723 2024-01-05T04:43:05 Z Faisal_Saqib Race (IOI11_race) C++17
100 / 100
587 ms 44572 KB
#include <iostream>
#include <vector>
#include <map>
using namespace std;
const int N1=2e5+10;
const int inf=4e5;
int n,k,ans=inf;
vector<pair<int,int>> ma[N1];
int sz[N1];
bool done_for[N1];
void dfs_sz(int x,int p=-1)
{
	sz[x]=1;
	for(auto [y,w]:ma[x])
		if(y!=p)
		{
			dfs_sz(y,x);
			sz[x]+=sz[y];
		}
}
int current_n=0;
int find_centroid(int x,int p=-1)
{
	for(auto [y,w]:ma[x])
	{
		if(!done_for[y] and y!=p and sz[y]>(current_n/2))
		{
			sz[x]-=sz[y];
			sz[y]+=sz[x];
			return find_centroid(y,x);
		}
	}
	return x;
}
map<int,int> tmp;
void dfs_update(int x,long long su=0,int h=1,int p=-1)
{
	if(su>k)
		return;
	if(tmp.find(k-su)!=tmp.end())
		ans=min(ans,tmp[k-su]+h);
	for(auto [y,w]:ma[x])
		if(y!=p and !done_for[y])
			dfs_update(y,su+w,h+1,x);
}
void dfs_add(int x,long long su=0,int h=1,int p=-1)
{
	if(su>k)
		return;
	if(tmp.find(su)==tmp.end())
		tmp[su]=h;
	else
		tmp[su]=min(tmp[su],h);
	for(auto [y,w]:ma[x])
	{
		if(y!=p and !done_for[y])
		{
			dfs_add(y,su+w,h+1,x);
		}
	}
}
void solve(int x,int p=-1,int l=0)
{
	int c=find_centroid(x);
	done_for[c]=1;
	// Full working so now we do dfs
	// What we do is the we go 
	tmp.clear();
	tmp[0]=0;
	for(auto [y,w]:ma[c])
	{
		if(!done_for[y])
		{
			dfs_update(y,w,1,-1);
			dfs_add(y,w,1,-1);
		}
	}
	for(auto [y,w]:ma[c])
	{
		if(!done_for[y])
		{
			current_n=sz[y];
			solve(y,c,l+1);
		}
	}
}
int best_path(int n1, int k1, int h[][2], int l[])
{
	n=n1;
	k=k1;
	for(int i=0;i<(n-1);i++)
	{
		ma[h[i][0]].push_back({h[i][1],l[i]});
		ma[h[i][1]].push_back({h[i][0],l[i]});
	}
	dfs_sz(0);
	current_n=n;
	solve(0);
	if(ans==inf)
		ans=-1;
	return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9564 KB Output is correct
2 Correct 2 ms 9564 KB Output is correct
3 Correct 2 ms 9564 KB Output is correct
4 Correct 2 ms 9564 KB Output is correct
5 Correct 2 ms 9564 KB Output is correct
6 Correct 2 ms 9564 KB Output is correct
7 Correct 2 ms 9564 KB Output is correct
8 Correct 2 ms 9564 KB Output is correct
9 Correct 2 ms 9564 KB Output is correct
10 Correct 2 ms 9564 KB Output is correct
11 Correct 2 ms 9560 KB Output is correct
12 Correct 2 ms 9564 KB Output is correct
13 Correct 2 ms 9564 KB Output is correct
14 Correct 2 ms 9564 KB Output is correct
15 Correct 2 ms 9564 KB Output is correct
16 Correct 2 ms 9564 KB Output is correct
17 Correct 2 ms 9564 KB Output is correct
18 Correct 2 ms 9564 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9564 KB Output is correct
2 Correct 2 ms 9564 KB Output is correct
3 Correct 2 ms 9564 KB Output is correct
4 Correct 2 ms 9564 KB Output is correct
5 Correct 2 ms 9564 KB Output is correct
6 Correct 2 ms 9564 KB Output is correct
7 Correct 2 ms 9564 KB Output is correct
8 Correct 2 ms 9564 KB Output is correct
9 Correct 2 ms 9564 KB Output is correct
10 Correct 2 ms 9564 KB Output is correct
11 Correct 2 ms 9560 KB Output is correct
12 Correct 2 ms 9564 KB Output is correct
13 Correct 2 ms 9564 KB Output is correct
14 Correct 2 ms 9564 KB Output is correct
15 Correct 2 ms 9564 KB Output is correct
16 Correct 2 ms 9564 KB Output is correct
17 Correct 2 ms 9564 KB Output is correct
18 Correct 2 ms 9564 KB Output is correct
19 Correct 2 ms 9560 KB Output is correct
20 Correct 3 ms 9560 KB Output is correct
21 Correct 3 ms 9564 KB Output is correct
22 Correct 2 ms 9564 KB Output is correct
23 Correct 2 ms 9792 KB Output is correct
24 Correct 3 ms 9804 KB Output is correct
25 Correct 3 ms 9820 KB Output is correct
26 Correct 2 ms 9564 KB Output is correct
27 Correct 3 ms 9564 KB Output is correct
28 Correct 3 ms 9820 KB Output is correct
29 Correct 3 ms 9820 KB Output is correct
30 Correct 4 ms 9820 KB Output is correct
31 Correct 4 ms 9820 KB Output is correct
32 Correct 3 ms 9820 KB Output is correct
33 Correct 3 ms 9820 KB Output is correct
34 Correct 3 ms 9560 KB Output is correct
35 Correct 3 ms 9820 KB Output is correct
36 Correct 2 ms 9820 KB Output is correct
37 Correct 3 ms 9820 KB Output is correct
38 Correct 3 ms 9820 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9564 KB Output is correct
2 Correct 2 ms 9564 KB Output is correct
3 Correct 2 ms 9564 KB Output is correct
4 Correct 2 ms 9564 KB Output is correct
5 Correct 2 ms 9564 KB Output is correct
6 Correct 2 ms 9564 KB Output is correct
7 Correct 2 ms 9564 KB Output is correct
8 Correct 2 ms 9564 KB Output is correct
9 Correct 2 ms 9564 KB Output is correct
10 Correct 2 ms 9564 KB Output is correct
11 Correct 2 ms 9560 KB Output is correct
12 Correct 2 ms 9564 KB Output is correct
13 Correct 2 ms 9564 KB Output is correct
14 Correct 2 ms 9564 KB Output is correct
15 Correct 2 ms 9564 KB Output is correct
16 Correct 2 ms 9564 KB Output is correct
17 Correct 2 ms 9564 KB Output is correct
18 Correct 2 ms 9564 KB Output is correct
19 Correct 118 ms 15636 KB Output is correct
20 Correct 119 ms 15872 KB Output is correct
21 Correct 126 ms 15440 KB Output is correct
22 Correct 128 ms 15700 KB Output is correct
23 Correct 62 ms 15700 KB Output is correct
24 Correct 49 ms 15760 KB Output is correct
25 Correct 85 ms 19128 KB Output is correct
26 Correct 75 ms 22900 KB Output is correct
27 Correct 122 ms 19396 KB Output is correct
28 Correct 133 ms 36788 KB Output is correct
29 Correct 128 ms 35924 KB Output is correct
30 Correct 122 ms 22440 KB Output is correct
31 Correct 126 ms 22352 KB Output is correct
32 Correct 137 ms 22420 KB Output is correct
33 Correct 162 ms 21536 KB Output is correct
34 Correct 79 ms 22180 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9564 KB Output is correct
2 Correct 2 ms 9564 KB Output is correct
3 Correct 2 ms 9564 KB Output is correct
4 Correct 2 ms 9564 KB Output is correct
5 Correct 2 ms 9564 KB Output is correct
6 Correct 2 ms 9564 KB Output is correct
7 Correct 2 ms 9564 KB Output is correct
8 Correct 2 ms 9564 KB Output is correct
9 Correct 2 ms 9564 KB Output is correct
10 Correct 2 ms 9564 KB Output is correct
11 Correct 2 ms 9560 KB Output is correct
12 Correct 2 ms 9564 KB Output is correct
13 Correct 2 ms 9564 KB Output is correct
14 Correct 2 ms 9564 KB Output is correct
15 Correct 2 ms 9564 KB Output is correct
16 Correct 2 ms 9564 KB Output is correct
17 Correct 2 ms 9564 KB Output is correct
18 Correct 2 ms 9564 KB Output is correct
19 Correct 2 ms 9560 KB Output is correct
20 Correct 3 ms 9560 KB Output is correct
21 Correct 3 ms 9564 KB Output is correct
22 Correct 2 ms 9564 KB Output is correct
23 Correct 2 ms 9792 KB Output is correct
24 Correct 3 ms 9804 KB Output is correct
25 Correct 3 ms 9820 KB Output is correct
26 Correct 2 ms 9564 KB Output is correct
27 Correct 3 ms 9564 KB Output is correct
28 Correct 3 ms 9820 KB Output is correct
29 Correct 3 ms 9820 KB Output is correct
30 Correct 4 ms 9820 KB Output is correct
31 Correct 4 ms 9820 KB Output is correct
32 Correct 3 ms 9820 KB Output is correct
33 Correct 3 ms 9820 KB Output is correct
34 Correct 3 ms 9560 KB Output is correct
35 Correct 3 ms 9820 KB Output is correct
36 Correct 2 ms 9820 KB Output is correct
37 Correct 3 ms 9820 KB Output is correct
38 Correct 3 ms 9820 KB Output is correct
39 Correct 118 ms 15636 KB Output is correct
40 Correct 119 ms 15872 KB Output is correct
41 Correct 126 ms 15440 KB Output is correct
42 Correct 128 ms 15700 KB Output is correct
43 Correct 62 ms 15700 KB Output is correct
44 Correct 49 ms 15760 KB Output is correct
45 Correct 85 ms 19128 KB Output is correct
46 Correct 75 ms 22900 KB Output is correct
47 Correct 122 ms 19396 KB Output is correct
48 Correct 133 ms 36788 KB Output is correct
49 Correct 128 ms 35924 KB Output is correct
50 Correct 122 ms 22440 KB Output is correct
51 Correct 126 ms 22352 KB Output is correct
52 Correct 137 ms 22420 KB Output is correct
53 Correct 162 ms 21536 KB Output is correct
54 Correct 79 ms 22180 KB Output is correct
55 Correct 18 ms 10328 KB Output is correct
56 Correct 11 ms 10076 KB Output is correct
57 Correct 105 ms 17080 KB Output is correct
58 Correct 37 ms 16848 KB Output is correct
59 Correct 214 ms 27572 KB Output is correct
60 Correct 583 ms 44572 KB Output is correct
61 Correct 172 ms 22816 KB Output is correct
62 Correct 193 ms 22816 KB Output is correct
63 Correct 223 ms 22664 KB Output is correct
64 Correct 587 ms 27304 KB Output is correct
65 Correct 105 ms 23124 KB Output is correct
66 Correct 360 ms 33512 KB Output is correct
67 Correct 84 ms 23220 KB Output is correct
68 Correct 282 ms 26912 KB Output is correct
69 Correct 277 ms 26704 KB Output is correct
70 Correct 245 ms 26360 KB Output is correct