Submission #168359

# Submission time Handle Problem Language Result Execution time Memory
168359 2019-12-12T16:38:59 Z Thuleanx Inspection (POI11_ins) C++14
70 / 100
1713 ms 131076 KB
#include <bits/stdc++.h>
using namespace std;

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

int dfs(int v, int p) {
	sz[v] = 1; ans[v] = 0; lp[v] = 0;
	for (int w : adj[v]) if (w != p) {
		sz[v] += dfs(w,v);
		ans[v] += ans[w] + sz[w];
		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] : ans[v];
	vector<int> xp;
	xp.push_back(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;
		}
		xp.push_back(lp[w]+2);
		sort(begin(xp),end(xp),greater<int>());
		while (xp.size() > 2) xp.pop_back();
	}
	if (n-sz[v] > max_sz) {
		max_sz = n-sz[v];
		llen = len;
	}
	valid[v] = max_sz - (n - 1 - max_sz) <= 1;
	dans[v] = max(len, lp[v]);
	if (max_sz - (n - 1 - max_sz) == 1)
		dans[v] = llen;	
	for (int w : adj[v]) if (w != p) {
		int l = xp[0];
		if (xp[0] == lp[w]+2) l = xp[1];
		dfsr(w,v,l);
	}
}

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 << (valid[i] ? 2*rans[i]-dans[i] : -1) << endl;
	cout << ss.str();

	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 24 ms 24056 KB Output is correct
2 Correct 21 ms 23916 KB Output is correct
3 Correct 24 ms 23928 KB Output is correct
4 Correct 23 ms 23800 KB Output is correct
5 Correct 23 ms 23932 KB Output is correct
6 Correct 28 ms 23800 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 26 ms 23928 KB Output is correct
2 Correct 28 ms 23892 KB Output is correct
3 Correct 27 ms 23928 KB Output is correct
4 Correct 28 ms 23928 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 24056 KB Output is correct
2 Correct 25 ms 24184 KB Output is correct
3 Correct 24 ms 24264 KB Output is correct
4 Correct 30 ms 24056 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 38 ms 25632 KB Output is correct
2 Correct 38 ms 26332 KB Output is correct
3 Correct 39 ms 27256 KB Output is correct
4 Correct 39 ms 25720 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 51 ms 27196 KB Output is correct
2 Correct 55 ms 28868 KB Output is correct
3 Correct 70 ms 30584 KB Output is correct
4 Correct 54 ms 27512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 139 ms 32564 KB Output is correct
2 Correct 126 ms 34992 KB Output is correct
3 Correct 113 ms 38036 KB Output is correct
4 Correct 106 ms 32752 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 738 ms 67624 KB Output is correct
2 Correct 801 ms 87064 KB Output is correct
3 Correct 798 ms 109628 KB Output is correct
4 Correct 674 ms 67992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1603 ms 111240 KB Output is correct
2 Runtime error 1287 ms 131076 KB Execution killed with signal 9 (could be triggered by violating memory limits)
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1713 ms 111152 KB Output is correct
2 Runtime error 1292 ms 131076 KB Execution killed with signal 9 (could be triggered by violating memory limits)
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1642 ms 111140 KB Output is correct
2 Runtime error 1363 ms 131072 KB Execution killed with signal 9 (could be triggered by violating memory limits)
3 Halted 0 ms 0 KB -