답안 #287865

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
287865 2020-09-01T05:24:01 Z 임성재(#5782) Election Campaign (JOI15_election_campaign) C++17
100 / 100
659 ms 75092 KB
#include<bits/stdc++.h>
using namespace std;

#define fast ios::sync_with_stdio(false);
#define fi first
#define se second
#define em emplace
#define eb emplace_back
#define mp make_pair
#define all(v) (v).begin(), (v).end()

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const int inf = 1e9;
const ll INF = 1e18;

int n, m;
vector<int> g[100010];
int d[100010];
int sp[100010][21];
ll dp[100010];
ll Dp[100010];
vector<pair<pii, int>> qr[100010];
vector<pii> upd[100010];
ll sum[100010][21];
int up[100010][21];

void dfs(int x) {
	up[x][0] = x;
	for(int i=1; i<=20; i++) {
		up[x][i] = up[sp[x][i-1]][i-1];
		sp[x][i] = sp[sp[x][i-1]][i-1];
	}

	for(auto i : g[x]) {
		if(i == sp[x][0]) continue;
		
		d[i] = d[x] + 1;
		sp[i][0] = x;
		
		dfs(i);
	}
}

int lca(int a, int b) {
	if(d[a] > d[b]) swap(a, b);

	for(int i=20; i>=0; i--) {
		if(d[b] - d[a] >= (1<<i)) {
			b = sp[b][i];
		}
	}

	if(a == b) return a;

	for(int i=20; i>=0; i--) {
		if(sp[a][i] != sp[b][i]) {
			a = sp[a][i];
			b = sp[b][i];
		}
	}

	return sp[a][0];
}

void f(int x) {
	for(auto i : g[x]) {
		if(i == sp[x][0]) continue;

		f(i);

		Dp[x] += dp[i];
	}

	dp[x] = Dp[x];
	sum[x][0] = Dp[x];

	for(auto i : upd[x]) {
		int k = i.fi;
		int u = i.se;

		sum[u][k] = sum[u][k-1] + sum[sp[u][k-1]][k-1] - dp[up[u][k-1]];
	}

	for(auto i : qr[x]) {
		ll s = i.se;
		
		int u=i.fi.fi, v=i.fi.se;

		for(int j=20; j>=0; j--) {
			if(d[u] - d[x] >= (1 << j)) {
				s -= dp[up[u][j]];
				s += sum[u][j];
				u = sp[u][j];
			}
		}

		for(int j=20; j>=0; j--) {
			if(d[v] - d[x] >= (1 << j)) {
				s -= dp[up[v][j]];
				s += sum[v][j];
				v = sp[v][j];
			}
		}

		s += Dp[x];

		dp[x] = max(dp[x], s);
	}
}

int main() {
	fast;

	cin >> n;

	for(int i=1; i<n; i++) {
		int u, v;
		cin >> u >> v;

		g[u].eb(v);
		g[v].eb(u);
	}

	dfs(1);

	cin >> m;
	for(int i=0; i<m; i++) {
		int a, b, c;
		cin >> a >> b >> c;

		if(d[a] > d[b]) swap(a, b);

		qr[lca(a, b)].eb(mp(a, b), c);
	}

	for(int i=1; i<=n; i++) {
		for(int j=1; j<=20; j++) {
			upd[sp[i][j]].eb(j, i);
		}
	}

	for(int i=1; i<=n; i++) sort(all(upd[i]));

	f(1);

	cout << dp[1];
}
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 7424 KB Output is correct
2 Correct 5 ms 7424 KB Output is correct
3 Correct 5 ms 7424 KB Output is correct
4 Correct 6 ms 8064 KB Output is correct
5 Correct 228 ms 62544 KB Output is correct
6 Correct 180 ms 69840 KB Output is correct
7 Correct 454 ms 73424 KB Output is correct
8 Correct 146 ms 61372 KB Output is correct
9 Correct 433 ms 72540 KB Output is correct
10 Correct 152 ms 61752 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 7424 KB Output is correct
2 Correct 5 ms 7424 KB Output is correct
3 Correct 7 ms 8064 KB Output is correct
4 Correct 291 ms 71768 KB Output is correct
5 Correct 286 ms 71880 KB Output is correct
6 Correct 258 ms 71892 KB Output is correct
7 Correct 275 ms 71892 KB Output is correct
8 Correct 287 ms 71944 KB Output is correct
9 Correct 256 ms 71892 KB Output is correct
10 Correct 281 ms 71896 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 7424 KB Output is correct
2 Correct 5 ms 7424 KB Output is correct
3 Correct 7 ms 8064 KB Output is correct
4 Correct 291 ms 71768 KB Output is correct
5 Correct 286 ms 71880 KB Output is correct
6 Correct 258 ms 71892 KB Output is correct
7 Correct 275 ms 71892 KB Output is correct
8 Correct 287 ms 71944 KB Output is correct
9 Correct 256 ms 71892 KB Output is correct
10 Correct 281 ms 71896 KB Output is correct
11 Correct 22 ms 8576 KB Output is correct
12 Correct 287 ms 71764 KB Output is correct
13 Correct 286 ms 72020 KB Output is correct
14 Correct 263 ms 71896 KB Output is correct
15 Correct 285 ms 71892 KB Output is correct
16 Correct 258 ms 71852 KB Output is correct
17 Correct 281 ms 71900 KB Output is correct
18 Correct 293 ms 71912 KB Output is correct
19 Correct 260 ms 71892 KB Output is correct
20 Correct 281 ms 71900 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 319 ms 63744 KB Output is correct
2 Correct 261 ms 71872 KB Output is correct
3 Correct 640 ms 75092 KB Output is correct
4 Correct 225 ms 62772 KB Output is correct
5 Correct 564 ms 74708 KB Output is correct
6 Correct 224 ms 62916 KB Output is correct
7 Correct 619 ms 74552 KB Output is correct
8 Correct 305 ms 64348 KB Output is correct
9 Correct 266 ms 71892 KB Output is correct
10 Correct 659 ms 73876 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 7424 KB Output is correct
2 Correct 5 ms 7424 KB Output is correct
3 Correct 5 ms 7424 KB Output is correct
4 Correct 6 ms 8064 KB Output is correct
5 Correct 228 ms 62544 KB Output is correct
6 Correct 180 ms 69840 KB Output is correct
7 Correct 454 ms 73424 KB Output is correct
8 Correct 146 ms 61372 KB Output is correct
9 Correct 433 ms 72540 KB Output is correct
10 Correct 152 ms 61752 KB Output is correct
11 Correct 7 ms 8064 KB Output is correct
12 Correct 7 ms 8064 KB Output is correct
13 Correct 7 ms 8064 KB Output is correct
14 Correct 7 ms 8064 KB Output is correct
15 Correct 7 ms 8064 KB Output is correct
16 Correct 7 ms 8064 KB Output is correct
17 Correct 7 ms 8064 KB Output is correct
18 Correct 7 ms 8064 KB Output is correct
19 Correct 7 ms 8064 KB Output is correct
20 Correct 7 ms 8064 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 7424 KB Output is correct
2 Correct 5 ms 7424 KB Output is correct
3 Correct 5 ms 7424 KB Output is correct
4 Correct 6 ms 8064 KB Output is correct
5 Correct 228 ms 62544 KB Output is correct
6 Correct 180 ms 69840 KB Output is correct
7 Correct 454 ms 73424 KB Output is correct
8 Correct 146 ms 61372 KB Output is correct
9 Correct 433 ms 72540 KB Output is correct
10 Correct 152 ms 61752 KB Output is correct
11 Correct 5 ms 7424 KB Output is correct
12 Correct 5 ms 7424 KB Output is correct
13 Correct 7 ms 8064 KB Output is correct
14 Correct 291 ms 71768 KB Output is correct
15 Correct 286 ms 71880 KB Output is correct
16 Correct 258 ms 71892 KB Output is correct
17 Correct 275 ms 71892 KB Output is correct
18 Correct 287 ms 71944 KB Output is correct
19 Correct 256 ms 71892 KB Output is correct
20 Correct 281 ms 71896 KB Output is correct
21 Correct 22 ms 8576 KB Output is correct
22 Correct 287 ms 71764 KB Output is correct
23 Correct 286 ms 72020 KB Output is correct
24 Correct 263 ms 71896 KB Output is correct
25 Correct 285 ms 71892 KB Output is correct
26 Correct 258 ms 71852 KB Output is correct
27 Correct 281 ms 71900 KB Output is correct
28 Correct 293 ms 71912 KB Output is correct
29 Correct 260 ms 71892 KB Output is correct
30 Correct 281 ms 71900 KB Output is correct
31 Correct 319 ms 63744 KB Output is correct
32 Correct 261 ms 71872 KB Output is correct
33 Correct 640 ms 75092 KB Output is correct
34 Correct 225 ms 62772 KB Output is correct
35 Correct 564 ms 74708 KB Output is correct
36 Correct 224 ms 62916 KB Output is correct
37 Correct 619 ms 74552 KB Output is correct
38 Correct 305 ms 64348 KB Output is correct
39 Correct 266 ms 71892 KB Output is correct
40 Correct 659 ms 73876 KB Output is correct
41 Correct 7 ms 8064 KB Output is correct
42 Correct 7 ms 8064 KB Output is correct
43 Correct 7 ms 8064 KB Output is correct
44 Correct 7 ms 8064 KB Output is correct
45 Correct 7 ms 8064 KB Output is correct
46 Correct 7 ms 8064 KB Output is correct
47 Correct 7 ms 8064 KB Output is correct
48 Correct 7 ms 8064 KB Output is correct
49 Correct 7 ms 8064 KB Output is correct
50 Correct 7 ms 8064 KB Output is correct
51 Correct 316 ms 64192 KB Output is correct
52 Correct 291 ms 71892 KB Output is correct
53 Correct 654 ms 73872 KB Output is correct
54 Correct 224 ms 63104 KB Output is correct
55 Correct 319 ms 63736 KB Output is correct
56 Correct 289 ms 72020 KB Output is correct
57 Correct 570 ms 74324 KB Output is correct
58 Correct 228 ms 62972 KB Output is correct
59 Correct 314 ms 64200 KB Output is correct
60 Correct 294 ms 72004 KB Output is correct
61 Correct 582 ms 74488 KB Output is correct
62 Correct 224 ms 62940 KB Output is correct
63 Correct 331 ms 63924 KB Output is correct
64 Correct 293 ms 72020 KB Output is correct
65 Correct 654 ms 74264 KB Output is correct
66 Correct 223 ms 63364 KB Output is correct
67 Correct 312 ms 63676 KB Output is correct
68 Correct 286 ms 71896 KB Output is correct
69 Correct 576 ms 73688 KB Output is correct
70 Correct 229 ms 63528 KB Output is correct