Submission #103632

# Submission time Handle Problem Language Result Execution time Memory
103632 2019-04-01T13:57:04 Z luciocf Election Campaign (JOI15_election_campaign) C++14
35 / 100
246 ms 20856 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;
 
	int cur = 0;
 
	for (auto i: op[u])
	{
		int a = A[i], b = B[i];
		int x = query(a, u) + query(b, u);

		if (x == 0)
		{
			dp[u ] = ans+1;
			upd(pos[u], 1);
			break;
		}
	}
}
 
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:110:6: warning: unused variable 'cur' [-Wunused-variable]
  int cur = 0;
      ^~~
election_campaign.cpp:124:1: warning: no return statement in function returning non-void [-Wreturn-type]
 }
 ^
election_campaign.cpp: In function 'int main()':
election_campaign.cpp:128:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &n);
  ~~~~~^~~~~~~~~~
election_campaign.cpp:133: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:141:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &m);
  ~~~~~^~~~~~~~~~
election_campaign.cpp:145: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 Incorrect 6 ms 5120 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5120 KB Output is correct
2 Correct 6 ms 5120 KB Output is correct
3 Correct 8 ms 5248 KB Output is correct
4 Correct 121 ms 20708 KB Output is correct
5 Correct 156 ms 20728 KB Output is correct
6 Correct 118 ms 20856 KB Output is correct
7 Correct 126 ms 20700 KB Output is correct
8 Correct 135 ms 20788 KB Output is correct
9 Correct 124 ms 20728 KB Output is correct
10 Correct 132 ms 20728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5120 KB Output is correct
2 Correct 6 ms 5120 KB Output is correct
3 Correct 8 ms 5248 KB Output is correct
4 Correct 121 ms 20708 KB Output is correct
5 Correct 156 ms 20728 KB Output is correct
6 Correct 118 ms 20856 KB Output is correct
7 Correct 126 ms 20700 KB Output is correct
8 Correct 135 ms 20788 KB Output is correct
9 Correct 124 ms 20728 KB Output is correct
10 Correct 132 ms 20728 KB Output is correct
11 Incorrect 17 ms 5736 KB Output isn't correct
12 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 239 ms 12992 KB Output is correct
2 Correct 124 ms 20788 KB Output is correct
3 Correct 242 ms 17912 KB Output is correct
4 Correct 136 ms 13168 KB Output is correct
5 Correct 211 ms 17628 KB Output is correct
6 Correct 142 ms 13300 KB Output is correct
7 Correct 227 ms 17244 KB Output is correct
8 Correct 245 ms 13432 KB Output is correct
9 Correct 141 ms 20728 KB Output is correct
10 Correct 246 ms 16316 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 6 ms 5120 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 6 ms 5120 KB Output isn't correct
2 Halted 0 ms 0 KB -