結果

問題 No.1418 Sum of Sum of Subtree Size
ユーザー y61mpnl
提出日時 2021-03-06 00:38:59
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 51 ms / 2,000 ms
コード長 832 bytes
コンパイル時間 1,785 ms
コンパイル使用メモリ 196,152 KB
最終ジャッジ日時 2025-01-19 12:10:46
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 41
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:26:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   26 |         scanf("%ld", &n);
      |         ~~~~~^~~~~~~~~~~
main.cpp:29:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   29 |                 scanf("%d %d", &a, &b);
      |                 ~~~~~^~~~~~~~~~~~~~~~~

ソースコード

diff #

#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];
ll 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(){
	ll n;
	scanf("%ld", &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;
}
0