結果

問題 No.1002 Twotone
ユーザー kotamanegikotamanegi
提出日時 2020-02-28 22:28:56
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 1,958 bytes
コンパイル時間 2,387 ms
コンパイル使用メモリ 165,504 KB
実行使用メモリ 49,696 KB
最終ジャッジ日時 2024-05-07 20:34:05
合計ジャッジ時間 12,396 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 RE -
testcase_07 AC 110 ms
33,672 KB
testcase_08 AC 174 ms
37,032 KB
testcase_09 AC 175 ms
36,904 KB
testcase_10 RE -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 RE -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 RE -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 RE -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 RE -
testcase_27 AC 154 ms
40,064 KB
testcase_28 AC 303 ms
46,844 KB
testcase_29 AC 255 ms
46,840 KB
testcase_30 RE -
testcase_31 AC 237 ms
45,656 KB
testcase_32 AC 302 ms
45,792 KB
testcase_33 AC 195 ms
45,916 KB
testcase_34 AC 134 ms
43,892 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:2:18: warning: '#pragma comment linker' ignored [-Wignored-pragmas]
    2 | #pragma comment (linker, "/STACK:526000000")
      |                  ^
1 warning generated.

ソースコード

diff #

#define  _CRT_SECURE_NO_WARNINGS
#pragma comment (linker, "/STACK:526000000")

#include "bits/stdc++.h"

using namespace std;
typedef string::const_iterator State;
#define eps 1e-11L
#define MAX_MOD 1000000007LL
#define GYAKU 500000004LL

#define MOD 998244353LL
#define seg_size 262144
#define pb push_back
#define mp make_pair
typedef long long ll;
#define REP(a,b) for(long long (a) = 0;(a) < (b);++(a))
#define ALL(x) (x).begin(),(x).end()

unsigned long xor128() {
	static unsigned long x = 123456789, y = 362436069, z = 521288629, w = time(NULL);
	unsigned long t = (x ^ (x << 11));
	x = y; y = z; z = w;
	return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)));
}

void init() {
	iostream::sync_with_stdio(false);
	cout << fixed << setprecision(20);
}

#define int ll

int union_tree[1000001];
int cnt[1000001];
vector<pair<int, int>> edges[1000001];
int dp[1000001];

int union_find(int now) {
	if (now == union_tree[now]) return now;
	return union_tree[now] = union_find(union_tree[now]);
}

void union_merge(int a, int b) {
	a = union_find(a);
	b = union_find(b);
	union_tree[a] = b;
	return;
}

void solve() {
	int n, k;
	cin >> n >> k;
	assert(n >= 10000);
	REP(i, n-1) {
		int a, b, c;
		cin >> a >> b >> c;
		a--; b--; c--;
		edges[c].push_back(mp(a, b));
	}

	REP(i, n) {
		union_tree[i] = i;
	}

	int ans = 0;

	for (int q = 0; q < k; ++q) {
		set<int> appeared;
		for (auto x : edges[q]) {
			union_merge(x.first, x.second);
			appeared.insert(x.first);
			appeared.insert(x.second);
		}
		for (auto x : appeared) {
			cnt[union_find(x)]++;
		}
		for (auto x : appeared) {
			int geko = cnt[union_find(x)] - 1LL;
			ans += dp[x] * geko;
		}

		for (auto x : appeared) {
			int geko = cnt[union_find(x)] - 1LL;
			dp[x] += geko;
		}

		for (auto x : edges[q]) {
			union_tree[x.first] = x.first;
			union_tree[x.second] = x.second;
			cnt[x.first] = 0;
			cnt[x.second] = 0;
		}
	}

	cout << ans << endl;
}

#undef int
int main() {
	init();
	solve();
}
0