結果

問題 No.1002 Twotone
ユーザー fuppy_kyoprofuppy_kyopro
提出日時 2020-02-28 23:17:04
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,590 ms / 5,000 ms
コード長 6,140 bytes
コンパイル時間 2,280 ms
コンパイル使用メモリ 197,688 KB
実行使用メモリ 68,832 KB
最終ジャッジ日時 2023-08-03 21:57:36
合計ジャッジ時間 16,912 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 390 ms
19,004 KB
testcase_04 AC 515 ms
23,304 KB
testcase_05 AC 528 ms
23,300 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 213 ms
16,172 KB
testcase_08 AC 376 ms
23,176 KB
testcase_09 AC 381 ms
23,272 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 447 ms
20,496 KB
testcase_12 AC 623 ms
24,984 KB
testcase_13 AC 634 ms
24,868 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 291 ms
15,096 KB
testcase_16 AC 617 ms
24,356 KB
testcase_17 AC 626 ms
24,612 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 550 ms
31,604 KB
testcase_20 AC 669 ms
48,764 KB
testcase_21 AC 648 ms
46,888 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 468 ms
25,404 KB
testcase_24 AC 905 ms
42,640 KB
testcase_25 AC 882 ms
39,760 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 102 ms
19,324 KB
testcase_28 AC 185 ms
24,552 KB
testcase_29 AC 158 ms
24,536 KB
testcase_30 AC 1 ms
4,384 KB
testcase_31 AC 145 ms
24,292 KB
testcase_32 AC 182 ms
23,988 KB
testcase_33 AC 155 ms
23,904 KB
testcase_34 AC 1,590 ms
68,832 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0