Submission #71026

# Submission time Handle Problem Language Result Execution time Memory
71026 2018-08-24T03:09:29 Z MatheusLealV Election Campaign (JOI15_election_campaign) C++17
10 / 100
281 ms 31104 KB
#include <bits/stdc++.h>
#define N 100050
#define logn 20
using namespace std;

struct qr{
	int a, b, c;
};

vector<qr> Q[N];

int n, q, anc[N][logn], deep[N], ini[N], fim[N], t;

vector<int> grafo[N];

int LCA(int u, int v)
{
	if(deep[u] < deep[v]) swap(u, v);

	for(int i = logn - 1; i >= 0; i--)
		if(deep[u] - (1<<i) >= deep[v]) u = anc[u][i];

	if(u == v) return u;

	for(int i = logn - 1; i >= 0; i--)
		if(anc[u][i] != -1 and anc[v][i] != -1 and anc[u][i] != anc[v][i])
			u = anc[u][i], v = anc[v][i];

	return anc[u][0];
}

void dfs(int x, int p)
{
	anc[x][0] = p;

	ini[x] = ++t;

	for(auto v: grafo[x])
	{
		if(v == p) continue;

		deep[v] = deep[x] + 1;

		dfs(v, x);
	}

	fim[x] = t;
}

int dp[N][2];

int solve(int x, int f)
{
	if(dp[x][f] != -1) return dp[x][f];

	dp[x][f] = 0;

	for(auto v: grafo[x])
	{
		if(v == anc[x][0]) continue;

		dp[x][f] += solve(v, 0);
	}

	if(f) return dp[x][f];

	for(auto q: Q[x])
	{
		int a = q.a, b = q.b, c = q.c;

		int ans = c + (a != x ? solve(a, 1) : 0) + (b != x ? solve(b, 1) : 0);

		for(auto v: grafo[x])
		{
			if(v == anc[x][0]) continue;

			if(ini[v] <= ini[a] and ini[a] <= fim[v]) continue;

			if(ini[v] <= ini[b] and ini[b] <= fim[v]) continue;

			ans += solve(v, 0);
		}

		dp[x][f] = max(ans, dp[x][f]);
	}

	return dp[x][f];
}

int main()
{
	ios::sync_with_stdio(false); cin.tie(0);

	cin>>n;

	for(int i = 1, a, b; i < n; i++)
	{
		cin>>a>>b;

		grafo[a].push_back(b);

		grafo[b].push_back(a);
	}

	memset(anc, -1, sizeof anc);

	dfs(1, 1);

	for(int j = 1; j < logn; j++)
		for(int i = 1; i <= n; i++) anc[i][j] = anc[anc[i][j - 1]][j - 1];

	cin>>q;

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

		int L = LCA(a, b);

		Q[L].push_back({a, b, c});
	}

	memset(dp, -1, sizeof dp);

	cout<<solve(1, 0)<<"\n";
}
# Verdict Execution time Memory Grader output
1 Correct 15 ms 13688 KB Output is correct
2 Correct 14 ms 13688 KB Output is correct
3 Incorrect 15 ms 13752 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 14 ms 13832 KB Output is correct
2 Correct 15 ms 13832 KB Output is correct
3 Correct 13 ms 13960 KB Output is correct
4 Correct 229 ms 30520 KB Output is correct
5 Correct 238 ms 31024 KB Output is correct
6 Correct 186 ms 31024 KB Output is correct
7 Correct 193 ms 31024 KB Output is correct
8 Correct 214 ms 31024 KB Output is correct
9 Correct 159 ms 31024 KB Output is correct
10 Correct 243 ms 31024 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 13832 KB Output is correct
2 Correct 15 ms 13832 KB Output is correct
3 Correct 13 ms 13960 KB Output is correct
4 Correct 229 ms 30520 KB Output is correct
5 Correct 238 ms 31024 KB Output is correct
6 Correct 186 ms 31024 KB Output is correct
7 Correct 193 ms 31024 KB Output is correct
8 Correct 214 ms 31024 KB Output is correct
9 Correct 159 ms 31024 KB Output is correct
10 Correct 243 ms 31024 KB Output is correct
11 Correct 26 ms 31024 KB Output is correct
12 Correct 253 ms 31024 KB Output is correct
13 Correct 244 ms 31024 KB Output is correct
14 Correct 194 ms 31024 KB Output is correct
15 Correct 256 ms 31028 KB Output is correct
16 Correct 229 ms 31104 KB Output is correct
17 Correct 228 ms 31104 KB Output is correct
18 Correct 281 ms 31104 KB Output is correct
19 Correct 206 ms 31104 KB Output is correct
20 Correct 269 ms 31104 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 244 ms 31104 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 15 ms 13688 KB Output is correct
2 Correct 14 ms 13688 KB Output is correct
3 Incorrect 15 ms 13752 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 15 ms 13688 KB Output is correct
2 Correct 14 ms 13688 KB Output is correct
3 Incorrect 15 ms 13752 KB Output isn't correct
4 Halted 0 ms 0 KB -