結果
問題 | No.1002 Twotone |
ユーザー | fuppy_kyopro |
提出日時 | 2020-02-28 23:17:04 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 1,297 ms / 5,000 ms |
コード長 | 6,140 bytes |
コンパイル時間 | 2,075 ms |
コンパイル使用メモリ | 199,860 KB |
実行使用メモリ | 67,436 KB |
最終ジャッジ日時 | 2024-10-13 19:08:26 |
合計ジャッジ時間 | 13,359 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 1 ms
5,248 KB |
testcase_02 | AC | 1 ms
5,248 KB |
testcase_03 | AC | 330 ms
19,376 KB |
testcase_04 | AC | 439 ms
23,564 KB |
testcase_05 | AC | 429 ms
23,472 KB |
testcase_06 | AC | 2 ms
5,248 KB |
testcase_07 | AC | 169 ms
16,376 KB |
testcase_08 | AC | 303 ms
23,584 KB |
testcase_09 | AC | 299 ms
23,584 KB |
testcase_10 | AC | 2 ms
5,248 KB |
testcase_11 | AC | 331 ms
20,560 KB |
testcase_12 | AC | 457 ms
24,972 KB |
testcase_13 | AC | 485 ms
24,972 KB |
testcase_14 | AC | 2 ms
5,248 KB |
testcase_15 | AC | 233 ms
15,540 KB |
testcase_16 | AC | 460 ms
24,776 KB |
testcase_17 | AC | 462 ms
24,684 KB |
testcase_18 | AC | 2 ms
5,248 KB |
testcase_19 | AC | 407 ms
31,928 KB |
testcase_20 | AC | 493 ms
47,240 KB |
testcase_21 | AC | 485 ms
46,188 KB |
testcase_22 | AC | 2 ms
5,248 KB |
testcase_23 | AC | 330 ms
25,676 KB |
testcase_24 | AC | 636 ms
42,504 KB |
testcase_25 | AC | 667 ms
39,060 KB |
testcase_26 | AC | 2 ms
5,248 KB |
testcase_27 | AC | 85 ms
17,828 KB |
testcase_28 | AC | 145 ms
24,200 KB |
testcase_29 | AC | 140 ms
24,324 KB |
testcase_30 | AC | 2 ms
5,248 KB |
testcase_31 | AC | 126 ms
24,032 KB |
testcase_32 | AC | 140 ms
23,908 KB |
testcase_33 | AC | 120 ms
24,068 KB |
testcase_34 | AC | 1,297 ms
67,436 KB |
ソースコード
#include <bits/stdc++.h> //#include <unistd.h> //#include <iostream> using namespace std; #define DEBUG(x) cerr<<#x<<": "<<x<<endl; #define DEBUG_VEC(v) cerr<<#v<<":";for(int i=0;i<v.size();i++) cerr<<" "<<v[i]; cerr<<endl; #define DEBUG_MAT(v) cerr<<#v<<endl;for(int i=0;i<v.size();i++){for(int j=0;j<v[i].size();j++) {cerr<<v[i][j]<<" ";}cerr<<endl;} typedef long long ll; #define int ll #define vi vector<int> #define vl vector<ll> #define vii vector< vector<int> > #define vll vector< vector<ll> > #define vs vector<string> #define pii pair<int,int> #define pis pair<int,string> #define psi pair<string,int> #define pll pair<ll,ll> template<class S, class T> pair<S, T> operator+(const pair<S, T> &s, const pair<S, T> &t) { return pair<S, T>(s.first + t.first, s.second + t.second); } template<class S, class T> pair<S, T> operator-(const pair<S, T> &s, const pair<S, T> &t) { return pair<S, T>(s.first - t.first, s.second - t.second); } template<class S, class T> ostream& operator<<(ostream& os, pair<S, T> p) { os << "(" << p.first << ", " << p.second << ")"; return os; } #define X first #define Y second #define rep(i,n) for(int i=0;i<(n);i++) #define rep1(i,n) for(int i=1;i<=(n);i++) #define rrep(i,n) for(int i=(n)-1;i>=0;i--) #define rrep1(i,n) for(int i=(n);i>0;i--) #define REP(i,a,b) for(int i=a;i<b;i++) #define in(x, a, b) (a <= x && x < b) #define all(c) c.begin(),c.end() template<class T> bool chmax(T &a, const T &b) { if (a<b) { a = b; return 1; } return 0; } template<class T> bool chmin(T &a, const T &b) { if (a>b) { a = b; return 1; } return 0; } #define UNIQUE(v) v.erase(std::unique(v.begin(), v.end()), v.end()); const ll inf = 1000000001; const ll INF = (ll)1e18 + 1; const long double pi = 3.1415926535897932384626433832795028841971L; #define Sp(p) cout<<setprecision(25)<< fixed<<p<<endl; //int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; //int dx2[8] = { 1,1,0,-1,-1,-1,0,1 }, dy2[8] = { 0,1,1,1,0,-1,-1,-1 }; vi dx = {1, 0, -1, 0}, dy = {0, 1, 0, -1}; vi dx2 = { 1,1,0,-1,-1,-1,0,1 }, dy2 = { 0,1,1,1,0,-1,-1,-1 }; #define fio() cin.tie(0); ios::sync_with_stdio(false); const ll MOD = 1000000007; //const ll MOD = 998244353; // #define mp make_pair //#define endl '\n' ll ans = 0; int n, k; class CentroidDecomposition { public: int n; //���_�� vector<vector<pii>> G; //�O���t�̗אڃ��X�g�\�� vector<bool> isused; //���̒��_�����łɕ����Ɏg���Ă��邩 vi subtreesize; //�����̃T�C�Y,�X�V����� vi parent; vi centroid; //�d�S�����ɒlj�����Ă��� CentroidDecomposition(int n) : n(n) { G.resize(n); isused.resize(n); subtreesize.resize(n); parent.resize(n); } void add_edge(int u, int v, int c) { G[u].push_back(pii(v, c)); G[v].push_back(pii(u, c)); } void dfs(int now, int par, pii c, map<pii, int> &mp) { mp[c]++; rep (i, G[now].size()) { int ch = G[now][i].first; int color = G[now][i].second; if (ch == par) continue; if (isused[ch]) continue; pii c2 = c; if (c2.first == color or c2.second == color) {} else if (c2.first == -1) c2.first = color; else if (c2.second == -1) { c2.second = color; if (c2.first > c2.second) swap(c2.first, c2.second); } else continue; dfs(ch, now, c2, mp); } } int search_centroid(int now, int par, int size) { subtreesize[now] = 1; parent[now] = par; bool iscentroid = true; for (int i = 0; i < G[now].size(); i++) { int child = G[now][i].first; if (child == par) continue; if (isused[child]) continue; int child_size = search_centroid(child, now, size); if (child_size > size / 2) iscentroid = false; subtreesize[now] += child_size; } if (size - subtreesize[now] > size / 2) { iscentroid = false; } if (iscentroid) { centroid.push_back(now); } return subtreesize[now]; } //���ɂ���ĕς�� map<pii, int> solve(int root, int par, int size) { //DEBUG(pii(root, par)); search_centroid(root, -1, size); int cent = centroid.back(); //DEBUG(cent); map<pii, int> res; dfs(root, par, pii(-1, -1), res); isused[cent] = true; map<pii, int> sum; map<int, int> mono, num; ll mono_sum = 0; rep(i, G[cent].size()) { int ch = G[cent][i].first; int color = G[cent][i].second; if (isused[ch]) { continue; } map<pii, int> mp; if (parent[ch] == cent) { mp = solve(ch, cent, subtreesize[ch]); } else { mp = solve(ch, cent, size - subtreesize[cent]); } map<pii, int> mp2; for (auto itr = mp.begin(); itr != mp.end(); itr++) { pii c = itr->first; if (c.first == color or c.second == color) { mp2[c] += itr->second; } else if (c.first == -1) { c.first = color; mp2[c] += itr->second; } else if (c.second == -1) { c.second = color; if (c.first > c.second) swap(c.first, c.second); mp2[c] += itr->second; } } for (auto itr = mp2.begin(); itr != mp2.end(); itr++) { pii c = itr->first; //UG(c); if (c.second == -1) { int x = c.first; //DEBUG(make_pair(pii(mono[x], num[x]), mono_sum)); ans += (mono_sum - mono[x] + num[x]) * itr->second; } else { int x = c.first; int y = c.second; assert (x < y); //DEBUG(make_pair(pii(mono[x], mono[y]), sum[pii(x, y)])); ans += (1 + mono[x] + mono[y] + sum[pii(x, y)]) * itr->second; } } for (auto itr = mp2.begin(); itr != mp2.end(); itr++) { pii c = itr->first; sum[itr->first] += itr->second; if (c.second == -1) { int x = c.first; mono[x] += itr->second; mono_sum += itr->second; } else { int x = c.first, y = c.second; num[x] += itr->second; num[y] += itr->second; } } } /* DEBUG(cent); for (auto itr = res.begin(); itr != res.end(); itr++) { DEBUG(*itr); } //*/ return res; } }; signed main() { fio(); cin >> n >> k; CentroidDecomposition tree(n); rep (i, n - 1) { int u, v, c; cin >> u >> v >> c; u--; v--; c--; tree.add_edge(u, v, c); } tree.solve(0, -1, n); cout << ans << endl; }