結果
問題 | No.1002 Twotone |
ユーザー | risujiroh |
提出日時 | 2020-02-28 23:36:57 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
TLE
|
実行時間 | - |
コード長 | 3,619 bytes |
コンパイル時間 | 2,588 ms |
コンパイル使用メモリ | 199,340 KB |
実行使用メモリ | 81,352 KB |
最終ジャッジ日時 | 2024-10-13 19:22:07 |
合計ジャッジ時間 | 43,159 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,248 KB |
testcase_02 | AC | 2 ms
5,248 KB |
testcase_03 | AC | 639 ms
33,304 KB |
testcase_04 | AC | 861 ms
41,944 KB |
testcase_05 | AC | 865 ms
42,076 KB |
testcase_06 | AC | 2 ms
5,248 KB |
testcase_07 | AC | 635 ms
26,880 KB |
testcase_08 | AC | 1,181 ms
42,200 KB |
testcase_09 | AC | 1,201 ms
42,080 KB |
testcase_10 | AC | 2 ms
5,248 KB |
testcase_11 | AC | 1,477 ms
33,188 KB |
testcase_12 | AC | 2,063 ms
41,860 KB |
testcase_13 | AC | 2,707 ms
41,856 KB |
testcase_14 | AC | 2 ms
5,248 KB |
testcase_15 | AC | 1,025 ms
25,472 KB |
testcase_16 | AC | 2,170 ms
41,708 KB |
testcase_17 | AC | 2,208 ms
41,864 KB |
testcase_18 | AC | 2 ms
5,248 KB |
testcase_19 | AC | 1,333 ms
41,480 KB |
testcase_20 | AC | 1,591 ms
51,520 KB |
testcase_21 | AC | 1,567 ms
51,040 KB |
testcase_22 | AC | 2 ms
5,248 KB |
testcase_23 | AC | 1,852 ms
51,932 KB |
testcase_24 | AC | 3,517 ms
81,244 KB |
testcase_25 | AC | 3,538 ms
81,352 KB |
testcase_26 | AC | 2 ms
5,248 KB |
testcase_27 | AC | 247 ms
29,952 KB |
testcase_28 | AC | 580 ms
42,808 KB |
testcase_29 | AC | 449 ms
42,936 KB |
testcase_30 | AC | 2 ms
5,248 KB |
testcase_31 | AC | 399 ms
42,912 KB |
testcase_32 | AC | 571 ms
42,940 KB |
testcase_33 | AC | 429 ms
42,932 KB |
testcase_34 | TLE | - |
ソースコード
#include <bits/stdc++.h> using namespace std; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int n, k; cin >> n >> k; vector<vector<int>> g(n); map<pair<int, int>, int> col; for (int _ = n - 1; _--; ) { int u, v, c; cin >> u >> v >> c; --u, --v, --c; g[u].push_back(v); g[v].push_back(u); col[{u, v}] = col[{v, u}] = c; } vector<bool> be(n, true); vector<int> sz(n), par(n); auto centroid = [&](int r) { vector<int> vs; auto dfs = [&](auto&& self, int v, int p) -> void { vs.push_back(v); par[v] = p; sz[v] = 1; for (int u : g[v]) { if (be[u] and u != p) { self(self, u, v); sz[v] += sz[u]; } } }; dfs(dfs, r, -1); for (int v : vs) { bool ok = vs.size() - sz[v] <= vs.size() / 2; for (int u : g[v]) { if (be[u] and u != par[v]) { if (sz[u] > (int)vs.size() / 2) { ok = false; break; } } } if (ok) { return v; } } return -1; }; long long res = 0; map<int, int> mp1; map<pair<int, int>, int> mp2; auto dfs = [&](auto&& self, int v, int p, set<int> se) -> void { if (se.size() == 1) { ++mp1[*begin(se)]; } else if (se.size() == 2) { ++mp2[{*begin(se), *next(begin(se))}]; } else { assert(false); } for (int u : g[v]) { if (be[u] and u != p) { auto nse = se; nse.insert(col[{v, u}]); if (nse.size() >= 3) { continue; } self(self, u, v, nse); } } }; auto rec = [&](auto&& self, int c) -> void { c = centroid(c); map<int, int> smp1; map<pair<int, int>, int> smp2; for (int v : g[c]) { if (be[v]) { mp1.clear(), mp2.clear(); dfs(dfs, v, c, {col[{c, v}]}); for (auto e : mp1) { for (auto f : mp1) { if (e.first != f.first) { res -= (long long)e.second * f.second; } } for (auto f : mp2) { if (e.first == f.first.first or e.first == f.first.second) { res -= (long long)e.second * f.second; } } } for (auto e : mp2) { res += 2 * e.second; for (auto f : mp1) { if (e.first.first == f.first or e.first.second == f.first) { res -= (long long)e.second * f.second; } } for (auto f : mp2) { if (e.first == f.first) { res -= (long long)e.second * f.second; } } } for (auto e : mp1) { smp1[e.first] += e.second; } for (auto e : mp2) { smp2[e.first] += e.second; } } } for (auto e : smp1) { for (auto f : smp1) { if (e.first != f.first) { res += (long long)e.second * f.second; } } for (auto f : smp2) { if (e.first == f.first.first or e.first == f.first.second) { res += (long long)e.second * f.second; } } } for (auto e : smp2) { for (auto f : smp1) { if (e.first.first == f.first or e.first.second == f.first) { res += (long long)e.second * f.second; } } for (auto f : smp2) { if (e.first == f.first) { res += (long long)e.second * f.second; } } } be[c] = false; for (int u : g[c]) { if (be[u]) { self(self, u); } } be[c] = true; }; rec(rec, 0); res /= 2; cout << res << '\n'; }