結果
| 問題 |
No.1418 Sum of Sum of Subtree Size
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2021-03-06 01:11:11 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 84 ms / 2,000 ms |
| コード長 | 1,268 bytes |
| コンパイル時間 | 1,583 ms |
| コンパイル使用メモリ | 162,980 KB |
| 実行使用メモリ | 16,256 KB |
| 最終ジャッジ日時 | 2024-10-07 14:45:02 |
| 合計ジャッジ時間 | 4,047 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 41 |
コンパイルメッセージ
main.cpp:1:25: warning: extra tokens at end of #include directive
1 | #include <bits/stdc++.h>>
| ^
ソースコード
#include <bits/stdc++.h>>
#define pt(sth) cout << sth << "\n"
using namespace std;
//#include <atcoder/all>
//using namespace atcoder;
typedef long long ll;
typedef pair<ll, ll> pll;
template<class T>bool chmax(T &a, const T &b) {if(a<b) {a=b; return 1;} return 0;}
template<class T>bool chmin(T &a, const T &b) {if(b<a) {a=b; return 1;} return 0;}
static const ll INF=1e18;
static const ll MAX=101010;
//static const ll MOD=1e9+7;
//static const ll MOD=998244353;
//for(i=0;i<N;i++) cin>>a[i];
typedef vector<vector<vector<ll>>> v3D;
typedef vector<vector<ll>> v2D;
typedef vector<ll> v1D;
vector<ll> g[MAX];
vector<ll> nc;
vector<ll> pa;
ll dfs(ll u,ll p){
pa[u]=p;
if(g[u].size()==1) return nc[u]=1;
ll res=1;
for(auto v:g[u]){
if(v==p) continue;
res+=dfs(v, u);
}
return nc[u]=res;
}
int main() {
ll i, j, k;
ll N;cin>>N;
nc=vector<ll>(N,0);
pa=vector<ll>(N,0);
for(i=0;i<N-1;i++){
ll a,b;cin>>a>>b;a--;b--;
g[a].push_back(b);
g[b].push_back(a);
}
g[0].push_back(N);
g[N].push_back(0);
dfs(0,N);
ll ans=0;
for(ll u=0;u<N;u++){
for(auto v:g[u]){
if(v==pa[u]){
ans+=(N-nc[u]+1)*nc[u];
}else{
ans+=(nc[v]+1)*(N-nc[v]);
}
}
}
pt(ans);
}