結果

問題 No.1002 Twotone
ユーザー fuppy_kyoprofuppy_kyopro
提出日時 2020-02-28 23:15:45
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 6,136 bytes
コンパイル時間 2,059 ms
コンパイル使用メモリ 200,232 KB
実行使用メモリ 52,612 KB
最終ジャッジ日時 2024-10-13 19:06:34
合計ジャッジ時間 11,883 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 AC 2 ms
5,248 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 2 ms
5,248 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 2 ms
5,248 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 2 ms
5,248 KB
testcase_27 AC 80 ms
17,704 KB
testcase_28 AC 141 ms
24,064 KB
testcase_29 AC 123 ms
24,156 KB
testcase_30 AC 1 ms
5,248 KB
testcase_31 AC 114 ms
24,032 KB
testcase_32 AC 131 ms
24,004 KB
testcase_33 AC 115 ms
24,068 KB
testcase_34 WA -
権限があれば一括ダウンロードができます

ソースコード

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);
		isused[cent] = true;

		map<pii, int> res;
		dfs(root, par, pii(-1, -1), res);

		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