Submission #287863

# Submission time Handle Problem Language Result Execution time Memory
287863 2020-09-01T05:15:51 Z 임성재(#5782) Election Campaign (JOI15_election_campaign) C++17
0 / 100
514 ms 21040 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];

void dfs(int x) {
	for(int i=1; i<=20; i++) 
		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];

	for(auto i : qr[x]) {
		ll sum = i.se;
		
		int u=0, v=0;
		for(int j=i.fi.fi; j != x; j = sp[j][0]) {
			sum += Dp[j] - dp[u];
			u = j;
		}

		for(int j=i.fi.se; j != x; j = sp[j][0]) {
			sum += Dp[j] - dp[v];
			v = j;
		}

		sum += Dp[x] - dp[u] - dp[v];

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

	cout << x << " " << dp[x] << " " << Dp[x] << endl;
}

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);
	}

	f(1);

	cout << dp[1];
}
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 4992 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 4992 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 4992 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 514 ms 21040 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 4992 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 4992 KB Output isn't correct
2 Halted 0 ms 0 KB -