結果
問題 |
No.1769 Don't Stop the Game
|
ユーザー |
|
提出日時 | 2021-11-03 08:59:17 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,560 bytes |
コンパイル時間 | 1,049 ms |
コンパイル使用メモリ | 70,032 KB |
最終ジャッジ日時 | 2025-01-25 10:46:56 |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | WA * 3 |
other | WA * 28 |
コンパイルメッセージ
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(N) */ #include <stdio.h> #include <algorithm> #include <vector> #include <unordered_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), size(n); { auto dfs = [&](auto&& dfs, int v, int par, int xor_val) -> void { x[v] = xor_val; 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]); size[v] += size[to]; } }; dfs(dfs, 0, -1, 0); } using i64 = long long; i64 ans = 0; std::vector<i64> count(n); { std::unordered_map<int, int> map; auto dfs = [&](auto&& dfs, int v, int par) -> void { count[map[x[v]]]++; int prev = count[v]; int tmp = map[x[v]]; map[x[v]] = v; for(int id: g[v]) { int to = a[id] ^ b[id] ^ v; if(to == par) continue; dfs(dfs, to, v); i64 diff = count[v] - prev; ans += diff * (n - size[to]); } map[x[v]] = tmp; }; } { std::unordered_map<int, int> map; auto dfs = [&](auto&& dfs, int v, int par) -> void { int root = map[x[v]]; ans += count[root] * size[v]; int tmp = map[x[v]]; map[x[v]] = v; for(int id: g[v]) { int to = v ^ a[id] ^ b[id]; if(to == par) continue; dfs(dfs, to, v); } map[x[v]] = tmp; }; } ans = (i64)n * (n - 1) - ans; printf("%lld\n", ans); }