Submission #2193507


Source Code Expand

#include <bits/stdc++.h>

#define _overload(_1,_2,_3,name,...) name
#define _rep(i,n) _range(i,0,n)
#define _range(i,a,b) for(int i=int(a);i<int(b);++i)
#define rep(...) _overload(__VA_ARGS__,_range,_rep,)(__VA_ARGS__)

#define _rrep(i,n) _rrange(i,n,0)
#define _rrange(i,a,b) for(int i=int(a)-1;i>=int(b);--i)
#define rrep(...) _overload(__VA_ARGS__,_rrange,_rrep,)(__VA_ARGS__)

#define _all(arg) begin(arg),end(arg)
#define uniq(arg) sort(_all(arg)),(arg).erase(unique(_all(arg)),end(arg))
#define getidx(ary,key) lower_bound(_all(ary),key)-begin(ary)
#define clr(a,b) memset((a),(b),sizeof(a))
#define bit(n) (1LL<<(n))
#define popcount(n) (__builtin_popcountll(n))

using namespace std;

template<class T>bool chmax(T &a, const T &b) { return (a < b) ? (a = b, 1) : 0;}
template<class T>bool chmin(T &a, const T &b) { return (b < a) ? (a = b, 1) : 0;}

using ll = long long;
using R = long double;
const R EPS = 1e-9L; // [-1000,1000]->EPS=1e-8 [-10000,10000]->EPS=1e-7
inline int sgn(const R& r) {return (r > EPS) - (r < -EPS);}
inline R sq(R x) {return sqrt(max(x, 0.0L));}

const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};

// Problem Specific Parameter:

const int limit = 100010;
ll s[limit];

int a[limit], b[limit];
ll c[limit];

int asz[limit], bsz[limit];
vector<int> tree[limit];
int sz[limit], par[limit];

ll dist[limit];

int dfs(int v, int p) {
	par[v] = p;
	sz[v] = 1;
	for (auto &e : tree[v]) {
		const int v2 = v ^ a[e] ^ b[e];
		if (v2 == p) continue;
		sz[v] += dfs(v2, v);
	}
	return sz[v];
}

void calc(int v, int p, ll d) {
	dist[v] = d;
	for (auto &e : tree[v]) {
		const int v2 = v ^ a[e] ^ b[e];
		if (v2 == p) continue;
		calc(v2, v, d + c[e]);
	}
}

int main(void) {
	int n;
	cin >> n;

	rep(i, n - 1) {
		cin >> a[i] >> b[i];
		a[i]--, b[i]--;
		tree[a[i]].push_back(i);
		tree[b[i]].push_back(i);
	}

	rep(i, n) cin >> s[i];
	dfs(0, -1);

	rep(i, n - 1) {
		if (par[a[i]] == b[i]) {
			asz[i] = sz[a[i]];
			bsz[i] = n - asz[i];
		} else {
			bsz[i] = sz[b[i]];
			asz[i] = n - bsz[i];
		}

		if (asz[i] != bsz[i]) {
			c[i] = abs(s[a[i]] - s[b[i]]) / abs(asz[i] - bsz[i]);
		}
	}

	rep(i, n - 1) {
		if (asz[i] == bsz[i]) {
			calc(a[i], b[i], 0LL);
			calc(b[i], a[i], 0LL);
			ll cur = s[a[i]];
			rep(j, n) cur -= dist[j];
			c[i] = cur / (n / 2);
		}
	}


	rep(i, n - 1) cout << c[i] << endl;

	return 0;
}

Submission Info

Submission Time
Task D - Ancient Tree Record
User Hec
Language C++14 (GCC 5.4.1)
Score 800
Code Size 2498 Byte
Status AC
Exec Time 310 ms
Memory 15616 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 2 ms 4864 KB
00_example_02.txt AC 2 ms 4864 KB
00_example_03.txt AC 2 ms 4864 KB
s1_01.txt AC 172 ms 10880 KB
s1_02.txt AC 240 ms 12800 KB
s1_03.txt AC 12 ms 5248 KB
s1_04.txt AC 199 ms 11392 KB
s1_05.txt AC 33 ms 5888 KB
s1_06.txt AC 301 ms 14848 KB
s1_07.txt AC 306 ms 15616 KB
s1_08.txt AC 307 ms 14848 KB
s1_09.txt AC 2 ms 4864 KB
s1_10.txt AC 2 ms 4864 KB
s1_11.txt AC 2 ms 4864 KB
s1_12.txt AC 2 ms 4864 KB
s1_13.txt AC 2 ms 4864 KB
s2_14.txt AC 157 ms 8060 KB
s2_15.txt AC 218 ms 9464 KB
s2_16.txt AC 274 ms 10744 KB
s2_17.txt AC 278 ms 10744 KB
s2_18.txt AC 275 ms 10744 KB
s2_19.txt AC 275 ms 10744 KB
s2_20.txt AC 2 ms 4864 KB
s2_21.txt AC 2 ms 4864 KB
s2_22.txt AC 2 ms 4864 KB
s2_23.txt AC 2 ms 4864 KB
s3_24.txt AC 170 ms 7936 KB
s3_25.txt AC 249 ms 9088 KB
s3_26.txt AC 12 ms 4992 KB
s3_27.txt AC 204 ms 8320 KB
s3_28.txt AC 33 ms 5376 KB
s3_29.txt AC 309 ms 10240 KB
s3_30.txt AC 301 ms 10496 KB
s3_31.txt AC 304 ms 10368 KB
s3_32.txt AC 309 ms 13568 KB
s3_33.txt AC 310 ms 14208 KB
s3_34.txt AC 301 ms 10744 KB
s3_35.txt AC 294 ms 10744 KB
s3_36.txt AC 2 ms 4864 KB
s3_37.txt AC 2 ms 4864 KB
s3_38.txt AC 3 ms 4864 KB
s3_39.txt AC 2 ms 4864 KB
s3_40.txt AC 173 ms 8448 KB
s3_41.txt AC 248 ms 9728 KB
s3_42.txt AC 12 ms 4992 KB
s3_43.txt AC 308 ms 11136 KB
s3_44.txt AC 306 ms 11264 KB
s3_45.txt AC 306 ms 11136 KB