// compile: make data
// run: ./data < data.in
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#ifdef LOCAL
#include <debug/codeforces.h>
#define debug(x...) _debug_print(#x, x);
#define Debug(x...) _debug_print_format(#x, x);
#else
#define debug(x...)
#define Debug(x...)
#endif
template<typename...Args> void print_(Args...args){((cout<<args<<" "),...)<<endl;}
#define VI vector<int>
#define VII vector<vector<int>>
#define VIII vector<vector<vector<int>>>
#define rep(i,a,b) for(int i=(a);i<(int)(b);++i)
#define sz(v) ((int)(v).size())
#define print(...) print_(__VA_ARGS__);
#define FIND(a, x) ((find(a.begin(),a.end(),(x))!=a.end())?1:0)
#define cmin(x,...) x=min({(x), __VA_ARGS__})
#define cmax(x,...) x=max({(x), __VA_ARGS__})
#define INTMAX (int)(9223372036854775807)
#define INF (int)(1152921504606846976)
#define NaN (int)(0x8b88e1d0595d51d1)
#define double long double
#define int long long
#define MAXN 200010
struct tree {
int n;
vector<vector<int>> e;
struct node {
vector<node*> ch;
node* parent = nullptr;
int size;
};
vector<node> vertex;
node *root;
tree(int V) {
n = V;
e.resize(n);
vertex.resize(n);
}
void add_edge(int u, int v) {
e[u].push_back(v), e[v].push_back(u);
}
int ind(node *x) {return x - &vertex[0]; }
void build(int rt = 0) {
function<void(int, int)> dfs = [&](int u, int pre) {
vertex[u].size = 1;
for (int v: e[u]) {
if (v == pre) continue;
vertex[u].ch.push_back(&vertex[v]);
vertex[v].parent = &vertex[u];
dfs(v, u);
vertex[u].size += vertex[v].size;
}
};
root = &vertex[rt];
dfs(rt, -1);
}
vector<int> ssp(int s) {
vector<int> vis(n, 0), dis(n, INF);
dis[s] = 0;
function<void(int)> dfs = [&](int u) -> void {
vis[u] = 1;
for (auto v: e[u]) {
if (vis[v]) continue;
dis[v] = dis[u] + 1;
dfs(v);
}
};
dfs(s);
return dis;
}
void graphviz_dump(int plus = 0, string filename = "graph.dot") {
FILE *f = fopen(filename.c_str(), "w");
fprintf(f, "digraph Tree {\n");
rep(i, 0, n) {
node *x = &vertex[i];
fprintf(f, " %lld;\n");
}
rep(i, 0, n) {
node *x = &vertex[i];
for (auto y: x->ch) {
fprintf(f, " %ld -> %ld;\n", x - &vertex[0], y - &vertex[0]);
}
}
fprintf(f, "}\n");
}
void solve() {
vector<int> ans(n, 0);
function<void(node*)> dfs = [&](node *x) {
if (x != root) ans[ind(x)] = ans[ind(x->parent)] + n - 2 * x->size;
for (auto y: x->ch) dfs(y);
};
auto dis = ssp(ind(root));
ans[ind(root)] = accumulate(dis.begin(), dis.end(), 0LL);
dfs(root);
rep(i, 0, n) cout << ans[i] << " \n"[i == n-1];
}
};
int32_t main() {
ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
int n; cin >> n;
tree g(n);
rep(i, 0, n-1) {
int u, v; cin >> u >> v; --u, --v;
g.add_edge(u, v);
}
g.build(0);
// g.graphviz_dump(1);
g.solve();
return 0;
}
Hey, just checking in to say bong88999 is actually pretty decent. The customer service is surprisingly responsive, which I appreciate. Give them a look, might surprise you: bong88999
H2betapp is another solid mobile option. I like the interface and the variety of available choices. Perfect for gaming on the move! h2betapp
Yo, from what I hear, good88vn is the spot for Vietnamese players. If you’re looking for a local flavor, this might be your jam. Check it out, bros: good88vn
I’ve been using this platform for a while now and the interface is super smooth. Definitely one of the best options available right now. Check out krbetwinnerapp for a great experience.
Finally found a site that actually pays out on time. The variety of games is impressive and keeps me entertained for hours. pklucky71
The VIP treatment here is real. I feel valued as a player and the bonuses are actually worth it. Join 989betvip
Xpgamesbr, galera! This one’s got a cool vibe. Lots of Brazilian-themed games, which is awesome! Give it your feedback xpgamesbr
Really liking the interface and how easy it is to deposit. It makes the whole gaming experience so much better. Give bd289bet a go!
Best betting experience I’ve had in a while. The odds are competitive and the customer support team is actually helpful. Definitely sticking with br76xbet from now on.