Submission #44346

# Submission time Handle Problem Language Result Execution time Memory
44346 2018-03-31T13:51:15 Z shash42 Race (IOI11_race) C++11
100 / 100
679 ms 30204 KB
#include<iostream>
#include<vector>
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
using namespace std;
vector< pii > adj[200005];
bool rem[200005];
int K, sub[200005], par[200005], nxt;
int arr[1000005], best=1e9, ans=1e9, inf=1e9;
vector< pii > q;
void dfs1(int u)
{
	sub[u]=1;
	for(pii v: adj[u])
	{
		if(v.first!=par[u] && !rem[v.first])
		{
			par[v.first]=u;
			dfs1(v.first);
			sub[u]+=sub[v.first];
		}
	}
}
void dfs2(int u, int cand)
{
	int currsz=sub[cand]-sub[u];
	for(pii v: adj[u])
	{
		if(v.first!=par[u] && !rem[v.first])
		{
			currsz=max(currsz, sub[v.first]);
			dfs2(v.first, cand);
		}
	}
	if(currsz<best)
	{
		nxt=u;
		best=currsz;
	}
}
void dfs3(int u, int w, int l)
{
	if(w>K) return;
	q.pb(mp(w, l));
	for(pii v: adj[u])
	{
		if(v.first!=par[u] && !rem[v.first])
		{
			par[v.first]=u;
			dfs3(v.first, w+v.second, l+1);	
		}
	}
}
void solve(int cent)
{
	vector<int> tnow;
	tnow.pb(0);
	arr[0]=0;
	for(pii v: adj[cent])
	{
		if(!rem[v.first])
		{
			par[v.first]=cent;
			dfs3(v.first, v.second, 1);
		}
		for(int i = 0; i < q.size(); i++)
		{
			int w=q[i].first, l=q[i].second;
			if(w<=K)
				ans=min(ans, arr[K-w]+l);
		}
		for(int i = 0; i < q.size(); i++)
		{
			arr[q[i].first]=min(arr[q[i].first], q[i].second);
			tnow.pb(q[i].first);
		}
		q.clear();
	}
	for(int i = 0; i < tnow.size(); i++)
		arr[tnow[i]]=inf;
	tnow.clear();
}
void cent(int cand)
{
	nxt=0;
	best=inf;
	par[cand]=cand;
	dfs1(cand);
	dfs2(cand, cand);
	solve(nxt);
	rem[nxt]=true;
	for(pii v: adj[nxt])
		if(!rem[v.first])
			cent(v.first);
}
int best_path(int n, int k, int h[][2], int l[])
{
	K=k;
	for(int i = 1; i <= n; i++)
	{
		adj[i].clear();
		rem[i]=false;
	}
	for(int i = 0; i <= k; i++)
		arr[i]=inf;
	for(int i = 0; i < n-1; i++)
	{
		adj[h[i][0]].pb(mp(h[i][1], l[i]));
		adj[h[i][1]].pb(mp(h[i][0], l[i]));
	}
	cent(1);
	if(ans==inf) ans=-1;
	return ans;
}
/*int main()
{
	int arr[][2]={{0, 1}, {1, 2}};
	int lnt[]={1, 1};
	cout << best_path(11, 12, arr, lnt);
}*/

Compilation message

race.cpp: In function 'void solve(int)':
race.cpp:67:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i = 0; i < q.size(); i++)
                  ~~^~~~~~~~~~
race.cpp:73:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i = 0; i < q.size(); i++)
                  ~~^~~~~~~~~~
race.cpp:80:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0; i < tnow.size(); i++)
                 ~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 4984 KB Output is correct
2 Correct 5 ms 5092 KB Output is correct
3 Correct 6 ms 5168 KB Output is correct
4 Correct 5 ms 5196 KB Output is correct
5 Correct 6 ms 5272 KB Output is correct
6 Correct 5 ms 5304 KB Output is correct
7 Correct 7 ms 5304 KB Output is correct
8 Correct 7 ms 5304 KB Output is correct
9 Correct 6 ms 5304 KB Output is correct
10 Correct 6 ms 5304 KB Output is correct
11 Correct 5 ms 5304 KB Output is correct
12 Correct 7 ms 5304 KB Output is correct
13 Correct 7 ms 5304 KB Output is correct
14 Correct 6 ms 5304 KB Output is correct
15 Correct 6 ms 5304 KB Output is correct
16 Correct 6 ms 5304 KB Output is correct
17 Correct 7 ms 5304 KB Output is correct
18 Correct 6 ms 5304 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 4984 KB Output is correct
2 Correct 5 ms 5092 KB Output is correct
3 Correct 6 ms 5168 KB Output is correct
4 Correct 5 ms 5196 KB Output is correct
5 Correct 6 ms 5272 KB Output is correct
6 Correct 5 ms 5304 KB Output is correct
7 Correct 7 ms 5304 KB Output is correct
8 Correct 7 ms 5304 KB Output is correct
9 Correct 6 ms 5304 KB Output is correct
10 Correct 6 ms 5304 KB Output is correct
11 Correct 5 ms 5304 KB Output is correct
12 Correct 7 ms 5304 KB Output is correct
13 Correct 7 ms 5304 KB Output is correct
14 Correct 6 ms 5304 KB Output is correct
15 Correct 6 ms 5304 KB Output is correct
16 Correct 6 ms 5304 KB Output is correct
17 Correct 7 ms 5304 KB Output is correct
18 Correct 6 ms 5304 KB Output is correct
19 Correct 7 ms 5304 KB Output is correct
20 Correct 5 ms 5304 KB Output is correct
21 Correct 6 ms 5372 KB Output is correct
22 Correct 9 ms 8956 KB Output is correct
23 Correct 9 ms 8956 KB Output is correct
24 Correct 9 ms 8956 KB Output is correct
25 Correct 9 ms 8956 KB Output is correct
26 Correct 10 ms 8956 KB Output is correct
27 Correct 8 ms 8956 KB Output is correct
28 Correct 8 ms 8956 KB Output is correct
29 Correct 8 ms 8956 KB Output is correct
30 Correct 9 ms 8956 KB Output is correct
31 Correct 9 ms 8956 KB Output is correct
32 Correct 9 ms 8956 KB Output is correct
33 Correct 9 ms 8956 KB Output is correct
34 Correct 8 ms 8956 KB Output is correct
35 Correct 9 ms 8956 KB Output is correct
36 Correct 11 ms 8956 KB Output is correct
37 Correct 11 ms 8956 KB Output is correct
38 Correct 9 ms 8956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 4984 KB Output is correct
2 Correct 5 ms 5092 KB Output is correct
3 Correct 6 ms 5168 KB Output is correct
4 Correct 5 ms 5196 KB Output is correct
5 Correct 6 ms 5272 KB Output is correct
6 Correct 5 ms 5304 KB Output is correct
7 Correct 7 ms 5304 KB Output is correct
8 Correct 7 ms 5304 KB Output is correct
9 Correct 6 ms 5304 KB Output is correct
10 Correct 6 ms 5304 KB Output is correct
11 Correct 5 ms 5304 KB Output is correct
12 Correct 7 ms 5304 KB Output is correct
13 Correct 7 ms 5304 KB Output is correct
14 Correct 6 ms 5304 KB Output is correct
15 Correct 6 ms 5304 KB Output is correct
16 Correct 6 ms 5304 KB Output is correct
17 Correct 7 ms 5304 KB Output is correct
18 Correct 6 ms 5304 KB Output is correct
19 Correct 316 ms 11216 KB Output is correct
20 Correct 268 ms 11260 KB Output is correct
21 Correct 221 ms 11644 KB Output is correct
22 Correct 192 ms 12024 KB Output is correct
23 Correct 149 ms 12024 KB Output is correct
24 Correct 126 ms 12024 KB Output is correct
25 Correct 221 ms 16124 KB Output is correct
26 Correct 160 ms 17912 KB Output is correct
27 Correct 249 ms 17912 KB Output is correct
28 Correct 679 ms 27388 KB Output is correct
29 Correct 497 ms 27388 KB Output is correct
30 Correct 229 ms 27388 KB Output is correct
31 Correct 223 ms 27388 KB Output is correct
32 Correct 285 ms 27388 KB Output is correct
33 Correct 375 ms 27388 KB Output is correct
34 Correct 347 ms 27388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 4984 KB Output is correct
2 Correct 5 ms 5092 KB Output is correct
3 Correct 6 ms 5168 KB Output is correct
4 Correct 5 ms 5196 KB Output is correct
5 Correct 6 ms 5272 KB Output is correct
6 Correct 5 ms 5304 KB Output is correct
7 Correct 7 ms 5304 KB Output is correct
8 Correct 7 ms 5304 KB Output is correct
9 Correct 6 ms 5304 KB Output is correct
10 Correct 6 ms 5304 KB Output is correct
11 Correct 5 ms 5304 KB Output is correct
12 Correct 7 ms 5304 KB Output is correct
13 Correct 7 ms 5304 KB Output is correct
14 Correct 6 ms 5304 KB Output is correct
15 Correct 6 ms 5304 KB Output is correct
16 Correct 6 ms 5304 KB Output is correct
17 Correct 7 ms 5304 KB Output is correct
18 Correct 6 ms 5304 KB Output is correct
19 Correct 7 ms 5304 KB Output is correct
20 Correct 5 ms 5304 KB Output is correct
21 Correct 6 ms 5372 KB Output is correct
22 Correct 9 ms 8956 KB Output is correct
23 Correct 9 ms 8956 KB Output is correct
24 Correct 9 ms 8956 KB Output is correct
25 Correct 9 ms 8956 KB Output is correct
26 Correct 10 ms 8956 KB Output is correct
27 Correct 8 ms 8956 KB Output is correct
28 Correct 8 ms 8956 KB Output is correct
29 Correct 8 ms 8956 KB Output is correct
30 Correct 9 ms 8956 KB Output is correct
31 Correct 9 ms 8956 KB Output is correct
32 Correct 9 ms 8956 KB Output is correct
33 Correct 9 ms 8956 KB Output is correct
34 Correct 8 ms 8956 KB Output is correct
35 Correct 9 ms 8956 KB Output is correct
36 Correct 11 ms 8956 KB Output is correct
37 Correct 11 ms 8956 KB Output is correct
38 Correct 9 ms 8956 KB Output is correct
39 Correct 316 ms 11216 KB Output is correct
40 Correct 268 ms 11260 KB Output is correct
41 Correct 221 ms 11644 KB Output is correct
42 Correct 192 ms 12024 KB Output is correct
43 Correct 149 ms 12024 KB Output is correct
44 Correct 126 ms 12024 KB Output is correct
45 Correct 221 ms 16124 KB Output is correct
46 Correct 160 ms 17912 KB Output is correct
47 Correct 249 ms 17912 KB Output is correct
48 Correct 679 ms 27388 KB Output is correct
49 Correct 497 ms 27388 KB Output is correct
50 Correct 229 ms 27388 KB Output is correct
51 Correct 223 ms 27388 KB Output is correct
52 Correct 285 ms 27388 KB Output is correct
53 Correct 375 ms 27388 KB Output is correct
54 Correct 347 ms 27388 KB Output is correct
55 Correct 18 ms 27388 KB Output is correct
56 Correct 19 ms 27388 KB Output is correct
57 Correct 131 ms 27388 KB Output is correct
58 Correct 52 ms 27388 KB Output is correct
59 Correct 141 ms 27388 KB Output is correct
60 Correct 658 ms 29700 KB Output is correct
61 Correct 230 ms 29700 KB Output is correct
62 Correct 248 ms 29700 KB Output is correct
63 Correct 354 ms 29700 KB Output is correct
64 Correct 583 ms 29700 KB Output is correct
65 Correct 320 ms 29700 KB Output is correct
66 Correct 542 ms 30204 KB Output is correct
67 Correct 160 ms 30204 KB Output is correct
68 Correct 318 ms 30204 KB Output is correct
69 Correct 350 ms 30204 KB Output is correct
70 Correct 289 ms 30204 KB Output is correct