結果

問題 No.1843 Tree ANDistance
ユーザー 👑 ygussanyygussany
提出日時 2022-02-18 21:45:16
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 153 ms / 2,000 ms
コード長 1,141 bytes
コンパイル時間 693 ms
コンパイル使用メモリ 29,932 KB
実行使用メモリ 6,924 KB
最終ジャッジ日時 2023-09-11 18:50:46
合計ジャッジ時間 4,891 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 153 ms
6,884 KB
testcase_01 AC 153 ms
6,784 KB
testcase_02 AC 120 ms
6,860 KB
testcase_03 AC 119 ms
6,784 KB
testcase_04 AC 142 ms
6,856 KB
testcase_05 AC 82 ms
6,856 KB
testcase_06 AC 115 ms
6,788 KB
testcase_07 AC 149 ms
6,744 KB
testcase_08 AC 153 ms
6,808 KB
testcase_09 AC 114 ms
6,736 KB
testcase_10 AC 104 ms
6,680 KB
testcase_11 AC 137 ms
6,924 KB
testcase_12 AC 71 ms
6,580 KB
testcase_13 AC 119 ms
6,480 KB
testcase_14 AC 5 ms
4,380 KB
testcase_15 AC 5 ms
4,376 KB
testcase_16 AC 3 ms
4,376 KB
testcase_17 AC 3 ms
4,376 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 4 ms
4,380 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 1 ms
4,500 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 2 ms
4,396 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 2 ms
4,856 KB
testcase_28 AC 89 ms
6,048 KB
testcase_29 AC 59 ms
5,284 KB
testcase_30 AC 42 ms
5,076 KB
testcase_31 AC 68 ms
6,336 KB
testcase_32 AC 71 ms
6,096 KB
testcase_33 AC 14 ms
5,044 KB
testcase_34 AC 53 ms
5,864 KB
testcase_35 AC 1 ms
4,380 KB
testcase_36 AC 2 ms
4,380 KB
testcase_37 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>

const int Mod = 1000000007;

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

int main()
{
	int i, N, u, w, c;
	edge *adj[100001] = {}, e[200001], *p;
	scanf("%d", &N);
	for (i = 0; i < N - 1; i++) {
		scanf("%d %d %d", &u, &w, &c);
		e[i*2].v = w;
		e[i*2+1].v = u;
		e[i*2].cost = c;
		e[i*2+1].cost = c;
		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[100001] = {}, q[100001], 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 k, dp[100001], bit;
	long long ans = 0, tmp;
	for (k = 0, bit = 1; k < 30; k++, bit <<= 1) {
		for (head = tail - 1; head >= 0; head--) {
			u = q[head];
			dp[u] = 1;
			for (p = adj[u]; p != NULL; p = p->next) {
				w = p->v;
				if (w == par[u] || (p->cost & bit) == 0) continue;
				ans += (long long)bit * dp[u] % Mod * dp[w] % Mod;
				dp[u] += dp[w];
			}
		}
	}
	printf("%lld\n", ans % Mod);
	fflush(stdout);
	return 0;
}
0