답안 #101203

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
101203 2019-03-17T13:12:39 Z davitmarg 경주 (Race) (IOI11_race) C++17
0 / 100
6 ms 4992 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;
map<LL, int> dp;
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[v]>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 (dp[sum] == 0)
		dp[sum] = mod;
	if (k > sum && dp[k - sum] == 0)
		dp[k - sum] = mod;
	upd.PB(MP(depth, sum));

	if (sum == k)
		ans = min(ans, depth);
	else if (sum < k)
		ans = min(ans, depth + dp[k - 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)
{
	dp.clear();
	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();
		for (int j = 0; j < upd.size(); j++)
			dp[upd[j].second] = min(dp[upd[j].second], upd[j].first);
		nxt.PB(to);
		dfs(to, D, 1);
	}
	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:38: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:62:16: 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:90: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:105:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0; i < g[v].size(); i++)
                  ~~^~~~~~~~~~~~~
race.cpp:113:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int j = 0; j < upd.size(); j++)
                   ~~^~~~~~~~~~~~
race.cpp:118: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:74:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^
# 결과 실행 시간 메모리 Grader output
1 Incorrect 6 ms 4992 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 6 ms 4992 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 6 ms 4992 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 6 ms 4992 KB Output isn't correct
2 Halted 0 ms 0 KB -