結果
問題 |
No.1418 Sum of Sum of Subtree Size
|
ユーザー |
![]() |
提出日時 | 2021-03-06 00:37:49 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 829 bytes |
コンパイル時間 | 1,892 ms |
コンパイル使用メモリ | 195,096 KB |
最終ジャッジ日時 | 2025-01-19 12:10:37 |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 35 WA * 6 |
コンパイルメッセージ
main.cpp: In function ‘int main()’: main.cpp:25:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 25 | scanf("%d", &n); | ~~~~~^~~~~~~~~~ main.cpp:28:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 28 | scanf("%d %d", &a, &b); | ~~~~~^~~~~~~~~~~~~~~~~
ソースコード
#include<bits/stdc++.h> #define REP(i,b,e) for(int i=b;i<e;i++) using ll = int_fast64_t; std::vector<int> links[100000], visit; int in[100000], out[100000], from[100000], cnt[100001]; bool used[100000]; void eular_tour(int v=0){ if(used[v]) return; used[v] = true; in[v] = visit.size(); visit.push_back(v); for(auto to: links[v]){ if(!used[to]){ from[to] = v; eular_tour(to); } } out[v] = visit.size(); } int main(){ int n; scanf("%d", &n); REP(i, 0, n-1){ int a, b; scanf("%d %d", &a, &b); links[a-1].push_back(b-1); links[b-1].push_back(a-1); } eular_tour(); REP(i, 0, n) cnt[i] = out[i] - in[i]; ll ans = 0; REP(i, 0, n){ ans += n; for(auto x: links[i]){ if(from[i]!=x) ans += cnt[x] * (n - cnt[x]); else ans += cnt[i] * (n - cnt[i]); } } printf("%ld\n", ans); return 0; }