結果
| 問題 |
No.1769 Don't Stop the Game
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2021-11-03 08:45:45 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,347 bytes |
| コンパイル時間 | 1,454 ms |
| コンパイル使用メモリ | 75,488 KB |
| 最終ジャッジ日時 | 2025-01-25 10:46:24 |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 18 WA * 10 |
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:17:21: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
17 | int n; scanf("%d", &n);
| ~~~~~^~~~~~~~~~
main.cpp:21:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
21 | scanf("%d%d%d", &a[i], &b[i], &c[i]); a[i]--; b[i]--;
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ソースコード
/*
# Algorithm
## Time Comlexity
O(Nlog(N))
*/
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <map>
int main() {
int n; scanf("%d", &n);
std::vector<std::vector<int>> g(n);
std::vector<int> a(n - 1), b(n - 1), c(n - 1);
for(int i = 0; i < n - 1; i++) {
scanf("%d%d%d", &a[i], &b[i], &c[i]); a[i]--; b[i]--;
g[a[i]].push_back(i);
g[b[i]].push_back(i);
}
std::vector<int> x(n), y(n), size(n), in(n), out(n), tour;
std::vector<std::vector<int>> child(n), child_order(n); // LA
std::map<int, std::vector<std::vector<int>>> map_xor; {
std::map<int, int> map;
auto dfs = [&](auto&& dfs, int v, int par, int xor_val) -> void {
in[v] = tour.size();
tour.push_back(v);
x[v] = xor_val;
y[v] = map[xor_val]++;
auto& vec = map_xor[xor_val];
if(vec.size() <= y[v]) vec.resize(y[v] + 2);
vec[y[v]].push_back(in[v]);
size[v] = 1;
for(int id: g[v]) {
int to = v ^ a[id] ^ b[id];
if(to == par) continue;
dfs(dfs, to, v, xor_val ^ c[id]);
child[v].push_back(to);
child_order[v].push_back(in[to]);
size[v] += size[to];
}
tour.push_back(v);
out[v] = tour.size();
map[xor_val]--;
}; dfs(dfs, 0, -1, 0);
}
using i64 = long long;
i64 ans = 0;
{ // 解説のパターン 1, 2 を数える
std::map<int, int> map;
auto dfs = [&](auto&& dfs, int v, int par, int depth) -> void {
if(y[v]) ans += map[x[v]] + size[v];
int tmp = map[x[v]];
for(int id: g[v]) {
int to = a[id] ^ b[id] ^ v;
if(to == par) continue;
map[x[v]] = n - size[to];
dfs(dfs, to, v, depth + 1);
}
map[x[v]] = tmp;
}; dfs(dfs, 0, -1, 0);
}
{ // 解説のパターン 3 を数える
std::map<int, int> map;
for(int i = 0; i < n; i++) map[x[i]] = -1;
auto dfs = [&](auto&& dfs, int v, int par) -> void {
if(v and map[x[v]] != par) {
int root = (y[v] ? map[x[v]] : 0);
auto& vec = map_xor[x[v]][y[v]];
i64 cnt = std::distance(lower_bound(begin(vec), end(vec), in[root]),
lower_bound(begin(vec), end(vec), out[root])) - 1;
ans += cnt * size[v];
}
int tmp = map[x[v]];
for(int id: g[v]) {
int to = a[id] ^ b[id] ^ v;
if(to == par) continue;
map[x[v]] = to;
dfs(dfs, to, v);
}
map[x[v]] = tmp;
}; dfs(dfs, 0, -1);
}
ans = (i64)n * (n - 1) - ans;
printf("%lld\n", ans);
}