Submission #120495

# Submission time Handle Problem Language Result Execution time Memory
120495 2019-06-24T17:06:14 Z popovicirobert Inspection (POI11_ins) C++14
0 / 100
1789 ms 88504 KB
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define lsb(x) (x & (-x)) 

using namespace std;

const int MAXN = (int) 1e6;

vector <int> g[MAXN + 1];
int down[MAXN + 1], sz[MAXN + 1];

void dfs(int nod, int par, ll &sum) {
	sz[nod] = 1;
	for(auto it : g[nod]) {
		if(it != par) {
			dfs(it, nod, sum);
			down[nod] = max(down[nod], down[it] + 1);
			sz[nod] += sz[it];
			sum += sz[it];
		}
	}
}

ll sol[MAXN + 1];
int n;

void solve(int nod, int par, ll sum, int mx_down) {

	int mx = n - sz[nod];
	multiset <int> mst;
	for(auto it : g[nod]) {
		if(it != par) {
			mx = max(mx, sz[it]);
			mst.insert(down[it]);
		}
	}

	if(mx <= (n - 1) - mx + 1) {
		sol[nod] = 2LL * sum - max(mx_down, down[nod]);		
		//cerr << sum << " " << mx_down << " " << down[nod] << "\n";
	}
	else {
		sol[nod] = -1;
	}

	for(auto it : g[nod]) {
		if(it != par) {
			mst.erase(mst.find(down[it]));

			int cur = 0;
			if(mst.size()) {
				cur = *prev(mst.end());
			}

			solve(it, nod, sum - 2LL * sz[it] + n, max(mx_down + 1, cur + 2));
			mst.insert(down[it]);
		}
	}
}

int main() {
	//ifstream cin("A.in");
	//ofstream cout("A.out");
	int i; 
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	
	cin >> n;

	for(i = 1; i < n; i++) {
		int x, y;
		cin >> x >> y;
		g[x].push_back(y);
		g[y].push_back(x);
	}

	ll sum = 0;
	dfs(1, 0, sum);
	solve(1, 0, sum, 0);
	
	for(i = 1; i <= n; i++) {
		cout << sol[i] << "\n";
	}

	return 0;
}
# Verdict Execution time Memory Grader output
1 Incorrect 23 ms 23808 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 22 ms 23808 KB Output is correct
2 Incorrect 21 ms 23928 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 24 ms 24032 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 34 ms 25080 KB Output is correct
2 Incorrect 34 ms 25864 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 52 ms 26360 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 131 ms 30192 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 837 ms 56124 KB Output is correct
2 Incorrect 781 ms 75584 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1768 ms 88392 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1764 ms 88504 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1789 ms 88408 KB Output isn't correct
2 Halted 0 ms 0 KB -