結果

問題 No.1843 Tree ANDistance
ユーザー yudedakoyudedako
提出日時 2022-02-28 18:46:05
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 241 ms / 2,000 ms
コード長 1,753 bytes
コンパイル時間 3,307 ms
コンパイル使用メモリ 115,296 KB
実行使用メモリ 12,232 KB
最終ジャッジ日時 2023-09-21 03:17:10
合計ジャッジ時間 9,302 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 241 ms
11,444 KB
testcase_01 AC 241 ms
11,512 KB
testcase_02 AC 209 ms
11,004 KB
testcase_03 AC 206 ms
10,980 KB
testcase_04 AC 220 ms
12,140 KB
testcase_05 AC 143 ms
12,176 KB
testcase_06 AC 149 ms
12,056 KB
testcase_07 AC 229 ms
11,108 KB
testcase_08 AC 237 ms
11,268 KB
testcase_09 AC 193 ms
10,596 KB
testcase_10 AC 193 ms
10,452 KB
testcase_11 AC 221 ms
12,232 KB
testcase_12 AC 133 ms
11,432 KB
testcase_13 AC 141 ms
11,244 KB
testcase_14 AC 8 ms
4,380 KB
testcase_15 AC 8 ms
4,380 KB
testcase_16 AC 4 ms
4,376 KB
testcase_17 AC 4 ms
4,376 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 7 ms
4,380 KB
testcase_20 AC 4 ms
4,380 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 150 ms
9,664 KB
testcase_29 AC 100 ms
7,616 KB
testcase_30 AC 83 ms
7,440 KB
testcase_31 AC 134 ms
9,760 KB
testcase_32 AC 138 ms
10,340 KB
testcase_33 AC 29 ms
5,572 KB
testcase_34 AC 71 ms
8,532 KB
testcase_35 AC 1 ms
4,376 KB
testcase_36 AC 2 ms
4,376 KB
testcase_37 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <unordered_map>
#include <unordered_set>
#include <set>
#include <vector>
#include <numeric>
#include <algorithm>
#include <queue>
#include <string>
#include <random>
#include <array>
#include <climits>
#include <map>
#include <cassert>
#include <stack>
#include <iomanip>
#include <cfloat>
#include <bitset>
#include <fstream>
#include <chrono>


int main() {
	constexpr int MOD = 1000000007;
	int n; std::cin >> n;
	std::vector<std::tuple<int, int, int>> edges(n - 1);
	for (auto& [a, b, c] : edges) {
		std::cin >> a >> b >> c; --a; --b;
	}
	std::vector<std::vector<std::pair<int, int>>> graph(n);
	for (const auto [a, b, c] : edges) {
		graph[a].emplace_back(b, c);
		graph[b].emplace_back(a, c);
	}
	std::vector<int> depth(n, -1); depth[0] = 0;
	std::queue<int> queue; queue.push(0);
	while (!queue.empty()) {
		const auto top = queue.front(); queue.pop();
		for (const auto [next, _] : graph[top]) {
			if (depth[next] == -1) {
				depth[next] = depth[top] + 1;
				queue.push(next);
			}
		}
	}
	std::vector<int> from_leaf(n); std::iota(from_leaf.begin(), from_leaf.end(), 0);
	std::sort(from_leaf.rbegin(), from_leaf.rend(), [&depth](const int i, const int j) {return depth[i] < depth[j]; });
	long long int result{ 0 };
	for (auto d = 0; d < 30; ++d) {
		long long int one_path{ 0 };
		std::vector<int> end_path(n, 0);
		for (const auto node : from_leaf) {
			int count{ 0 };
			for (const auto [child, c] : graph[node]) {
				if (depth[child] <= depth[node] || (((c >> d) & 1) == 0)) continue;
				one_path += (count + 1LL) * (end_path[child] + 1LL);
				count += end_path[child] + 1;
			}
			end_path[node] = count;
		}
		result += ((one_path % MOD) << d) % MOD;
	}
	result %= MOD;
	std::cout << result << '\n';
}
0