Submission #1857273


Source Code Expand

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

const int N = 100005;
typedef pair<int, int> ii;
	
int n, pos, sz[N];
long long sum;
long long d[N];
long long a[N];
long long res[N];
vector<ii> G[N];

void dfs(int u, int p) {
	sz[u] = 1;
	for (auto v : G[u]) {
		if (v.first == p) continue; 
		dfs(v.first, u), sz[u] += sz[v.first];
		if (n != 2 * sz[v.first]) {
			res[v.second] = (a[v.first] - a[u]) / (n - 2 * sz[v.first]);
		}
		else pos = v.first;
	}
}

void redfs(int u, int p) {
	sum += d[u];
	for (auto v : G[u]) {
		if (v.first == p) continue;
		d[v.first] = d[u] + res[v.second], redfs(v.first, u);
	}
}

int main() {
	ios::sync_with_stdio(false);
	cin >> n;
	for (int i = 1; i < n; ++i) {
		int u, v; cin >> u >> v;
		G[u].push_back(ii(v, i));
		G[v].push_back(ii(u, i));
	}
	for (int i = 1; i <= n; ++i) cin >> a[i];
	dfs(1, 1), redfs(1, 1);
	for (int i = 1; i < n; ++i) {
		if (!res[i]) res[i] = (a[1] - sum) / sz[pos];
		cout << res[i] << '\n';
	}
}

Submission Info

Submission Time
Task D - Ancient Tree Record
User aome
Language C++14 (GCC 5.4.1)
Score 800
Code Size 1009 Byte
Status AC
Exec Time 75 ms
Memory 16896 KB

Judge Result

Set Name Sample Subtask1 Subtask2 All
Score / Max Score 0 / 0 300 / 300 200 / 200 300 / 300
Status
AC × 3
AC × 14
AC × 11
AC × 48
Set Name Test Cases
Sample 00_example_01.txt, 00_example_02.txt, 00_example_03.txt
Subtask1 00_example_01.txt, s1_01.txt, s1_02.txt, s1_03.txt, s1_04.txt, s1_05.txt, s1_06.txt, s1_07.txt, s1_08.txt, s1_09.txt, s1_10.txt, s1_11.txt, s1_12.txt, s1_13.txt
Subtask2 00_example_02.txt, s2_14.txt, s2_15.txt, s2_16.txt, s2_17.txt, s2_18.txt, s2_19.txt, s2_20.txt, s2_21.txt, s2_22.txt, s2_23.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, s1_08.txt, s1_09.txt, s1_10.txt, s1_11.txt, s1_12.txt, s1_13.txt, s2_14.txt, s2_15.txt, s2_16.txt, s2_17.txt, s2_18.txt, s2_19.txt, s2_20.txt, s2_21.txt, s2_22.txt, s2_23.txt, s3_24.txt, s3_25.txt, s3_26.txt, s3_27.txt, s3_28.txt, s3_29.txt, s3_30.txt, s3_31.txt, s3_32.txt, s3_33.txt, s3_34.txt, s3_35.txt, s3_36.txt, s3_37.txt, s3_38.txt, s3_39.txt, s3_40.txt, s3_41.txt, s3_42.txt, s3_43.txt, s3_44.txt, s3_45.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 3 ms 4352 KB
s1_01.txt AC 36 ms 11264 KB
s1_02.txt AC 50 ms 14208 KB
s1_03.txt AC 5 ms 4736 KB
s1_04.txt AC 41 ms 12544 KB
s1_05.txt AC 9 ms 5632 KB
s1_06.txt AC 61 ms 16896 KB
s1_07.txt AC 62 ms 16896 KB
s1_08.txt AC 61 ms 16896 KB
s1_09.txt AC 3 ms 4352 KB
s1_10.txt AC 3 ms 4352 KB
s1_11.txt AC 3 ms 4352 KB
s1_12.txt AC 3 ms 4352 KB
s1_13.txt AC 3 ms 4352 KB
s2_14.txt AC 29 ms 7416 KB
s2_15.txt AC 40 ms 8820 KB
s2_16.txt AC 50 ms 9972 KB
s2_17.txt AC 50 ms 9972 KB
s2_18.txt AC 50 ms 9972 KB
s2_19.txt AC 51 ms 9972 KB
s2_20.txt AC 3 ms 4352 KB
s2_21.txt AC 3 ms 4352 KB
s2_22.txt AC 3 ms 4352 KB
s2_23.txt AC 3 ms 4352 KB
s3_24.txt AC 37 ms 7424 KB
s3_25.txt AC 55 ms 8448 KB
s3_26.txt AC 5 ms 4480 KB
s3_27.txt AC 45 ms 7808 KB
s3_28.txt AC 9 ms 4864 KB
s3_29.txt AC 69 ms 9600 KB
s3_30.txt AC 67 ms 9856 KB
s3_31.txt AC 71 ms 9600 KB
s3_32.txt AC 65 ms 13312 KB
s3_33.txt AC 67 ms 15744 KB
s3_34.txt AC 56 ms 9972 KB
s3_35.txt AC 56 ms 9972 KB
s3_36.txt AC 3 ms 4352 KB
s3_37.txt AC 3 ms 4352 KB
s3_38.txt AC 3 ms 4352 KB
s3_39.txt AC 3 ms 4352 KB
s3_40.txt AC 37 ms 7424 KB
s3_41.txt AC 55 ms 8448 KB
s3_42.txt AC 5 ms 4480 KB
s3_43.txt AC 75 ms 9600 KB
s3_44.txt AC 68 ms 9856 KB
s3_45.txt AC 69 ms 9600 KB