답안 #527016

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
527016 2022-02-16T19:50:13 Z Hanksburger 경주 (Race) (IOI11_race) C++17
100 / 100
484 ms 51756 KB
#include <bits/stdc++.h>
using namespace std;
long long dp[1000005], sz[200005], n, k, ans=1e18;
vector<pair<long long, long long> > adj[200005], val;
vector<long long> val2, tmp;
void dfs(long long u, long long prev)
{
	sz[u]=1;
	for (long long i=0; i<adj[u].size(); i++)
	{
		long long v=adj[u][i].first;
		if (v==prev)
			continue;
		dfs(v, u);
		sz[u]+=sz[v];
	}
}
void dfs2(long long u, long long prev, long long cnt, long long cur)
{
	if (cur>k)
		return;
	val.push_back({cnt, cur});
	val2.push_back(cur);
	for (long long i=0; i<adj[u].size(); i++)
	{
		long long v=adj[u][i].first, w=adj[u][i].second;
		if (v==prev)
			continue;
		dfs2(v, u, cnt+1, cur+w);
	}
}
void dfs3(long long u, long long prev)
{
	tmp.push_back(u);
	for (long long i=0; i<adj[u].size(); i++)
	{
		long long v=adj[u][i].first;
		if (v==prev)
			continue;
		dfs3(v, u);
	}
}
void recur(vector<long long> vec)
{
//	cout << "vec: ";
//	for (long long i=0; i<vec.size(); i++)
//		cout << vec[i] << ' ';
//	cout << '\n';
	long long m=vec.size(), center;
	if (m==1)
		return;
	dfs(vec[0], -1);
	for (long long i=0; i<m; i++)
	{
		long long u=vec[i];
		long long maxi=m-sz[u];
		for (long long j=0; j<adj[u].size(); j++)
		{
			long long v=adj[u][j].first;
			if (sz[u]>sz[v])
				maxi=max(maxi, sz[v]);
		}
		if (maxi<=m/2)
		{
			center=u;
			break;
		}
	}
//	cout << "center: " << center << '\n';
	for (long long i=0; i<adj[center].size(); i++)
	{
		long long v=adj[center][i].first, w=adj[center][i].second;
		dfs2(v, center, 1, w);
		for (long long j=0; j<val.size(); j++)
			ans=min(ans, dp[k-val[j].second]+val[j].first);
		for (long long j=0; j<val.size(); j++)
			dp[val[j].second]=min(dp[val[j].second], val[j].first);
//		cout << "val: ";
//		for (long long j=0; j<val.size(); j++)
//			cout << val[j].first << ' ' << val[j].second << ' ';
//		cout << '\n';
		val.clear();
//		cout << "dp: ";
//		for (long long j=0; j<=k; j++)
//			cout << dp[j] << ' ';
//		cout << '\n';
//		cout << "ans: " << ans << '\n';
	}
	for (long long i=0; i<val2.size(); i++)
		dp[val2[i]]=1e18;
	val2.clear();
	for (long long i=0; i<adj[center].size(); i++)
	{
		long long v=adj[center][i].first;
		for (long long j=0; j<adj[v].size(); j++)
		{
			if (adj[v][j].first==center)
			{
				adj[v].erase(adj[v].begin()+j);
				break;
			}
		}
		tmp.clear();
		dfs3(v, center);
		recur(tmp);
	}
}
int best_path(int N, int K, int H[][2], int L[])
{
	n=N;
	k=K;
	for (long long i=0; i<=n-2; i++)
	{
		adj[H[i][0]].push_back({H[i][1], L[i]});
		adj[H[i][1]].push_back({H[i][0], L[i]});
	}
	for (long long i=1; i<=k; i++)
		dp[i]=1e18;
	for (long long i=0; i<n; i++)
		tmp.push_back(i);
	recur(tmp);
	if (ans==1e18)
		return -1;
	return ans;
}

Compilation message

race.cpp: In function 'void dfs(long long int, long long int)':
race.cpp:9:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    9 |  for (long long i=0; i<adj[u].size(); i++)
      |                      ~^~~~~~~~~~~~~~
race.cpp: In function 'void dfs2(long long int, long long int, long long int, long long int)':
race.cpp:24:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   24 |  for (long long i=0; i<adj[u].size(); i++)
      |                      ~^~~~~~~~~~~~~~
race.cpp: In function 'void dfs3(long long int, long long int)':
race.cpp:35:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   35 |  for (long long i=0; i<adj[u].size(); i++)
      |                      ~^~~~~~~~~~~~~~
race.cpp: In function 'void recur(std::vector<long long int>)':
race.cpp:57:24: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   57 |   for (long long j=0; j<adj[u].size(); j++)
      |                       ~^~~~~~~~~~~~~~
race.cpp:70:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   70 |  for (long long i=0; i<adj[center].size(); i++)
      |                      ~^~~~~~~~~~~~~~~~~~~
race.cpp:74:24: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   74 |   for (long long j=0; j<val.size(); j++)
      |                       ~^~~~~~~~~~~
race.cpp:76:24: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   76 |   for (long long j=0; j<val.size(); j++)
      |                       ~^~~~~~~~~~~
race.cpp:89:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   89 |  for (long long i=0; i<val2.size(); i++)
      |                      ~^~~~~~~~~~~~
race.cpp:92:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   92 |  for (long long i=0; i<adj[center].size(); i++)
      |                      ~^~~~~~~~~~~~~~~~~~~
race.cpp:95:24: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   95 |   for (long long j=0; j<adj[v].size(); j++)
      |                       ~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 4 ms 5000 KB Output is correct
4 Correct 3 ms 5008 KB Output is correct
5 Correct 3 ms 5008 KB Output is correct
6 Correct 3 ms 4940 KB Output is correct
7 Correct 3 ms 4940 KB Output is correct
8 Correct 3 ms 4940 KB Output is correct
9 Correct 4 ms 5004 KB Output is correct
10 Correct 3 ms 4940 KB Output is correct
11 Correct 3 ms 4992 KB Output is correct
12 Correct 3 ms 4940 KB Output is correct
13 Correct 3 ms 4940 KB Output is correct
14 Correct 4 ms 5060 KB Output is correct
15 Correct 4 ms 5068 KB Output is correct
16 Correct 3 ms 5012 KB Output is correct
17 Correct 3 ms 5012 KB Output is correct
18 Correct 3 ms 4940 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 4 ms 5000 KB Output is correct
4 Correct 3 ms 5008 KB Output is correct
5 Correct 3 ms 5008 KB Output is correct
6 Correct 3 ms 4940 KB Output is correct
7 Correct 3 ms 4940 KB Output is correct
8 Correct 3 ms 4940 KB Output is correct
9 Correct 4 ms 5004 KB Output is correct
10 Correct 3 ms 4940 KB Output is correct
11 Correct 3 ms 4992 KB Output is correct
12 Correct 3 ms 4940 KB Output is correct
13 Correct 3 ms 4940 KB Output is correct
14 Correct 4 ms 5060 KB Output is correct
15 Correct 4 ms 5068 KB Output is correct
16 Correct 3 ms 5012 KB Output is correct
17 Correct 3 ms 5012 KB Output is correct
18 Correct 3 ms 4940 KB Output is correct
19 Correct 2 ms 4940 KB Output is correct
20 Correct 3 ms 4940 KB Output is correct
21 Correct 5 ms 5148 KB Output is correct
22 Correct 7 ms 12320 KB Output is correct
23 Correct 6 ms 10956 KB Output is correct
24 Correct 7 ms 11852 KB Output is correct
25 Correct 7 ms 11724 KB Output is correct
26 Correct 7 ms 7712 KB Output is correct
27 Correct 6 ms 11288 KB Output is correct
28 Correct 4 ms 6604 KB Output is correct
29 Correct 5 ms 7628 KB Output is correct
30 Correct 6 ms 7964 KB Output is correct
31 Correct 8 ms 10272 KB Output is correct
32 Correct 6 ms 10696 KB Output is correct
33 Correct 6 ms 11168 KB Output is correct
34 Correct 5 ms 9676 KB Output is correct
35 Correct 6 ms 11420 KB Output is correct
36 Correct 6 ms 12364 KB Output is correct
37 Correct 6 ms 11340 KB Output is correct
38 Correct 8 ms 9036 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 4 ms 5000 KB Output is correct
4 Correct 3 ms 5008 KB Output is correct
5 Correct 3 ms 5008 KB Output is correct
6 Correct 3 ms 4940 KB Output is correct
7 Correct 3 ms 4940 KB Output is correct
8 Correct 3 ms 4940 KB Output is correct
9 Correct 4 ms 5004 KB Output is correct
10 Correct 3 ms 4940 KB Output is correct
11 Correct 3 ms 4992 KB Output is correct
12 Correct 3 ms 4940 KB Output is correct
13 Correct 3 ms 4940 KB Output is correct
14 Correct 4 ms 5060 KB Output is correct
15 Correct 4 ms 5068 KB Output is correct
16 Correct 3 ms 5012 KB Output is correct
17 Correct 3 ms 5012 KB Output is correct
18 Correct 3 ms 4940 KB Output is correct
19 Correct 141 ms 15872 KB Output is correct
20 Correct 147 ms 15928 KB Output is correct
21 Correct 193 ms 16296 KB Output is correct
22 Correct 137 ms 17296 KB Output is correct
23 Correct 88 ms 15872 KB Output is correct
24 Correct 61 ms 14856 KB Output is correct
25 Correct 178 ms 19432 KB Output is correct
26 Correct 108 ms 24756 KB Output is correct
27 Correct 172 ms 25788 KB Output is correct
28 Correct 294 ms 41604 KB Output is correct
29 Correct 311 ms 40452 KB Output is correct
30 Correct 169 ms 25744 KB Output is correct
31 Correct 210 ms 25896 KB Output is correct
32 Correct 215 ms 25792 KB Output is correct
33 Correct 238 ms 25952 KB Output is correct
34 Correct 261 ms 26116 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 4 ms 5000 KB Output is correct
4 Correct 3 ms 5008 KB Output is correct
5 Correct 3 ms 5008 KB Output is correct
6 Correct 3 ms 4940 KB Output is correct
7 Correct 3 ms 4940 KB Output is correct
8 Correct 3 ms 4940 KB Output is correct
9 Correct 4 ms 5004 KB Output is correct
10 Correct 3 ms 4940 KB Output is correct
11 Correct 3 ms 4992 KB Output is correct
12 Correct 3 ms 4940 KB Output is correct
13 Correct 3 ms 4940 KB Output is correct
14 Correct 4 ms 5060 KB Output is correct
15 Correct 4 ms 5068 KB Output is correct
16 Correct 3 ms 5012 KB Output is correct
17 Correct 3 ms 5012 KB Output is correct
18 Correct 3 ms 4940 KB Output is correct
19 Correct 2 ms 4940 KB Output is correct
20 Correct 3 ms 4940 KB Output is correct
21 Correct 5 ms 5148 KB Output is correct
22 Correct 7 ms 12320 KB Output is correct
23 Correct 6 ms 10956 KB Output is correct
24 Correct 7 ms 11852 KB Output is correct
25 Correct 7 ms 11724 KB Output is correct
26 Correct 7 ms 7712 KB Output is correct
27 Correct 6 ms 11288 KB Output is correct
28 Correct 4 ms 6604 KB Output is correct
29 Correct 5 ms 7628 KB Output is correct
30 Correct 6 ms 7964 KB Output is correct
31 Correct 8 ms 10272 KB Output is correct
32 Correct 6 ms 10696 KB Output is correct
33 Correct 6 ms 11168 KB Output is correct
34 Correct 5 ms 9676 KB Output is correct
35 Correct 6 ms 11420 KB Output is correct
36 Correct 6 ms 12364 KB Output is correct
37 Correct 6 ms 11340 KB Output is correct
38 Correct 8 ms 9036 KB Output is correct
39 Correct 141 ms 15872 KB Output is correct
40 Correct 147 ms 15928 KB Output is correct
41 Correct 193 ms 16296 KB Output is correct
42 Correct 137 ms 17296 KB Output is correct
43 Correct 88 ms 15872 KB Output is correct
44 Correct 61 ms 14856 KB Output is correct
45 Correct 178 ms 19432 KB Output is correct
46 Correct 108 ms 24756 KB Output is correct
47 Correct 172 ms 25788 KB Output is correct
48 Correct 294 ms 41604 KB Output is correct
49 Correct 311 ms 40452 KB Output is correct
50 Correct 169 ms 25744 KB Output is correct
51 Correct 210 ms 25896 KB Output is correct
52 Correct 215 ms 25792 KB Output is correct
53 Correct 238 ms 25952 KB Output is correct
54 Correct 261 ms 26116 KB Output is correct
55 Correct 12 ms 6176 KB Output is correct
56 Correct 12 ms 6220 KB Output is correct
57 Correct 96 ms 17384 KB Output is correct
58 Correct 41 ms 16132 KB Output is correct
59 Correct 108 ms 26220 KB Output is correct
60 Correct 443 ms 51756 KB Output is correct
61 Correct 188 ms 26724 KB Output is correct
62 Correct 213 ms 36628 KB Output is correct
63 Correct 252 ms 36668 KB Output is correct
64 Correct 484 ms 34076 KB Output is correct
65 Correct 271 ms 26816 KB Output is correct
66 Correct 386 ms 45944 KB Output is correct
67 Correct 144 ms 36388 KB Output is correct
68 Correct 245 ms 35092 KB Output is correct
69 Correct 265 ms 34932 KB Output is correct
70 Correct 247 ms 34184 KB Output is correct