結果
問題 | No.1002 Twotone |
ユーザー | 👑 emthrm |
提出日時 | 2020-03-01 05:02:39 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 1,000 ms / 5,000 ms |
コード長 | 4,630 bytes |
コンパイル時間 | 2,943 ms |
コンパイル使用メモリ | 247,160 KB |
実行使用メモリ | 72,952 KB |
最終ジャッジ日時 | 2024-10-13 20:29:13 |
合計ジャッジ時間 | 15,720 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,816 KB |
testcase_01 | AC | 2 ms
6,820 KB |
testcase_02 | AC | 2 ms
6,820 KB |
testcase_03 | AC | 311 ms
29,892 KB |
testcase_04 | AC | 450 ms
37,980 KB |
testcase_05 | AC | 514 ms
37,940 KB |
testcase_06 | AC | 2 ms
6,820 KB |
testcase_07 | AC | 214 ms
24,320 KB |
testcase_08 | AC | 390 ms
37,880 KB |
testcase_09 | AC | 392 ms
37,760 KB |
testcase_10 | AC | 2 ms
6,820 KB |
testcase_11 | AC | 388 ms
31,252 KB |
testcase_12 | AC | 566 ms
39,544 KB |
testcase_13 | AC | 564 ms
39,664 KB |
testcase_14 | AC | 2 ms
6,816 KB |
testcase_15 | AC | 281 ms
23,888 KB |
testcase_16 | AC | 587 ms
39,184 KB |
testcase_17 | AC | 587 ms
39,236 KB |
testcase_18 | AC | 2 ms
6,816 KB |
testcase_19 | AC | 482 ms
34,356 KB |
testcase_20 | AC | 588 ms
42,364 KB |
testcase_21 | AC | 582 ms
41,988 KB |
testcase_22 | AC | 2 ms
6,820 KB |
testcase_23 | AC | 442 ms
33,652 KB |
testcase_24 | AC | 836 ms
52,556 KB |
testcase_25 | AC | 811 ms
52,716 KB |
testcase_26 | AC | 2 ms
6,816 KB |
testcase_27 | AC | 105 ms
26,476 KB |
testcase_28 | AC | 177 ms
37,884 KB |
testcase_29 | AC | 159 ms
37,780 KB |
testcase_30 | AC | 2 ms
6,816 KB |
testcase_31 | AC | 154 ms
37,688 KB |
testcase_32 | AC | 194 ms
37,848 KB |
testcase_33 | AC | 158 ms
37,884 KB |
testcase_34 | AC | 1,000 ms
72,952 KB |
ソースコード
#define _USE_MATH_DEFINES #include <bits/stdc++.h> using namespace std; #define FOR(i,m,n) for(int i=(m);i<(n);++i) #define REP(i,n) FOR(i,0,n) #define ALL(v) (v).begin(),(v).end() using ll = long long; const int INF = 0x3f3f3f3f; const ll LINF = 0x3f3f3f3f3f3f3f3fLL; const double EPS = 1e-8; const int MOD = 1000000007; // const int MOD = 998244353; const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1}; const int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1}; template <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; } template <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; } struct IOSetup { IOSetup() { cin.tie(nullptr); ios_base::sync_with_stdio(false); cout << fixed << setprecision(20); } } iosetup; using CostType = int; struct Edge { int src, dst; CostType cost; Edge(int src, int dst, CostType cost = 0) : src(src), dst(dst), cost(cost) {} inline bool operator<(const Edge &rhs) const { return cost != rhs.cost ? cost < rhs.cost : dst != rhs.dst ? dst < rhs.dst : src < rhs.src; } inline bool operator<=(const Edge &rhs) const { return !(rhs < *this); } inline bool operator>(const Edge &rhs) const { return rhs < *this; } inline bool operator>=(const Edge &rhs) const { return !(*this < rhs); } }; struct CentroidDecomposition { int root; vector<vector<int> > comp; vector<int> par; CentroidDecomposition(const vector<vector<Edge> > &graph) : graph(graph) { int n = graph.size(); alive.assign(n, true); subtree.resize(n); comp.resize(n); par.assign(n, -1); root = build(0); } private: const vector<vector<Edge> > graph; vector<bool> alive; vector<int> subtree; int build(int s) { int centroid = search_centroid(-1, s, calc_subtree(-1, s) / 2); alive[centroid] = false; for (const Edge &e : graph[centroid]) { if (alive[e.dst]) { comp[centroid].emplace_back(build(e.dst)); par[e.dst] = centroid; } } alive[centroid] = true; return centroid; } int calc_subtree(int par, int ver) { subtree[ver] = 1; for (const Edge &e : graph[ver]) { if (e.dst != par && alive[e.dst]) subtree[ver] += calc_subtree(ver, e.dst); } return subtree[ver]; } int search_centroid(int par, int ver, int half) { for (const Edge &e : graph[ver]) { if (e.dst != par && alive[e.dst]) { if (subtree[e.dst] > half) return search_centroid(ver, e.dst, half); } } return ver; } }; int main() { int n, k; cin >> n >> k; vector<vector<Edge> > graph(n); REP(_, n - 1) { int u, v, c; cin >> u >> v >> c; --u; --v; --c; graph[u].emplace_back(u, v, c); graph[v].emplace_back(v, u, c); } CentroidDecomposition cd(graph); vector<bool> visited(n, false); ll ans = 0; function<void(int)> dfs = [&](int ver) { visited[ver] = true; map<int, ll> mp1, two; ll one = 0; map<pair<int, int>, ll> mp2; for (const Edge &e : graph[ver]) { if (visited[e.dst]) continue; map<int, ll> nx_mp1; map<pair<int, int>, ll> nx_mp2; map<int, int> now; now[e.cost] = 1; ans += one - (mp1.count(e.cost) == 1 ? mp1[e.cost] : 0) + two[e.cost]; // cout << e.dst << ' ' << one << ' ' << ans << '\n'; nx_mp1[e.cost] = 1; function<void(int, int)> dfs2 = [&](int p, int v) { for (const Edge &d : graph[v]) { if (d.dst == p || visited[d.dst]) continue; ++now[d.cost]; if (now.size() == 1) { int x = now.begin()->first; ans += one - (mp1.count(x) == 1 ? mp1[x] : 0) + two[x]; ++nx_mp1[x]; } else if (now.size() == 2) { int x = now.begin()->first, y = next(now.begin())->first; ans += (mp1.count(x) == 1 ? mp1[x] : 0) + (mp1.count(y) == 1 ? mp1[y] : 0) + (mp2.count({x, y}) == 1 ? mp2[{x, y}] : 0) + 1; ++nx_mp2[{x, y}]; } // cout << d.dst << ' ' << ans << '\n'; if (now.size() <= 2) dfs2(v, d.dst); --now[d.cost]; if (now[d.cost] == 0) now.erase(d.cost); } }; dfs2(ver, e.dst); for (auto pr : nx_mp1) { mp1[pr.first] += pr.second; one += pr.second; } for (auto pr : nx_mp2) { mp2[pr.first] += pr.second; two[pr.first.first] += pr.second; two[pr.first.second] += pr.second; } } // cout << ver << ' ' << ans << '\n'; for (int e : cd.comp[ver]) dfs(e); }; dfs(cd.root); cout << ans << '\n'; return 0; }