CARVIEW |
Select Language
HTTP/2 200
date: Sun, 12 Oct 2025 05:15:36 GMT
content-type: text/html; charset=UTF-8
server: cloudflare
x-frame-options: DENY
x-content-type-options: nosniff
x-xss-protection: 1;mode=block
vary: accept-encoding
cf-cache-status: DYNAMIC
content-encoding: gzip
set-cookie: _csrf-frontend=b0ee9b8abf9a990b2976830849d774d06c0eec18d199dc74441532667c3521eba%3A2%3A%7Bi%3A0%3Bs%3A14%3A%22_csrf-frontend%22%3Bi%3A1%3Bs%3A32%3A%22-LHcZNQdX80C5HnjonsCwEGPsWozrc1x%22%3B%7D; HttpOnly; Path=/
cf-ray: 98d41ecbcb797fa8-BLR
Tree Distances II - Pastebin.com
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define F first
- #define S second
- #define all(x) x.begin(),x.end()
- #define endl '\n'
- using namespace std;
- using ll = long long;
- using pii = pair<int, int>;
- const int INF = 0x3f3f3f3f;
- const ll INFLL = 0x3f3f3f3f3f3f3f3fLL;
- const int MOD = 1000000007;
- const int MAXN = 200010;
- vector<int> adj[MAXN];
- ll down[MAXN], up[MAXN], sz[MAXN];
- void dfs1(int u, int p){
- sz[u] = 1;
- down[u] = 0;
- for(int to: adj[u]){
- if(to == p) continue;
- dfs1(to, u);
- sz[u] += sz[to];
- down[u] += (down[to] + sz[to]);
- }
- }
- int n;
- void dfs2(int u, int p){
- for(int to: adj[u]){
- if(to == p) continue;
- ll aux = down[u] - (sz[to] + down[to]) + up[u];
- up[to] = (aux + n-sz[to]);
- dfs2(to, u);
- }
- }
- 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);
- }
- dfs1(1, 0);
- dfs2(1, 0);
- for(int i=1; i<=n; i++)
- cout << (down[i] + up[i]) << " ";
- cout << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
-
⭐⭐⭐Exchange Exploit⭐⭐
Java | 2 sec ago | 0.10 KB
-
📝 EASY MONEY GUIDE ✅ Working
JavaScript | 7 sec ago | 0.24 KB
-
⭐✅ Exploit 2500$ in 15 Minutes⭐⭐⭐ Y
JavaScript | 7 sec ago | 0.25 KB
-
⭐⭐⭐Profit Method⭐⭐
Java | 14 sec ago | 0.10 KB
-
Free Crypto Method (NEVER SEEN BEFORE)⭐⭐ A
JavaScript | 17 sec ago | 0.25 KB
-
📝 Exchange profit method
JavaScript | 18 sec ago | 0.24 KB
-
⭐⭐⭐Instant Profit Method⭐⭐
Java | 25 sec ago | 0.10 KB
-
⭐⭐⭐Make $15OO in 2O minutesV G⭐⭐
Java | 25 sec ago | 0.10 KB
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand