Submission #417557

# Submission time Handle Problem Language Result Execution time Memory
417557 2021-06-03T23:58:47 Z MohamedAhmed04 Election Campaign (JOI15_election_campaign) C++14
100 / 100
240 ms 34048 KB
#include <bits/stdc++.h>

using namespace std ;

const int MAX = 1e5 + 10 ;

int n , m ;

int P[MAX][17] , dep[MAX] ;
int in[MAX] , out[MAX] ;

vector< vector<int> >adj(MAX) ;

int tim = 0 ;

void dfs(int node , int par)
{
	in[node] = ++tim ;
	P[node][0] = par ;
	for(int j = 1 ; j < 17 ; ++j)
		P[node][j] = P[P[node][j-1]][j-1] ;
	for(auto &child : adj[node])
	{
		if(child == par)
			continue ;
		dep[child] = dep[node] + 1 ;
		dfs(child , node) ;
	}
	out[node] = tim ;
}

int kth_ancestor(int node , int k)
{
	for(int j = 16 ; j >= 0 ; --j)
	{
		if((1 << j) <= k)
			node = P[node][j] , k -= (1 << j) ;
	}
	return node ;
}

int LCA(int x , int y)
{
	if(dep[x] < dep[y])
		swap(x , y) ;
	for(int j = 16 ; j >= 0 ; --j)
	{
		if(dep[x] - (1 << j) >= dep[y])
			x = P[x][j] ;
	}
	if(x == y)
		return x ;
	for(int j = 16 ; j >= 0 ; --j)
	{
		if(P[x][j] != P[y][j])
			x = P[x][j] , y = P[y][j] ;
	}
	return P[x][0] ;
}

int bit[MAX] ;

void add(int i , int x)
{
	for(; i <= n ; i += (i & (-i)))
		bit[i] += x ;
}

int get(int i)
{
	int sum = 0 ;
	for(; i > 0 ; i -= (i & (-i)))
		sum += bit[i] ;
	return sum ;
}

vector< array<int , 3> >v[MAX] ;

int dp[MAX] ;

void dfs2(int node , int par)
{
	int sum = 0 ;
	for(auto &child : adj[node])
	{
		if(child == par)
			continue ;
		dfs2(child , node) ;
		sum += dp[child] ;
	}
	add(in[node] , sum) ;
	add(in[node]+1 , -sum) ;
	dp[node] = sum ;
	for(auto &a : v[node])
	{
		int x = a[0] , y = a[1] , z = a[2] ;
		int now = sum + z ;
		if(x != node)
			now += get(in[x]) , now -= dp[kth_ancestor(x , dep[x] - dep[node] - 1)] ;
		if(y != node)
			now += get(in[y]) , now -= dp[kth_ancestor(y , dep[y] - dep[node] - 1)] ;
		dp[node] = max(dp[node] , now) ;
	}
	for(auto &child : adj[node])
	{
		if(child == par)
			continue ;
		sum -= dp[child] ;
		add(in[child] , sum) ;
		add(out[child]+1 , -sum) ;
		sum += dp[child] ;
	}
}

int main()
{
	ios_base::sync_with_stdio(0) ;
	cin.tie(0) ;
	cin>>n ;
	for(int i = 0 ; i < n-1 ; ++i)
	{
		int x , y ;
		cin>>x>>y ;
		adj[x].push_back(y) ;
		adj[y].push_back(x) ;
	}
	dfs(1 , 0) ;
	int m ;
	cin>>m ;
	while(m--)
	{
		int x , y , z ;
		cin>>x>>y>>z ;
		v[LCA(x , y)].push_back({x , y , z}) ;
	}
	dfs2(1 , 0) ;
	return cout<<dp[1]<<"\n" , 0 ;
}		
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 3 ms 5172 KB Output is correct
5 Correct 101 ms 18060 KB Output is correct
6 Correct 60 ms 30384 KB Output is correct
7 Correct 100 ms 26092 KB Output is correct
8 Correct 80 ms 18432 KB Output is correct
9 Correct 104 ms 23592 KB Output is correct
10 Correct 79 ms 18408 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 4 ms 5196 KB Output is correct
4 Correct 142 ms 33656 KB Output is correct
5 Correct 144 ms 33640 KB Output is correct
6 Correct 124 ms 33688 KB Output is correct
7 Correct 148 ms 33764 KB Output is correct
8 Correct 139 ms 33680 KB Output is correct
9 Correct 123 ms 33656 KB Output is correct
10 Correct 137 ms 33640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 4 ms 5196 KB Output is correct
4 Correct 142 ms 33656 KB Output is correct
5 Correct 144 ms 33640 KB Output is correct
6 Correct 124 ms 33688 KB Output is correct
7 Correct 148 ms 33764 KB Output is correct
8 Correct 139 ms 33680 KB Output is correct
9 Correct 123 ms 33656 KB Output is correct
10 Correct 137 ms 33640 KB Output is correct
11 Correct 17 ms 6104 KB Output is correct
12 Correct 152 ms 33980 KB Output is correct
13 Correct 141 ms 33976 KB Output is correct
14 Correct 130 ms 33988 KB Output is correct
15 Correct 146 ms 33988 KB Output is correct
16 Correct 127 ms 33996 KB Output is correct
17 Correct 147 ms 33884 KB Output is correct
18 Correct 145 ms 33928 KB Output is correct
19 Correct 125 ms 33920 KB Output is correct
20 Correct 149 ms 34048 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 173 ms 20732 KB Output is correct
2 Correct 126 ms 33688 KB Output is correct
3 Correct 240 ms 29052 KB Output is correct
4 Correct 132 ms 21244 KB Output is correct
5 Correct 196 ms 28068 KB Output is correct
6 Correct 135 ms 21156 KB Output is correct
7 Correct 233 ms 27776 KB Output is correct
8 Correct 183 ms 21160 KB Output is correct
9 Correct 126 ms 33648 KB Output is correct
10 Correct 236 ms 26152 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 3 ms 5172 KB Output is correct
5 Correct 101 ms 18060 KB Output is correct
6 Correct 60 ms 30384 KB Output is correct
7 Correct 100 ms 26092 KB Output is correct
8 Correct 80 ms 18432 KB Output is correct
9 Correct 104 ms 23592 KB Output is correct
10 Correct 79 ms 18408 KB Output is correct
11 Correct 4 ms 5196 KB Output is correct
12 Correct 4 ms 5324 KB Output is correct
13 Correct 4 ms 5180 KB Output is correct
14 Correct 4 ms 5196 KB Output is correct
15 Correct 5 ms 5196 KB Output is correct
16 Correct 5 ms 5180 KB Output is correct
17 Correct 4 ms 5180 KB Output is correct
18 Correct 5 ms 5196 KB Output is correct
19 Correct 4 ms 5180 KB Output is correct
20 Correct 5 ms 5324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 3 ms 5172 KB Output is correct
5 Correct 101 ms 18060 KB Output is correct
6 Correct 60 ms 30384 KB Output is correct
7 Correct 100 ms 26092 KB Output is correct
8 Correct 80 ms 18432 KB Output is correct
9 Correct 104 ms 23592 KB Output is correct
10 Correct 79 ms 18408 KB Output is correct
11 Correct 3 ms 4940 KB Output is correct
12 Correct 3 ms 4940 KB Output is correct
13 Correct 4 ms 5196 KB Output is correct
14 Correct 142 ms 33656 KB Output is correct
15 Correct 144 ms 33640 KB Output is correct
16 Correct 124 ms 33688 KB Output is correct
17 Correct 148 ms 33764 KB Output is correct
18 Correct 139 ms 33680 KB Output is correct
19 Correct 123 ms 33656 KB Output is correct
20 Correct 137 ms 33640 KB Output is correct
21 Correct 17 ms 6104 KB Output is correct
22 Correct 152 ms 33980 KB Output is correct
23 Correct 141 ms 33976 KB Output is correct
24 Correct 130 ms 33988 KB Output is correct
25 Correct 146 ms 33988 KB Output is correct
26 Correct 127 ms 33996 KB Output is correct
27 Correct 147 ms 33884 KB Output is correct
28 Correct 145 ms 33928 KB Output is correct
29 Correct 125 ms 33920 KB Output is correct
30 Correct 149 ms 34048 KB Output is correct
31 Correct 173 ms 20732 KB Output is correct
32 Correct 126 ms 33688 KB Output is correct
33 Correct 240 ms 29052 KB Output is correct
34 Correct 132 ms 21244 KB Output is correct
35 Correct 196 ms 28068 KB Output is correct
36 Correct 135 ms 21156 KB Output is correct
37 Correct 233 ms 27776 KB Output is correct
38 Correct 183 ms 21160 KB Output is correct
39 Correct 126 ms 33648 KB Output is correct
40 Correct 236 ms 26152 KB Output is correct
41 Correct 4 ms 5196 KB Output is correct
42 Correct 4 ms 5324 KB Output is correct
43 Correct 4 ms 5180 KB Output is correct
44 Correct 4 ms 5196 KB Output is correct
45 Correct 5 ms 5196 KB Output is correct
46 Correct 5 ms 5180 KB Output is correct
47 Correct 4 ms 5180 KB Output is correct
48 Correct 5 ms 5196 KB Output is correct
49 Correct 4 ms 5180 KB Output is correct
50 Correct 5 ms 5324 KB Output is correct
51 Correct 187 ms 21512 KB Output is correct
52 Correct 143 ms 33988 KB Output is correct
53 Correct 231 ms 26596 KB Output is correct
54 Correct 133 ms 21504 KB Output is correct
55 Correct 175 ms 21164 KB Output is correct
56 Correct 144 ms 33992 KB Output is correct
57 Correct 207 ms 27704 KB Output is correct
58 Correct 152 ms 21432 KB Output is correct
59 Correct 192 ms 21392 KB Output is correct
60 Correct 144 ms 33988 KB Output is correct
61 Correct 204 ms 27740 KB Output is correct
62 Correct 142 ms 21188 KB Output is correct
63 Correct 172 ms 20980 KB Output is correct
64 Correct 146 ms 33908 KB Output is correct
65 Correct 240 ms 27772 KB Output is correct
66 Correct 135 ms 21504 KB Output is correct
67 Correct 172 ms 21032 KB Output is correct
68 Correct 143 ms 34004 KB Output is correct
69 Correct 212 ms 25988 KB Output is correct
70 Correct 138 ms 21544 KB Output is correct