This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <climits>
#include <cmath>
#include <fstream>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define ll long long
//cin.tie(0);ios_base::sync_with_stdio(0);
const int MAXN = 1e6 + 5;
int curLongest = 0;
bool cent[MAXN];
int s[MAXN];
vector<int> adjList[MAXN];
int n;
void dfs(int src, int par) {
s[src] = 1;
bool bad = false;
for (auto i : adjList[src]) {
if (i == par) {
continue;
}
dfs(i, src);
s[src] += s[i];
if (s[i] > n / 2) {
bad = true;
}
}
if (n - s[src] > n / 2) {
bad = true;
}
cent[src] = !bad;
}
int dfs1(int src, int par, int dist) {
int ret = 2*dist;
curLongest = max(curLongest, dist);
for (auto i : adjList[src]) {
if (i == par) {
continue;
}
ret += dfs1(i, src, dist + 1);
}
return ret;
}
int main() {
cin >> n;
for (int i = 0; i < n - 1; i++) {
int a, b;
cin >> a >> b;
a--; b--;
adjList[a].push_back(b);
adjList[b].push_back(a);
}
dfs(0, 0);
for (int i = 0; i < n; i++) {
if (!cent[i]) {
cout << -1 << endl;
}
else {
cout << dfs1(i, i, 0) - curLongest << endl;
}
curLongest = 0;
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |