結果
問題 | No.1098 LCAs |
ユーザー |
![]() |
提出日時 | 2020-06-26 21:54:58 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 163 ms / 2,000 ms |
コード長 | 1,731 bytes |
コンパイル時間 | 1,162 ms |
コンパイル使用メモリ | 119,468 KB |
実行使用メモリ | 25,088 KB |
最終ジャッジ日時 | 2024-07-04 20:57:11 |
合計ジャッジ時間 | 4,373 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 28 |
ソースコード
#include <iostream>#include <vector>#include <algorithm>#include <cmath>#include <string>#include <queue>#include <stack>#include <set>#include <map>#include <iomanip>#include <utility>#include <tuple>#include <functional>#include <bitset>#include <cassert>#include <complex>#include <stdio.h>#include <time.h>#include <numeric>#include <random>#include <unordered_map>#include <unordered_set>#define all(a) a.begin(),a.end()#define rep(i, n) for (ll i = 0; i < (n); i++)#define pb push_back#pragma GCC optimize("O3")#pragma GCC optimize("unroll-loops")using namespace std;typedef long long ll;typedef unsigned long long ull;typedef long double ld;typedef pair<ll, ll> P;typedef complex<ld> com;constexpr int inf = 1000000010;constexpr ll INF = 1000000000000000010;constexpr ld eps = 1e-12;constexpr ld pi = 3.141592653589793238;template<class T, class U> inline bool chmax(T &a, const U &b) { if (a < b) { a = b; return true; } return false; }template<class T, class U> inline bool chmin(T &a, const U &b) { if (a > b) { a = b; return true; } return false; }vector<ll> sz(200010, 1), par(200010);vector<vector<int>> graph(200010, vector<int>());void dfs(int n, int p) {par[n] = p;for (int i : graph[n]) {if (i == p) continue;dfs(i, n);sz[n] += sz[i];}}signed main() {cin.tie(0);ios::sync_with_stdio(false);cout << fixed << setprecision(20);int n;cin >> n;rep(_, n - 1) {int u, v;cin >> u >> v;u--; v--;graph[u].pb(v); graph[v].pb(u);}dfs(0, -1);rep(i, n) {ll ans = sz[i] * 2 - 1;ll cnt = 0;for (int j : graph[i]) {if (j != par[i]) {cnt += sz[j];ans -= sz[j] * sz[j];}}cout << ans + cnt * cnt << '\n';}}