結果

問題 No.1002 Twotone
ユーザー kotamanegikotamanegi
提出日時 2020-02-28 22:07:49
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,917 bytes
コンパイル時間 2,199 ms
コンパイル使用メモリ 164,224 KB
実行使用メモリ 31,976 KB
最終ジャッジ日時 2024-05-07 20:33:31
合計ジャッジ時間 9,744 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
10,368 KB
testcase_01 AC 7 ms
10,368 KB
testcase_02 AC 7 ms
10,368 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 AC 7 ms
10,496 KB
testcase_07 AC 101 ms
18,292 KB
testcase_08 AC 158 ms
22,036 KB
testcase_09 AC 158 ms
22,040 KB
testcase_10 AC 6 ms
10,368 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 7 ms
10,368 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 7 ms
10,368 KB
testcase_27 AC 116 ms
24,680 KB
testcase_28 AC 201 ms
31,972 KB
testcase_29 AC 200 ms
31,976 KB
testcase_30 AC 7 ms
10,368 KB
testcase_31 AC 178 ms
30,792 KB
testcase_32 AC 193 ms
30,872 KB
testcase_33 AC 183 ms
30,792 KB
testcase_34 AC 127 ms
29,028 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[300000];
int cnt[300000];
int used[300000];
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;
}
vector<pair<int, int>> edges[300000];
int dp[300000];
void solve() {
	int n, k;
	cin >> n >> k;
	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) {
			ans += dp[x] * (cnt[union_find(x)] - 1LL);
			dp[x] += cnt[union_find(x)] - 1LL;
		}
		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;
			used[x.first] = 0;
			used[x.second] = 0;
		}
	}
	cout << ans << endl;
}

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