Submission #168361

# Submission time Handle Problem Language Result Execution time Memory
168361 2019-12-12T16:52:39 Z Thuleanx Inspection (POI11_ins) C++14
100 / 100
1247 ms 127472 KB
#include <bits/stdc++.h>
using namespace std;

const int N = 1e6+7;
int n;
int sz[N];
vector<int> adj[N];
long long rans[N];
int lp[N];
bool valid[N];

int dfs(int v, int p) {
	sz[v] = 1; lp[v] = 0;
	for (int w : adj[v]) if (w != p) {
		sz[v] += dfs(w,v);
		lp[v] = max(lp[v], lp[w]+1);
	}
	return sz[v];
}

void dfsr(int v, int p, int len) {
	rans[v] = v ? rans[p] + (n - sz[v]) - sz[v] : 0;
	if (!v) for (int i = 1; i < n; i++)
		rans[v] += sz[i];
	int a = -1, b = -1;
	a = len+1;
	int max_sz = 0, llen = 0;
	for (int w : adj[v]) if (w != p) {
		if (sz[w] > max_sz) {
			max_sz = sz[w];
			llen = lp[w]+1;
		}
		int c = lp[w]+2;
		if (c > a) swap(a, c);
		if (c > b) swap(b, c);
		// maintain that a>=b
	}
	if (n-sz[v] > max_sz) {
		max_sz = n-sz[v];
		llen = len;
	}
	for (int w : adj[v]) if (w != p)
		dfsr(w,v, lp[w]+2 == a ? b : a);
	if (max_sz - (n-1-max_sz) > 1)
		rans[v] = -1;
	else if (max_sz - (n - 1 - max_sz) == 1)
		rans[v] = 2*rans[v] - llen;
	else
		rans[v] = 2*rans[v] - max(len, lp[v]);
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	cin>>n;	
	for (int i = 0; i < n-1; i++) {
		int a, b; cin>>a>>b;
		adj[--a].push_back(--b);
		adj[b].push_back(a);
	}
	dfs(0,-1);
	dfsr(0,-1, 0);
	stringstream ss;
	for (int i = 0; i < n; i++)
		ss << rans[i] << endl;
	cout << ss.str();

	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 23 ms 23800 KB Output is correct
2 Correct 23 ms 23800 KB Output is correct
3 Correct 27 ms 23800 KB Output is correct
4 Correct 27 ms 23928 KB Output is correct
5 Correct 23 ms 23800 KB Output is correct
6 Correct 23 ms 23800 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 23800 KB Output is correct
2 Correct 22 ms 23672 KB Output is correct
3 Correct 22 ms 23800 KB Output is correct
4 Correct 22 ms 23800 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 23928 KB Output is correct
2 Correct 23 ms 24056 KB Output is correct
3 Correct 24 ms 24084 KB Output is correct
4 Correct 23 ms 24056 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 35 ms 25080 KB Output is correct
2 Correct 34 ms 25464 KB Output is correct
3 Correct 36 ms 25980 KB Output is correct
4 Correct 34 ms 25080 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 48 ms 26192 KB Output is correct
2 Correct 46 ms 27056 KB Output is correct
3 Correct 47 ms 27896 KB Output is correct
4 Correct 45 ms 26164 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 85 ms 29712 KB Output is correct
2 Correct 84 ms 31108 KB Output is correct
3 Correct 89 ms 32880 KB Output is correct
4 Correct 99 ms 29868 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 590 ms 52568 KB Output is correct
2 Correct 554 ms 64228 KB Output is correct
3 Correct 547 ms 75620 KB Output is correct
4 Correct 490 ms 52488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1226 ms 81092 KB Output is correct
2 Correct 1190 ms 117608 KB Output is correct
3 Correct 1160 ms 127328 KB Output is correct
4 Correct 1038 ms 94708 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1236 ms 81044 KB Output is correct
2 Correct 1168 ms 117772 KB Output is correct
3 Correct 1154 ms 127304 KB Output is correct
4 Correct 1034 ms 94560 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1247 ms 81488 KB Output is correct
2 Correct 1239 ms 117704 KB Output is correct
3 Correct 1192 ms 127472 KB Output is correct
4 Correct 1049 ms 94556 KB Output is correct