結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 11 ms
26,752 KB
testcase_01 AC 12 ms
26,880 KB
testcase_02 AC 12 ms
26,752 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 AC 12 ms
26,624 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 11 ms
27,008 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 12 ms
27,008 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 12 ms
26,880 KB
testcase_27 AC 127 ms
37,468 KB
testcase_28 AC 217 ms
42,992 KB
testcase_29 AC 197 ms
42,996 KB
testcase_30 AC 11 ms
26,880 KB
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
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;
	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