Submission #71039

# Submission time Handle Problem Language Result Execution time Memory
71039 2018-08-24T04:22:32 Z MatheusLealV Election Campaign (JOI15_election_campaign) C++17
20 / 100
1000 ms 32436 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], best[N][logn], opt[N][logn][2] ;

inline void update(int x)
{
	best[x][0] = max({dp[x][0], dp[x][1], dp[anc[x][0]][0], dp[anc[x][0]][1]});

	opt[x][0][0] = max(dp[x][0], dp[anc[x][0]][1]);

	opt[x][0][1] = max(dp[x][1], dp[anc[x][0]][1]);

	for(int j = 1; j < logn; j++)
	{
		if(anc[x][j - 1] == -1) continue;

		best[x][j] = max(best[anc[x][j - 1]][j - 1], best[x][j - 1]);

		opt[x][j][0] = max(opt[anc[x][j - 1]][j - 1][0], opt[x][j - 1][0]);

		opt[x][j][1] = max(opt[anc[x][j - 1]][j - 1][1], opt[x][j - 1][1]);
	}
}

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

	int soma = 0;

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

		soma += max(solve(v, 1), solve(v, 0));
	}

	dp[x][f] = soma;

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

			int nivel = deep[x], u = a;

			int somamax = 0, soma1 = 0, soma0 = 0;

			while(u != x)
			{
				soma1 += dp[u][1];

				soma0 += dp[u][0];

				somamax += max(dp[u][1], dp[u][0]); 

				u = anc[u][0];
			}

			u = b;

			while(u != x)
			{
				soma1 += dp[u][1];

				soma0 += dp[u][0];

				somamax += max(dp[u][1], dp[u][0]); 

				u = anc[u][0];
			}

			int ans = soma + c - somamax + soma1;

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

	//update(x);

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

Compilation message

election_campaign.cpp: In function 'int solve(int, int)':
election_campaign.cpp:93:8: warning: unused variable 'nivel' [-Wunused-variable]
    int nivel = deep[x], u = a;
        ^~~~~
# Verdict Execution time Memory Grader output
1 Correct 15 ms 13688 KB Output is correct
2 Correct 13 ms 13800 KB Output is correct
3 Correct 13 ms 13800 KB Output is correct
4 Correct 16 ms 13912 KB Output is correct
5 Correct 164 ms 19472 KB Output is correct
6 Correct 83 ms 29984 KB Output is correct
7 Correct 169 ms 29984 KB Output is correct
8 Correct 131 ms 29984 KB Output is correct
9 Correct 183 ms 29984 KB Output is correct
10 Correct 124 ms 29984 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 29984 KB Output is correct
2 Correct 14 ms 29984 KB Output is correct
3 Correct 16 ms 29984 KB Output is correct
4 Execution timed out 1079 ms 32436 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 15 ms 29984 KB Output is correct
2 Correct 14 ms 29984 KB Output is correct
3 Correct 16 ms 29984 KB Output is correct
4 Execution timed out 1079 ms 32436 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 272 ms 32436 KB Output is correct
2 Execution timed out 1092 ms 32436 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 15 ms 13688 KB Output is correct
2 Correct 13 ms 13800 KB Output is correct
3 Correct 13 ms 13800 KB Output is correct
4 Correct 16 ms 13912 KB Output is correct
5 Correct 164 ms 19472 KB Output is correct
6 Correct 83 ms 29984 KB Output is correct
7 Correct 169 ms 29984 KB Output is correct
8 Correct 131 ms 29984 KB Output is correct
9 Correct 183 ms 29984 KB Output is correct
10 Correct 124 ms 29984 KB Output is correct
11 Correct 16 ms 32436 KB Output is correct
12 Correct 17 ms 32436 KB Output is correct
13 Correct 14 ms 32436 KB Output is correct
14 Correct 15 ms 32436 KB Output is correct
15 Correct 15 ms 32436 KB Output is correct
16 Correct 19 ms 32436 KB Output is correct
17 Correct 15 ms 32436 KB Output is correct
18 Correct 15 ms 32436 KB Output is correct
19 Correct 15 ms 32436 KB Output is correct
20 Correct 18 ms 32436 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 13688 KB Output is correct
2 Correct 13 ms 13800 KB Output is correct
3 Correct 13 ms 13800 KB Output is correct
4 Correct 16 ms 13912 KB Output is correct
5 Correct 164 ms 19472 KB Output is correct
6 Correct 83 ms 29984 KB Output is correct
7 Correct 169 ms 29984 KB Output is correct
8 Correct 131 ms 29984 KB Output is correct
9 Correct 183 ms 29984 KB Output is correct
10 Correct 124 ms 29984 KB Output is correct
11 Correct 15 ms 29984 KB Output is correct
12 Correct 14 ms 29984 KB Output is correct
13 Correct 16 ms 29984 KB Output is correct
14 Execution timed out 1079 ms 32436 KB Time limit exceeded
15 Halted 0 ms 0 KB -