Submission #366544

# Submission time Handle Problem Language Result Execution time Memory
366544 2021-02-14T12:24:07 Z Rainbowbunny Election Campaign (JOI15_election_campaign) C++17
10 / 100
335 ms 35180 KB
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <chrono>
#include <random>
#include <bitset>
#include <utility>

int n, timer;
int tin[100005], tout[100005], up[20][100005];
std::vector <int> Adj[100005];

int m;
int A[100005], B[100005], C[100005];
int dp[100005];
std::vector <int> Query[100005];

int BIT[100005];

void Add(int id, int value)
{
	for(id; id <= n; id += id & -id)
	{
		BIT[id] += value; 
	}
}

int Get(int id)
{
	int ans = 0;
	for(id; id > 0; id -= id & -id)
	{
		ans += BIT[id];
	}
	return ans;
}

void DFS(int node, int p)
{
	timer++;
	tin[node] = timer;
	up[0][node] = p;
	for(int i = 1; i <= 19; i++)
	{
		up[i][node] = up[i - 1][up[i - 1][node]];
	}
	for(auto x : Adj[node])
	{
		if(x == p)
		{
			continue;
		}
		DFS(x, node);
	}
	tout[node] = timer;
}

int Isparent(int u, int v)
{
	return tin[u] <= tin[v] and tout[u] >= tout[v];
}

int LCA(int u, int v)
{
	if(Isparent(u, v))
	{
		return u;
	}
	for(int i = 19; i >= 0; i--)
	{
		if(!Isparent(up[i][u], v))
		{
			u = up[i][u];
		}
	}
	return up[0][u];
}

int Subtree(int u, int v)
{
	assert(Isparent(u, v));
	for(int i = 19; i >= 0; i--)
	{
		if(!Isparent(up[i][v], u))
		{
			v = up[i][v];
		}
	}
	return v;
}

void Caldp(int node, int p)
{
	int sumdp = 0;
	for(auto x : Adj[node])
	{
		if(x == p)
		{
			continue;
		}
		Caldp(x, node);
		sumdp += dp[x];
		dp[node] = std::max(dp[node], dp[x]);
	}
	for(auto x : Query[node])
	{
		if(A[x] == B[x])
		{
			dp[node] = std::max(dp[node], sumdp + C[x]);
		}
		else if(A[x] == node)
		{
			dp[node] = std::max(dp[node], sumdp - dp[Subtree(A[x], B[x])] + Get(tin[B[x]]) + C[x]);	
		}
		else if(B[x] == node)
		{
			dp[node] = std::max(dp[node], sumdp - dp[Subtree(B[x], A[x])] + Get(tin[A[x]]) + C[x]);
		}
		else
		{
			dp[node] = std::max(dp[node], sumdp - dp[Subtree(node, A[x])] - dp[Subtree(node, B[x])] + Get(tin[A[x]]) + Get(tin[B[x]]) + C[x]);
		}
	}
	for(auto x : Adj[node])
	{
		if(x == p)
		{
			continue;
		}
		Add(tin[x], sumdp - dp[x]);
		Add(tout[x] + 1, dp[x] - sumdp);
	}
	Add(tin[node], sumdp);
	Add(tin[node] + 1, -sumdp);
}

int main()
{
	std::ios_base::sync_with_stdio(false);
	std::cin.tie(0);
	std::cout.tie(0);
	std::cin >> n;
	for(int i = 1; i < n; i++)
	{
		int u, v;
		std::cin >> u >> v;
		Adj[u].push_back(v);
		Adj[v].push_back(u);
	}
	DFS(1, 1);
	std::cin >> m;
	for(int i = 1; i <= m; i++)
	{
		std::cin >> A[i] >> B[i] >> C[i];
		Query[LCA(A[i], B[i])].push_back(i);
	}
	Caldp(1, 1);
	std::cout << *std::max_element(dp + 1, dp + n + 1);
}

Compilation message

election_campaign.cpp: In function 'void Add(int, int)':
election_campaign.cpp:29:6: warning: statement has no effect [-Wunused-value]
   29 |  for(id; id <= n; id += id & -id)
      |      ^~
election_campaign.cpp: In function 'int Get(int)':
election_campaign.cpp:38:6: warning: statement has no effect [-Wunused-value]
   38 |  for(id; id > 0; id -= id & -id)
      |      ^~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5248 KB Output is correct
2 Correct 4 ms 5100 KB Output is correct
3 Correct 3 ms 5100 KB Output is correct
4 Incorrect 4 ms 5356 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5120 KB Output is correct
2 Correct 3 ms 5100 KB Output is correct
3 Correct 4 ms 5536 KB Output is correct
4 Correct 213 ms 34796 KB Output is correct
5 Correct 195 ms 34796 KB Output is correct
6 Correct 154 ms 34796 KB Output is correct
7 Correct 192 ms 34944 KB Output is correct
8 Correct 195 ms 34796 KB Output is correct
9 Correct 141 ms 34796 KB Output is correct
10 Correct 183 ms 34852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5120 KB Output is correct
2 Correct 3 ms 5100 KB Output is correct
3 Correct 4 ms 5536 KB Output is correct
4 Correct 213 ms 34796 KB Output is correct
5 Correct 195 ms 34796 KB Output is correct
6 Correct 154 ms 34796 KB Output is correct
7 Correct 192 ms 34944 KB Output is correct
8 Correct 195 ms 34796 KB Output is correct
9 Correct 141 ms 34796 KB Output is correct
10 Correct 183 ms 34852 KB Output is correct
11 Correct 20 ms 6124 KB Output is correct
12 Correct 209 ms 35052 KB Output is correct
13 Correct 183 ms 35052 KB Output is correct
14 Correct 146 ms 35180 KB Output is correct
15 Correct 212 ms 35052 KB Output is correct
16 Correct 154 ms 35180 KB Output is correct
17 Correct 185 ms 35052 KB Output is correct
18 Correct 197 ms 35052 KB Output is correct
19 Correct 162 ms 35160 KB Output is correct
20 Correct 189 ms 35052 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 335 ms 19432 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5248 KB Output is correct
2 Correct 4 ms 5100 KB Output is correct
3 Correct 3 ms 5100 KB Output is correct
4 Incorrect 4 ms 5356 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5248 KB Output is correct
2 Correct 4 ms 5100 KB Output is correct
3 Correct 3 ms 5100 KB Output is correct
4 Incorrect 4 ms 5356 KB Output isn't correct
5 Halted 0 ms 0 KB -