Submission #1868189


Source Code Expand

#include <bits/stdc++.h>
using namespace std;

const int N = 100005;

struct Edge {
	int u, v, w;

	Edge (int u, int v, int w) :
		u(u), v(v), w(w) {}

	bool operator < (const Edge& rhs) const {
		return w > rhs.w;
	}
};

int n, par[N];
long long res[N];
long long val[N];
vector<Edge> E;
vector<int> comp[N];

int find(int u) {
	return (u == par[u]) ? u : par[u] = find(par[u]);
}

void join(int u, int v, int w) {
	u = find(u), v = find(v);
	if (comp[u].size() > comp[v].size()) swap(u, v);
	int szu = comp[u].size(), szv = comp[v].size();
	val[v] += 1LL * szu * w;
	for (auto i : comp[u]) {
		res[i] += val[u] + 1LL * szv * w - val[v], comp[v].push_back(i);
	}
	val[u] = 0, comp[u].clear(), par[u] = v;
}

int main() {
	ios::sync_with_stdio(false);
	cin >> n;
	for (int i = 1; i <= n; ++i) {
		par[i] = i, comp[i].push_back(i);
	}
	for (int i = 1; i < n; ++i) {
		int u, v, w; cin >> u >> v >> w;
		E.push_back(Edge(u, v, w));
	}
	sort(E.begin(), E.end());
	for (auto i : E) join(i.u, i.v, i.w);
	for (int i = 1; i <= n; ++i) {
		res[i] += val[find(i)]; cout << res[i] << '\n';
	}
}

Submission Info

Submission Time
Task E - Black Cats Deployment
User aome
Language C++14 (GCC 5.4.1)
Score 800
Code Size 1137 Byte
Status AC
Exec Time 70 ms
Memory 14068 KB

Judge Result

Set Name Sample Subtask1 Subtask2 All
Score / Max Score 0 / 0 200 / 200 200 / 200 400 / 400
Status
AC × 3
AC × 10
AC × 9
AC × 32
Set Name Test Cases
Sample 00_example_01.txt, 00_example_02.txt, 00_example_03.txt
Subtask1 00_example_01.txt, 00_example_02.txt, 00_example_03.txt, s1_01.txt, s1_02.txt, s1_03.txt, s1_04.txt, s1_05.txt, s1_06.txt, s1_07.txt
Subtask2 00_example_02.txt, s1_07.txt, s2_08.txt, s2_09.txt, s2_10.txt, s2_11.txt, s2_12.txt, s2_13.txt, s2_14.txt
All 00_example_01.txt, 00_example_02.txt, 00_example_03.txt, s1_01.txt, s1_02.txt, s1_03.txt, s1_04.txt, s1_05.txt, s1_06.txt, s1_07.txt, s2_08.txt, s2_09.txt, s2_10.txt, s2_11.txt, s2_12.txt, s2_13.txt, s2_14.txt, s3_15.txt, s3_16.txt, s3_17.txt, s3_18.txt, s3_19.txt, s3_20.txt, s3_21.txt, s3_22.txt, s3_23.txt, s3_24.txt, s3_25.txt, s3_26.txt, s3_27.txt, s3_28.txt, s3_29.txt
Case Name Status Exec Time Memory
00_example_01.txt AC 3 ms 4352 KB
00_example_02.txt AC 3 ms 4352 KB
00_example_03.txt AC 2 ms 4352 KB
s1_01.txt AC 3 ms 4352 KB
s1_02.txt AC 3 ms 4352 KB
s1_03.txt AC 3 ms 4352 KB
s1_04.txt AC 3 ms 4480 KB
s1_05.txt AC 3 ms 4480 KB
s1_06.txt AC 3 ms 4480 KB
s1_07.txt AC 2 ms 4352 KB
s2_08.txt AC 47 ms 10484 KB
s2_09.txt AC 24 ms 7156 KB
s2_10.txt AC 15 ms 6012 KB
s2_11.txt AC 54 ms 11124 KB
s2_12.txt AC 55 ms 10996 KB
s2_13.txt AC 54 ms 11892 KB
s2_14.txt AC 51 ms 12532 KB
s3_15.txt AC 59 ms 10996 KB
s3_16.txt AC 30 ms 7668 KB
s3_17.txt AC 18 ms 6140 KB
s3_18.txt AC 29 ms 7416 KB
s3_19.txt AC 65 ms 11764 KB
s3_20.txt AC 68 ms 12276 KB
s3_21.txt AC 65 ms 11636 KB
s3_22.txt AC 65 ms 11636 KB
s3_23.txt AC 70 ms 14068 KB
s3_24.txt AC 63 ms 13556 KB
s3_25.txt AC 53 ms 11252 KB
s3_26.txt AC 63 ms 13428 KB
s3_27.txt AC 52 ms 11252 KB
s3_28.txt AC 63 ms 13300 KB
s3_29.txt AC 53 ms 11252 KB