結果
問題 | No.1002 Twotone |
ユーザー | hitonanode |
提出日時 | 2020-02-29 00:16:20 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
TLE
(最新)
AC
(最初)
|
実行時間 | - |
コード長 | 7,677 bytes |
コンパイル時間 | 2,543 ms |
コンパイル使用メモリ | 207,504 KB |
実行使用メモリ | 45,020 KB |
最終ジャッジ日時 | 2024-10-13 19:33:57 |
合計ジャッジ時間 | 19,949 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,248 KB |
testcase_02 | AC | 1 ms
5,248 KB |
testcase_03 | AC | 318 ms
15,024 KB |
testcase_04 | AC | 414 ms
18,448 KB |
testcase_05 | AC | 416 ms
18,580 KB |
testcase_06 | AC | 2 ms
5,248 KB |
testcase_07 | AC | 220 ms
12,476 KB |
testcase_08 | AC | 397 ms
18,504 KB |
testcase_09 | AC | 413 ms
18,504 KB |
testcase_10 | AC | 2 ms
5,248 KB |
testcase_11 | AC | 455 ms
15,844 KB |
testcase_12 | AC | 617 ms
19,420 KB |
testcase_13 | AC | 611 ms
19,432 KB |
testcase_14 | AC | 2 ms
5,248 KB |
testcase_15 | AC | 282 ms
12,416 KB |
testcase_16 | AC | 603 ms
19,168 KB |
testcase_17 | AC | 579 ms
19,172 KB |
testcase_18 | AC | 2 ms
5,248 KB |
testcase_19 | AC | 536 ms
19,484 KB |
testcase_20 | AC | 654 ms
25,552 KB |
testcase_21 | AC | 644 ms
25,052 KB |
testcase_22 | AC | 2 ms
5,248 KB |
testcase_23 | AC | 405 ms
15,444 KB |
testcase_24 | AC | 832 ms
23,716 KB |
testcase_25 | AC | 834 ms
22,360 KB |
testcase_26 | AC | 2 ms
5,248 KB |
testcase_27 | AC | 80 ms
15,120 KB |
testcase_28 | AC | 131 ms
20,572 KB |
testcase_29 | AC | 125 ms
20,700 KB |
testcase_30 | AC | 2 ms
5,248 KB |
testcase_31 | AC | 119 ms
20,420 KB |
testcase_32 | AC | 128 ms
20,572 KB |
testcase_33 | AC | 114 ms
20,568 KB |
testcase_34 | TLE | - |
ソースコード
#include <bits/stdc++.h> using namespace std; using lint = long long int; using pint = pair<int, int>; using plint = pair<lint, lint>; struct fast_ios { fast_ios(){ cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_; #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((lint)(x).size()) #define POW2(n) (1LL << (n)) #define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++) #define IFOR(i, begin, end) for(int i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--) #define REP(i, n) FOR(i,0,n) #define IREP(i, n) IFOR(i,0,n) template<typename T> void ndarray(vector<T> &vec, int len) { vec.resize(len); } template<typename T, typename... Args> void ndarray(vector<T> &vec, int len, Args... args) { vec.resize(len); for (auto &v : vec) ndarray(v, args...); } template<typename T> bool mmax(T &m, const T q) { if (m < q) {m = q; return true;} else return false; } template<typename T> bool mmin(T &m, const T q) { if (m > q) {m = q; return true;} else return false; } template<typename T1, typename T2> pair<T1, T2> operator+(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first + r.first, l.second + r.second); } template<typename T1, typename T2> pair<T1, T2> operator-(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first - r.first, l.second - r.second); } template<typename T> istream &operator>>(istream &is, vector<T> &vec){ for (auto &v : vec) is >> v; return is; } ///// This part below is only for debug, not used ///// template<typename T> ostream &operator<<(ostream &os, const vector<T> &vec){ os << "["; for (auto v : vec) os << v << ","; os << "]"; return os; } template<typename T> ostream &operator<<(ostream &os, const deque<T> &vec){ os << "deq["; for (auto v : vec) os << v << ","; os << "]"; return os; } template<typename T> ostream &operator<<(ostream &os, const set<T> &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template<typename T> ostream &operator<<(ostream &os, const unordered_set<T> &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template<typename T> ostream &operator<<(ostream &os, const multiset<T> &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template<typename T> ostream &operator<<(ostream &os, const unordered_multiset<T> &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template<typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &pa){ os << "(" << pa.first << "," << pa.second << ")"; return os; } template<typename TK, typename TV> ostream &operator<<(ostream &os, const map<TK, TV> &mp){ os << "{"; for (auto v : mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; } template<typename TK, typename TV> ostream &operator<<(ostream &os, const unordered_map<TK, TV> &mp){ os << "{"; for (auto v : mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; } #define dbg(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ") " << __FILE__ << endl; ///// END ///// vector<vector<int>> edge; vector<int> color; vector<pint> uv; lint ret; struct Tree { int NO_PARENT = -1; using pint = std::pair<int, int>; int V; int E; std::vector<std::vector<pint>> to; // (node_id, edge_id) std::vector<int> par; // parent node_id par[root] = -1 std::vector<int> subtree_size; // size of each subtree std::vector<int> available_edge; // If 0, ignore the corresponding edge. Tree() : Tree(0) {} Tree(int v) : V(v), E(0), to(v), par(v, NO_PARENT), subtree_size(v) {} std::vector<int> color; void add_edge(int v1, int v2, int c) { to[v1].emplace_back(v2, E); to[v2].emplace_back(v1, E); color.emplace_back(c); E++; available_edge.emplace_back(1); } int _dfs_fixroot(int now, int prv) { subtree_size[now] = 1; for (auto nxt : to[now]) { if (nxt.first != prv and available_edge[nxt.second]) { par[nxt.first] = now; subtree_size[now] += _dfs_fixroot(nxt.first, now); } } return subtree_size[now]; } void fix_root(int root) { par[root] = NO_PARENT; _dfs_fixroot(root, -1); } //// Centroid Decpmposition //// std::vector<int> centroid_cand_tmp; void _dfs_detect_centroids(int now, int prv, int n) { bool is_centroid = true; for (auto nxt : to[now]) { if (nxt.first != prv and available_edge[nxt.second]) { _dfs_detect_centroids(nxt.first, now, n); if (subtree_size[nxt.first] > n / 2) is_centroid = false; } } if (n - subtree_size[now] > n / 2) is_centroid = false; if (is_centroid) centroid_cand_tmp.push_back(now); } pint detect_centroids(int r) // ([centroid_node_id1], ([centroid_node_id2]|-1)) { centroid_cand_tmp.clear(); while (par[r] != NO_PARENT) r = par[r]; int n = subtree_size[r]; _dfs_detect_centroids(r, -1, n); if (centroid_cand_tmp.size() == 1) return std::make_pair(centroid_cand_tmp[0], -1); else return std::make_pair(centroid_cand_tmp[0], centroid_cand_tmp[1]); } lint ret; unordered_map<int, lint> mp; void cddfs(int now, int prv, int c1, int c2) { for (auto p : to[now]) if (p.first != prv and available_edge[p.second]) { int nxt = p.first, eid = p.second; int c = color[eid]; int c2new = c2; if (c2 >= 0 and c != c2 and c != c1) continue; if (c != c1) c2new = c; mp[c2new]++; cddfs(nxt, now, c1, c2new); } } int dfscnt(int now, int prv, int c) { int ret = 1; for (auto p : to[now]) if (available_edge[p.second] and color[p.second] == c and p.first != prv) ret += dfscnt(p.first, now, c); return ret; } void centroid_decomposition(int now) { fix_root(now); now = detect_centroids(now).first; unordered_map<int, lint> singlemp; for (auto p : to[now]) if (available_edge[p.second]) singlemp[color[p.second]] += dfscnt(p.first, now, color[p.second]); lint nbt = 0; for (auto p : singlemp) { ret += nbt * p.second; nbt += p.second; } unordered_map<int, vector<int>> c2nxt; for (auto p : to[now]) if (available_edge[p.second]) { c2nxt[color[p.second]].push_back(p.first); } map<pint, lint> mem; for (auto p : to[now]) if (available_edge[p.second]) { int nxt = p.first, col = color[p.second]; mp.clear(); mp[-1] = 1; cddfs(nxt, now, col, -1); for (auto p : mp) if (p.first >= 0) { ret += p.second; pint c = minmax(col, p.first); if (mem.count(c)) ret += p.second * mem[c]; ret += p.second * singlemp[p.first]; ret += p.second * (singlemp[col] - mp[-1]); } for (auto p : mp) mem[minmax(col, p.first)] += p.second; } for (auto p : to[now]) { if (!available_edge[p.second]) continue; available_edge[p.second] = 0; centroid_decomposition(p.first); } } }; int main() { int N, K; cin >> N >> K; Tree tree(N); REP(e, N - 1) { int u, v, c; cin >> u >> v >> c; tree.add_edge(u - 1, v - 1, c); } tree.ret = 0; tree.centroid_decomposition(0); cout << tree.ret << endl; }