結果

問題 No.2504 NOT Path Painting
ユーザー kotatsugamekotatsugame
提出日時 2023-10-13 22:44:50
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 138 ms / 2,000 ms
コード長 942 bytes
コンパイル時間 686 ms
コンパイル使用メモリ 76,236 KB
実行使用メモリ 8,652 KB
最終ジャッジ日時 2023-10-13 22:44:57
合計ジャッジ時間 4,261 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 91 ms
4,352 KB
testcase_02 AC 91 ms
4,364 KB
testcase_03 AC 91 ms
4,352 KB
testcase_04 AC 91 ms
4,484 KB
testcase_05 AC 90 ms
4,488 KB
testcase_06 AC 91 ms
4,392 KB
testcase_07 AC 91 ms
4,348 KB
testcase_08 AC 91 ms
4,384 KB
testcase_09 AC 92 ms
4,356 KB
testcase_10 AC 90 ms
4,348 KB
testcase_11 AC 90 ms
4,380 KB
testcase_12 AC 91 ms
4,348 KB
testcase_13 AC 94 ms
4,352 KB
testcase_14 AC 100 ms
4,512 KB
testcase_15 AC 121 ms
5,932 KB
testcase_16 AC 127 ms
6,248 KB
testcase_17 AC 135 ms
5,708 KB
testcase_18 AC 119 ms
6,548 KB
testcase_19 AC 138 ms
8,100 KB
testcase_20 AC 118 ms
8,652 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<algorithm>
#include<cassert>
#include<atcoder/modint>
using namespace std;
using mint=atcoder::modint998244353;
int N;
vector<int>G[40000];
int ch[40000];
void dfs1(int u,int p)
{
	ch[u]=1;
	for(int v:G[u])if(v!=p)
	{
		dfs1(v,u);
		ch[u]+=ch[v];
	}
}
mint ans;
mint ALL_INV;
void dfs2(int u,int p,int pc)
{
	mint P,Q;
	{
		mint sum=pc;
		mint cur=N;
		for(int v:G[u])if(v!=p)
		{
			cur+=sum*ch[v];
			sum+=ch[v];
		}
		P=cur*ALL_INV;
		Q=mint(pc)*ch[u]*ALL_INV;
	}
	ans+=P/(1-P);
	ans-=Q/(1-Q);
	for(int v:G[u])if(v!=p)dfs2(v,u,N-ch[v]);
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int T;cin>>T;
	for(;T--;)
	{
		cin>>N;
		for(int i=0;i<N;i++)G[i].clear();
		for(int i=1;i<N;i++)
		{
			int u,v;cin>>u>>v;
			u--,v--;
			G[u].push_back(v);
			G[v].push_back(u);
		}
		dfs1(0,-1);
		ans=1;
		ALL_INV=mint((long)N*(N+1)/2).inv();
		dfs2(0,-1,0);
		cout<<ans.val()<<"\n";
	}
}
0