답안 #101218

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
101218 2019-03-17T14:29:56 Z davitmarg 경주 (Race) (IOI11_race) C++17
100 / 100
1096 ms 36956 KB
/*DavitMarg*/
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <iterator>
#include <ctype.h>
#include <stdlib.h>  
#include <cassert>
#include <fstream>

#ifndef death
#include "race.h"
#endif

#define mod 1000000007ll
#define LL long long
#define LD long double
#define MP make_pair
#define PB push_back
#define all(v) v.begin(),v.end()
using namespace std;

int n, k, num[200005], used[200005], usedNum[200005], usedC[200005], wasC[200005], color = 1, cColor = 1, numColor = 1, ans = mod;
vector<pair<int, LL>> g[200005], upd;
vector<int> cl;
int dp[2000006];
void getNum(int v)
{
	usedNum[v] = numColor;
	num[v] = 1;
	for (int i = 0; i < g[v].size(); i++)
	{
		int to = g[v][i].first;
		if (usedNum[to] == numColor || wasC[to])
			continue;
		getNum(to);
		num[v] += num[to];
	}
}

int centroid(int v)
{
	numColor++;
	cColor++;
	getNum(v);
	int st = v;
	queue<int> q;
	q.push(v);
	while (!q.empty())
	{
		v = q.front();
		q.pop();
		usedC[v] = cColor;
		bool is = 1;
		for (int i = 0; i<g[v].size(); i++)
			if (num[g[v][i].first]>num[st] / 2 && usedC[g[v][i].first] != cColor && !wasC[g[v][i].first])
			{
				is = 0;
				q.push(g[v][i].first);
			}
		if (is)
		{
			wasC[v] = 1;
			return v;
		}
	}
}

void dfs(int v, LL sum, int depth)
{
	used[v] = color;

	if (sum <= k && dp[sum] == 0)
		dp[sum] = mod;
	if (k > sum && dp[k - sum] == 0)
		dp[k - sum] = mod;

	if (sum == k)
		ans = min(ans, depth);
	else if (sum < k)
		ans = min(ans, depth + dp[k - sum]);
	else
		return;

	cl.PB(sum);
	upd.PB(MP(depth, sum));

	for (int i = 0; i < g[v].size(); i++)
	{
		int to = g[v][i].first;
		LL D = g[v][i].second;
		if (used[to] == color || wasC[to])
			continue;
		dfs(to, sum + D, depth + 1);
	}
}

void solve(int v)
{
	color++;
	vector<int> nxt;
	for (int i = 0; i < g[v].size(); i++)
	{
		int to = g[v][i].first;
		LL D = g[v][i].second;
		if (used[to] == color || wasC[to])
			continue;

		upd.clear();
		dfs(to, D, 1);
		for (int j = 0; j < upd.size(); j++)
			dp[upd[j].second] = min(dp[upd[j].second], upd[j].first);
		nxt.PB(to);
	}
	while (!cl.empty())
	{
		dp[cl.back()] = 0;
		cl.pop_back();
	}
	for (int i = 0; i < nxt.size(); i++)
		solve(centroid(nxt[i]));
}

int Main()
{
	solve(centroid(1));
	if (ans == mod)
		ans = -1;
	return 0;
}

int best_path(int N, int K, int h[][2], int l[])
{
	n = N;
	k = K;
	for (int i = 0; i < n - 1; i++)
	{
		h[i][0]++;
		h[i][1]++;
		g[h[i][0]].PB(MP(h[i][1], l[i]));
		g[h[i][1]].PB(MP(h[i][0], l[i]));
	}
	Main();
	return ans;
}

#ifdef death
int main()
{
	cin >> n >> k;

	for (int i = 0; i < n - 1; i++)
	{
		int a, b;
		LL D;
		cin >> a >> b >> D;
		/*a = i;
		b = a + 1;
		D = rand() % 20;*/
		a++;
		b++;
		g[a].PB(MP(b, D));
		g[b].PB(MP(a, D));
	}
	Main();
	cout << ans << endl;
	return 0;
}
#endif

/*

*/

Compilation message

race.cpp: In function 'void getNum(int)':
race.cpp:39:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0; i < g[v].size(); i++)
                  ~~^~~~~~~~~~~~~
race.cpp: In function 'int centroid(int)':
race.cpp:63:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int i = 0; i<g[v].size(); i++)
                   ~^~~~~~~~~~~~
race.cpp: In function 'void dfs(int, long long int, int)':
race.cpp:96:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0; i < g[v].size(); i++)
                  ~~^~~~~~~~~~~~~
race.cpp: In function 'void solve(int)':
race.cpp:110:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0; i < g[v].size(); i++)
                  ~~^~~~~~~~~~~~~
race.cpp:119:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int j = 0; j < upd.size(); j++)
                   ~~^~~~~~~~~~~~
race.cpp:128:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0; i < nxt.size(); i++)
                  ~~^~~~~~~~~~~~
race.cpp: In function 'int centroid(int)':
race.cpp:75:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 4992 KB Output is correct
2 Correct 8 ms 5120 KB Output is correct
3 Correct 6 ms 5120 KB Output is correct
4 Correct 7 ms 4992 KB Output is correct
5 Correct 8 ms 5120 KB Output is correct
6 Correct 7 ms 5120 KB Output is correct
7 Correct 8 ms 5080 KB Output is correct
8 Correct 7 ms 5092 KB Output is correct
9 Correct 6 ms 5120 KB Output is correct
10 Correct 6 ms 4992 KB Output is correct
11 Correct 7 ms 5120 KB Output is correct
12 Correct 0 ms 4992 KB Output is correct
13 Correct 7 ms 5120 KB Output is correct
14 Correct 7 ms 5120 KB Output is correct
15 Correct 6 ms 5120 KB Output is correct
16 Correct 7 ms 5120 KB Output is correct
17 Correct 8 ms 5120 KB Output is correct
18 Correct 7 ms 5120 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 4992 KB Output is correct
2 Correct 8 ms 5120 KB Output is correct
3 Correct 6 ms 5120 KB Output is correct
4 Correct 7 ms 4992 KB Output is correct
5 Correct 8 ms 5120 KB Output is correct
6 Correct 7 ms 5120 KB Output is correct
7 Correct 8 ms 5080 KB Output is correct
8 Correct 7 ms 5092 KB Output is correct
9 Correct 6 ms 5120 KB Output is correct
10 Correct 6 ms 4992 KB Output is correct
11 Correct 7 ms 5120 KB Output is correct
12 Correct 0 ms 4992 KB Output is correct
13 Correct 7 ms 5120 KB Output is correct
14 Correct 7 ms 5120 KB Output is correct
15 Correct 6 ms 5120 KB Output is correct
16 Correct 7 ms 5120 KB Output is correct
17 Correct 8 ms 5120 KB Output is correct
18 Correct 7 ms 5120 KB Output is correct
19 Correct 7 ms 4992 KB Output is correct
20 Correct 8 ms 5120 KB Output is correct
21 Correct 9 ms 5120 KB Output is correct
22 Correct 12 ms 8448 KB Output is correct
23 Correct 13 ms 7936 KB Output is correct
24 Correct 12 ms 8448 KB Output is correct
25 Correct 11 ms 8476 KB Output is correct
26 Correct 10 ms 6400 KB Output is correct
27 Correct 8 ms 5120 KB Output is correct
28 Correct 10 ms 5888 KB Output is correct
29 Correct 10 ms 6400 KB Output is correct
30 Correct 9 ms 6528 KB Output is correct
31 Correct 13 ms 7680 KB Output is correct
32 Correct 12 ms 7936 KB Output is correct
33 Correct 12 ms 8192 KB Output is correct
34 Correct 12 ms 7424 KB Output is correct
35 Correct 6 ms 8420 KB Output is correct
36 Correct 15 ms 8832 KB Output is correct
37 Correct 14 ms 8192 KB Output is correct
38 Correct 12 ms 7296 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 4992 KB Output is correct
2 Correct 8 ms 5120 KB Output is correct
3 Correct 6 ms 5120 KB Output is correct
4 Correct 7 ms 4992 KB Output is correct
5 Correct 8 ms 5120 KB Output is correct
6 Correct 7 ms 5120 KB Output is correct
7 Correct 8 ms 5080 KB Output is correct
8 Correct 7 ms 5092 KB Output is correct
9 Correct 6 ms 5120 KB Output is correct
10 Correct 6 ms 4992 KB Output is correct
11 Correct 7 ms 5120 KB Output is correct
12 Correct 0 ms 4992 KB Output is correct
13 Correct 7 ms 5120 KB Output is correct
14 Correct 7 ms 5120 KB Output is correct
15 Correct 6 ms 5120 KB Output is correct
16 Correct 7 ms 5120 KB Output is correct
17 Correct 8 ms 5120 KB Output is correct
18 Correct 7 ms 5120 KB Output is correct
19 Correct 292 ms 13612 KB Output is correct
20 Correct 320 ms 13560 KB Output is correct
21 Correct 314 ms 14068 KB Output is correct
22 Correct 375 ms 14412 KB Output is correct
23 Correct 259 ms 13816 KB Output is correct
24 Correct 187 ms 13048 KB Output is correct
25 Correct 343 ms 15500 KB Output is correct
26 Correct 203 ms 19316 KB Output is correct
27 Correct 357 ms 22228 KB Output is correct
28 Correct 766 ms 30072 KB Output is correct
29 Correct 704 ms 29432 KB Output is correct
30 Correct 298 ms 22264 KB Output is correct
31 Correct 347 ms 22192 KB Output is correct
32 Correct 431 ms 22092 KB Output is correct
33 Correct 550 ms 20856 KB Output is correct
34 Correct 565 ms 20828 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 4992 KB Output is correct
2 Correct 8 ms 5120 KB Output is correct
3 Correct 6 ms 5120 KB Output is correct
4 Correct 7 ms 4992 KB Output is correct
5 Correct 8 ms 5120 KB Output is correct
6 Correct 7 ms 5120 KB Output is correct
7 Correct 8 ms 5080 KB Output is correct
8 Correct 7 ms 5092 KB Output is correct
9 Correct 6 ms 5120 KB Output is correct
10 Correct 6 ms 4992 KB Output is correct
11 Correct 7 ms 5120 KB Output is correct
12 Correct 0 ms 4992 KB Output is correct
13 Correct 7 ms 5120 KB Output is correct
14 Correct 7 ms 5120 KB Output is correct
15 Correct 6 ms 5120 KB Output is correct
16 Correct 7 ms 5120 KB Output is correct
17 Correct 8 ms 5120 KB Output is correct
18 Correct 7 ms 5120 KB Output is correct
19 Correct 7 ms 4992 KB Output is correct
20 Correct 8 ms 5120 KB Output is correct
21 Correct 9 ms 5120 KB Output is correct
22 Correct 12 ms 8448 KB Output is correct
23 Correct 13 ms 7936 KB Output is correct
24 Correct 12 ms 8448 KB Output is correct
25 Correct 11 ms 8476 KB Output is correct
26 Correct 10 ms 6400 KB Output is correct
27 Correct 8 ms 5120 KB Output is correct
28 Correct 10 ms 5888 KB Output is correct
29 Correct 10 ms 6400 KB Output is correct
30 Correct 9 ms 6528 KB Output is correct
31 Correct 13 ms 7680 KB Output is correct
32 Correct 12 ms 7936 KB Output is correct
33 Correct 12 ms 8192 KB Output is correct
34 Correct 12 ms 7424 KB Output is correct
35 Correct 6 ms 8420 KB Output is correct
36 Correct 15 ms 8832 KB Output is correct
37 Correct 14 ms 8192 KB Output is correct
38 Correct 12 ms 7296 KB Output is correct
39 Correct 292 ms 13612 KB Output is correct
40 Correct 320 ms 13560 KB Output is correct
41 Correct 314 ms 14068 KB Output is correct
42 Correct 375 ms 14412 KB Output is correct
43 Correct 259 ms 13816 KB Output is correct
44 Correct 187 ms 13048 KB Output is correct
45 Correct 343 ms 15500 KB Output is correct
46 Correct 203 ms 19316 KB Output is correct
47 Correct 357 ms 22228 KB Output is correct
48 Correct 766 ms 30072 KB Output is correct
49 Correct 704 ms 29432 KB Output is correct
50 Correct 298 ms 22264 KB Output is correct
51 Correct 347 ms 22192 KB Output is correct
52 Correct 431 ms 22092 KB Output is correct
53 Correct 550 ms 20856 KB Output is correct
54 Correct 565 ms 20828 KB Output is correct
55 Correct 20 ms 6088 KB Output is correct
56 Correct 23 ms 6144 KB Output is correct
57 Correct 144 ms 15084 KB Output is correct
58 Correct 79 ms 14180 KB Output is correct
59 Correct 212 ms 20020 KB Output is correct
60 Correct 1096 ms 36956 KB Output is correct
61 Correct 346 ms 25320 KB Output is correct
62 Correct 368 ms 26012 KB Output is correct
63 Correct 508 ms 25940 KB Output is correct
64 Correct 955 ms 30292 KB Output is correct
65 Correct 513 ms 25464 KB Output is correct
66 Correct 933 ms 35208 KB Output is correct
67 Correct 358 ms 26944 KB Output is correct
68 Correct 581 ms 28292 KB Output is correct
69 Correct 566 ms 28324 KB Output is correct
70 Correct 508 ms 27268 KB Output is correct