結果

問題 No.2047 Path Factory
ユーザー CleverLarry0702CleverLarry0702
提出日時 2022-12-01 13:54:24
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 81 ms / 2,000 ms
コード長 606 bytes
コンパイル時間 1,422 ms
コンパイル使用メモリ 166,364 KB
実行使用メモリ 22,912 KB
最終ジャッジ日時 2024-04-17 04:17:01
合計ジャッジ時間 3,431 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,656 KB
testcase_01 AC 2 ms
6,656 KB
testcase_02 AC 2 ms
6,528 KB
testcase_03 AC 2 ms
6,656 KB
testcase_04 AC 3 ms
6,528 KB
testcase_05 AC 3 ms
6,656 KB
testcase_06 AC 3 ms
6,656 KB
testcase_07 AC 46 ms
10,368 KB
testcase_08 AC 37 ms
9,728 KB
testcase_09 AC 44 ms
10,368 KB
testcase_10 AC 60 ms
11,520 KB
testcase_11 AC 38 ms
9,600 KB
testcase_12 AC 26 ms
8,704 KB
testcase_13 AC 27 ms
8,576 KB
testcase_14 AC 40 ms
9,856 KB
testcase_15 AC 47 ms
10,496 KB
testcase_16 AC 35 ms
10,112 KB
testcase_17 AC 37 ms
9,984 KB
testcase_18 AC 40 ms
11,008 KB
testcase_19 AC 46 ms
12,032 KB
testcase_20 AC 67 ms
14,464 KB
testcase_21 AC 40 ms
10,624 KB
testcase_22 AC 10 ms
7,680 KB
testcase_23 AC 12 ms
8,064 KB
testcase_24 AC 11 ms
7,680 KB
testcase_25 AC 70 ms
22,912 KB
testcase_26 AC 81 ms
20,480 KB
testcase_27 AC 74 ms
12,204 KB
testcase_28 AC 52 ms
12,500 KB
testcase_29 AC 4 ms
6,912 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define ll long long

const ll mod=998244353;
ll N;
vector<ll>G[1<<17];
ll dp[1<<17][3];

void dfs(ll u,ll p)
{
	ll a=1,b=0,c=0;
	for(ll v:G[u])if(v!=p)
	{
		dfs(v,u);
		ll na=a*(dp[v][0]+dp[v][1])%mod;
        ll nb=( b*(dp[v][0]+dp[v][1])+a*(dp[v][1]+dp[v][2]) )%mod;
		ll nc=( c*(dp[v][0]+dp[v][1])+b*(dp[v][1]+dp[v][2]) )%mod;
		a=na,b=nb,c=nc;
	}
	dp[u][0]=c;
	dp[u][1]=b;
	dp[u][2]=a;
}

int main()
{
	cin>>N;
	for(ll i=1;i<N;i++)
	{
		ll u,v;
		cin>>u>>v;
		G[u].push_back(v);
		G[v].push_back(u);
	}
	dfs(1,0);
	cout<<(dp[1][0]+dp[1][1])%mod <<endl;
}
0