結果

問題 No.2214 Products on Tree
ユーザー 👑 ygussanyygussany
提出日時 2023-02-04 19:22:48
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 85 ms / 3,000 ms
コード長 1,064 bytes
コンパイル時間 303 ms
コンパイル使用メモリ 29,504 KB
実行使用メモリ 14,412 KB
最終ジャッジ日時 2023-09-16 15:53:58
合計ジャッジ時間 6,317 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,968 KB
testcase_01 AC 2 ms
6,708 KB
testcase_02 AC 2 ms
6,812 KB
testcase_03 AC 77 ms
14,084 KB
testcase_04 AC 77 ms
14,144 KB
testcase_05 AC 72 ms
13,916 KB
testcase_06 AC 45 ms
12,740 KB
testcase_07 AC 13 ms
8,996 KB
testcase_08 AC 10 ms
8,936 KB
testcase_09 AC 16 ms
9,112 KB
testcase_10 AC 42 ms
12,428 KB
testcase_11 AC 21 ms
11,076 KB
testcase_12 AC 25 ms
11,124 KB
testcase_13 AC 80 ms
14,336 KB
testcase_14 AC 83 ms
14,408 KB
testcase_15 AC 84 ms
14,336 KB
testcase_16 AC 80 ms
14,412 KB
testcase_17 AC 85 ms
14,280 KB
testcase_18 AC 46 ms
12,620 KB
testcase_19 AC 48 ms
13,860 KB
testcase_20 AC 43 ms
13,684 KB
testcase_21 AC 32 ms
11,452 KB
testcase_22 AC 67 ms
14,104 KB
testcase_23 AC 2 ms
7,040 KB
testcase_24 AC 2 ms
6,792 KB
testcase_25 AC 3 ms
6,952 KB
testcase_26 AC 2 ms
6,660 KB
testcase_27 AC 2 ms
6,800 KB
testcase_28 AC 33 ms
12,944 KB
testcase_29 AC 13 ms
7,284 KB
testcase_30 AC 40 ms
14,368 KB
testcase_31 AC 67 ms
14,332 KB
testcase_32 AC 57 ms
14,336 KB
testcase_33 AC 46 ms
14,412 KB
testcase_34 AC 46 ms
14,272 KB
testcase_35 AC 64 ms
14,276 KB
testcase_36 AC 63 ms
14,344 KB
testcase_37 AC 45 ms
14,324 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>

const int Mod = 998244353;

typedef struct Edge {
	struct Edge *next;
	int v;
} edge;

int main()
{
	int i, N, M, u, w;
	edge *adj[200001] = {}, e[400001], *p;
	scanf("%d", &N);
	for (i = 0; i < N - 1; i++) {
		scanf("%d %d", &u, &w);
		e[i*2].v = w;
		e[i*2+1].v = u;
		e[i*2].next = adj[u];
		e[i*2+1].next = adj[w];
		adj[u] = &(e[i*2]);
		adj[w] = &(e[i*2+1]);
	}
	
	int par[200001] = {}, q[200001], head, tail;
	par[1] = 1;
	q[0] = 1;
	for (head = 0, tail = 1; head < tail; head++) {
		u = q[head];
		for (p = adj[u]; p != NULL; p = p->next) {
			w = p->v;
			if (par[w] == 0) {
				par[w] = u;
				q[tail++] = w;
			}
		}
	}
	
	int j, ww;
	long long dp[200001][2];
	for (head--; head >= 0; head--) {
		u = q[head];
		dp[u][0] = 1;
		dp[u][1] = 1;
		for (p = adj[u]; p != NULL; p = p->next) {
			w = p->v;
			if (par[u] == w) continue;
			
			dp[u][1] = (dp[u][0] * dp[w][1] + dp[u][1] * (dp[w][0] + dp[w][1])) % Mod;
			dp[u][0] = (dp[u][0] * (dp[w][0] + dp[w][1])) % Mod;
		}
	}
	printf("%lld\n", dp[1][1]);
	fflush(stdout);
	return 0;
}
0