Submission #103605

# Submission time Handle Problem Language Result Execution time Memory
103605 2019-03-31T22:57:22 Z luciocf Election Campaign (JOI15_election_campaign) C++14
35 / 100
275 ms 23404 KB
#include <bits/stdc++.h>

using namespace std;

const int maxn = 1e5+10;

int n, m;

int A[maxn], B[maxn], C[maxn];

int nivel[maxn], pai[maxn], sz[maxn];

int chain[maxn], head[maxn], pos[maxn];
int cc, pp;

int bit[maxn];

int dp[maxn];

vector<int> grafo[maxn], op[maxn];

void upd(int x, int v)
{
	for (; x < maxn; x += (x&-x))
		bit[x] += v;
}

int soma(int x)
{
	int ans = 0;
	for (; x > 0; x -= (x&-x))
		ans += bit[x];
	return ans;
}

void dfs(int u, int p)
{
	sz[u] = 1;

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

		pai[v] = u, nivel[v] = nivel[u]+1;

		dfs(v, u);

		sz[u] += sz[v];
	}
}

void hld(int u)
{
	if (!head[cc]) head[cc] = u;
	chain[u] = cc, pos[u] = ++pp;

	int maior = -1, ind = -1;
	for (auto v: grafo[u])
		if (v != pai[u] && sz[v] > maior)
			maior = sz[v], ind = v;

	if (ind != -1) hld(ind);

	for (auto v: grafo[u])
		if (v != pai[u] && v != ind)
			cc++, hld(v);
}

int lca(int u, int v)
{
	while (chain[u] != chain[v])
	{
		if (nivel[head[chain[u]]] > nivel[head[chain[v]]])
			u = pai[head[chain[u]]];
		else
			v = pai[head[chain[v]]];
	}

	if (nivel[u] > nivel[v]) return v;
	return u;
}

int query(int u, int v)
{
	int ans = 0;

	while (true)
	{
		if (chain[u] == chain[v]) return ans+soma(pos[u])-soma(pos[v]);

		ans += soma(pos[u])-soma(pos[head[chain[u]]]-1);
		u = pai[head[chain[u]]];
	}
}

int solve(int u, int p)
{
	int ans = 0;
	for (auto v: grafo[u])
	{
		if (v == p) continue;

		solve(v, u);

		ans += dp[v];
	}

	dp[u] = ans;

	for (auto i: op[u])
	{
		int a = A[i], b = B[i], c = C[i];
		int x = query(a, u) + query(b, u);

		if (c > x && ans-x+c > dp[u])
		{
			dp[u] = ans-x+c;
			upd(pos[u], c-(soma(pos[u])-soma(pos[u]-1)));
		}
	}
}

int main(void)
{
	scanf("%d", &n);

	for (int i = 1; i < n; i++)
	{
		int u, v;
		scanf("%d %d", &u, &v);

		grafo[u].push_back(v); grafo[v].push_back(u);
	}

	dfs(1, 0);
	hld(1);

	scanf("%d", &m);

	for (int i = 1; i <= m; i++)
	{
		scanf("%d %d %d", &A[i], &B[i], &C[i]);

		op[lca(A[i], B[i])].push_back(i);
	}

	solve(1, 0);

	printf("%d\n", dp[1]);
}

Compilation message

election_campaign.cpp: In function 'int solve(int, int)':
election_campaign.cpp:121:1: warning: no return statement in function returning non-void [-Wreturn-type]
 }
 ^
election_campaign.cpp: In function 'int main()':
election_campaign.cpp:125:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &n);
  ~~~~~^~~~~~~~~~
election_campaign.cpp:130:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d", &u, &v);
   ~~~~~^~~~~~~~~~~~~~~~~
election_campaign.cpp:138:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &m);
  ~~~~~^~~~~~~~~~
election_campaign.cpp:142:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d %d", &A[i], &B[i], &C[i]);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 8 ms 5120 KB Output is correct
2 Correct 7 ms 5120 KB Output is correct
3 Incorrect 7 ms 5068 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5120 KB Output is correct
2 Correct 6 ms 5120 KB Output is correct
3 Correct 7 ms 5248 KB Output is correct
4 Correct 119 ms 23160 KB Output is correct
5 Correct 126 ms 23212 KB Output is correct
6 Correct 163 ms 23356 KB Output is correct
7 Correct 108 ms 23160 KB Output is correct
8 Correct 117 ms 23212 KB Output is correct
9 Correct 110 ms 23404 KB Output is correct
10 Correct 115 ms 23140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5120 KB Output is correct
2 Correct 6 ms 5120 KB Output is correct
3 Correct 7 ms 5248 KB Output is correct
4 Correct 119 ms 23160 KB Output is correct
5 Correct 126 ms 23212 KB Output is correct
6 Correct 163 ms 23356 KB Output is correct
7 Correct 108 ms 23160 KB Output is correct
8 Correct 117 ms 23212 KB Output is correct
9 Correct 110 ms 23404 KB Output is correct
10 Correct 115 ms 23140 KB Output is correct
11 Incorrect 15 ms 6016 KB Output isn't correct
12 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 275 ms 15484 KB Output is correct
2 Correct 113 ms 23372 KB Output is correct
3 Correct 186 ms 20344 KB Output is correct
4 Correct 149 ms 15628 KB Output is correct
5 Correct 179 ms 20064 KB Output is correct
6 Correct 148 ms 15604 KB Output is correct
7 Correct 180 ms 19628 KB Output is correct
8 Correct 252 ms 15864 KB Output is correct
9 Correct 142 ms 23160 KB Output is correct
10 Correct 269 ms 18804 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 5120 KB Output is correct
2 Correct 7 ms 5120 KB Output is correct
3 Incorrect 7 ms 5068 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 8 ms 5120 KB Output is correct
2 Correct 7 ms 5120 KB Output is correct
3 Incorrect 7 ms 5068 KB Output isn't correct
4 Halted 0 ms 0 KB -