답안 #120504

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
120504 2019-06-24T17:17:34 Z popovicirobert Inspection (POI11_ins) C++14
100 / 100
1977 ms 131072 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) {
		if(mx < (n - 1) - mx + 1) {
			sol[nod] = 2LL * sum - max(mx_down, down[nod]);		
		}
		else {
			if(mx == n - sz[nod]) {
				sol[nod] = 2LL * sum - mx_down;
			}
			else {
				for(auto it : g[nod]) {
					if(it != par && sz[it] == mx) {
						sol[nod] = 2LL * sum - (down[it] + 1);
					}
				}
			}
		}
	}
	else {
		sol[nod] = -1;
	}

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

			int cur = -2 * MAXN;
			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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 23808 KB Output is correct
2 Correct 23 ms 23808 KB Output is correct
3 Correct 23 ms 23808 KB Output is correct
4 Correct 23 ms 23808 KB Output is correct
5 Correct 22 ms 23808 KB Output is correct
6 Correct 22 ms 23808 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 23808 KB Output is correct
2 Correct 23 ms 23808 KB Output is correct
3 Correct 28 ms 23808 KB Output is correct
4 Correct 23 ms 23808 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 23936 KB Output is correct
2 Correct 26 ms 24184 KB Output is correct
3 Correct 23 ms 24056 KB Output is correct
4 Correct 22 ms 24064 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 35 ms 25080 KB Output is correct
2 Correct 37 ms 25820 KB Output is correct
3 Correct 37 ms 26616 KB Output is correct
4 Correct 34 ms 25244 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 51 ms 26232 KB Output is correct
2 Correct 50 ms 27896 KB Output is correct
3 Correct 50 ms 29560 KB Output is correct
4 Correct 46 ms 26496 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 100 ms 29560 KB Output is correct
2 Correct 118 ms 31844 KB Output is correct
3 Correct 119 ms 35084 KB Output is correct
4 Correct 113 ms 29880 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 869 ms 50324 KB Output is correct
2 Correct 831 ms 69944 KB Output is correct
3 Correct 792 ms 88956 KB Output is correct
4 Correct 675 ms 50488 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1765 ms 75760 KB Output is correct
2 Correct 1713 ms 114256 KB Output is correct
3 Correct 1677 ms 131072 KB Output is correct
4 Correct 1416 ms 76192 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1977 ms 75752 KB Output is correct
2 Correct 1720 ms 114128 KB Output is correct
3 Correct 1687 ms 131072 KB Output is correct
4 Correct 1392 ms 76148 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1797 ms 75760 KB Output is correct
2 Correct 1911 ms 114252 KB Output is correct
3 Correct 1671 ms 131072 KB Output is correct
4 Correct 1381 ms 76188 KB Output is correct