Submission #499562

#TimeUsernameProblemLanguageResultExecution timeMemory
499562iwantinTraffic (IOI10_traffic)C++17
0 / 100
29 ms55244 KiB
#include <bits/stdc++.h>
using namespace std;

#define M_PI 3.14159265358979323846 
#define pb push_back
#define fs first
#define sc second

typedef long long ll;
typedef long double ld;

const ll mod = 1e9 + 7;
const ll maxn = 1e6 + 1;
const ll inf = 	LLONG_MAX;
const ll minf = LLONG_MIN; 

vector <ll> cost(maxn), dist(maxn),
children(maxn), ans(maxn);
vector <vector <ll> > g(maxn);
vector <bool> used(maxn);
ll n;

ll findChildren(ll v){
	used[v] = true;
	children[v] = 1;

	for(auto i: g[v])
		if(!used[i])
			children[v] += findChildren(i);

	return children[v];
}

ll findDist(ll v){
	used[v] = true;
	ll isZero = !(v == 0), num = 0;  

	ans[0] += cost[v] * children[v] * isZero;

	for(auto i: g[v])
		if(!used[i])
			num += findDist(i);

	dist[v] = num + cost[v] * children[v] * isZero;
	
	return dist[v];
}

void findAns(ll v, ll parent = -1){
	used[v] = true;

	if(parent != -1){
		ans[v] = cost[parent] * (n - children[parent] + 1) + ans[parent];
		ans[v] -= cost[v] * (children[v]); 	  
	}  

	for(auto i: g[v])
		if(!used[i])
			findAns(i, v);

}

int LocateCentre (int n, int p[], int d[], int s[]){
	
	ios_base :: sync_with_stdio(false);
	cin.tie(NULL);

	cin>>n;

	for(ll k = 0; k < n; k++)
		cost[k] = p[k];

	for(ll k = 0; k < n - 1; k++){
		ll a, b; a = s[k], b = d[k];
		g[a].pb(b);
		g[b].pb(a);
	} 

	findChildren(0); 

	for(ll k = 0; k < n; k++)
		used[k] = false;
	
	findDist(0);

	for(ll k = 0; k < n; k++)
		used[k] = false;
	
	findAns(0);
	
	ll mn = LLONG_MAX;
	int ansk = 0;

	for(int k = 0; k < n; k++){
		if(ans[k] < mn){
			mn = ans[k];
			ansk = k;
		}
	}

	return ansk;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...