Submission #248680

# Submission time Handle Problem Language Result Execution time Memory
248680 2020-07-13T06:40:12 Z Lawliet Election Campaign (JOI15_election_campaign) C++17
100 / 100
284 ms 31616 KB
#include <bits/stdc++.h>

using namespace std;

const int LOG = 20;
const int MAXN = 100010;

class FenwickTree
{
	public:

		void update(int i, int v)
		{
			for( ; i > 0 ; i -= i & -i)
				BIT[i] += v;
		}

		int query(int i)
		{
			int ans = 0;

			for( ; i < MAXN ; i += i & -i)
				ans += BIT[i];

			return ans;
		}

		void update(int L, int R, int v)
		{
			update( R , v );
			update( L - 1 , -v );
		}

		FenwickTree() { memset( BIT , 0 , sizeof(BIT) ); }

	private:

		int BIT[MAXN];
};

int n, m;
int curTime;

int depth[MAXN];
int tab[LOG][MAXN];
int dp[MAXN], f[MAXN];
int tin[MAXN], tout[MAXN];
int A[MAXN], B[MAXN], C[MAXN];

vector<int> adj[MAXN];
vector<int> groupLCA[MAXN];

FenwickTree sumDP, sumF;

void DFSInit(int cur, int p)
{
	tab[0][cur] = p;
	tin[cur] = ++curTime;
	depth[cur] = depth[p] + 1;

	for(int k = 1 ; k < LOG ; k++)
		tab[k][cur] = tab[k - 1][ tab[k - 1][cur] ];

	for(int i = 0 ; i < (int)adj[cur].size() ; i++)
		if( adj[cur][i] != p ) DFSInit( adj[cur][i] , cur );

	tout[cur] = curTime;
}

void DFSCalculate(int cur, int p)
{
	for(int i = 0 ; i < (int)adj[cur].size() ; i++)
	{
		int viz = adj[cur][i];

		if( viz == p ) continue;

		DFSCalculate( viz , cur );
		f[cur] += dp[viz];
	}

	dp[cur] = f[cur];

	for(int i = 0 ; i < (int)groupLCA[cur].size() ; i++)
	{
		int ind = groupLCA[cur][i];

		int curAns = C[ind] + f[cur];
		curAns += sumF.query( tin[ A[ind] ] ) - sumDP.query( tin[ A[ind] ] );
		curAns += sumF.query( tin[ B[ind] ] ) - sumDP.query( tin[ B[ind] ] );

		dp[cur] = max( dp[cur] , curAns );
	}

	sumF.update( tin[cur] , tout[cur] , f[cur] );
	sumDP.update( tin[cur] , tout[cur] , dp[cur] );
}

int LCA(int A, int B)
{
	if( depth[A] < depth[B] ) swap( A , B );

	int d = depth[A] - depth[B];

	for(int k = 0 ; k < LOG ; k++)
		if( d & (1 << k) ) A = tab[k][A];

	if( A == B ) return A;

	for(int k = LOG - 1 ; k >= 0 ; k--)
	{
		if( tab[k][A] != tab[k][B] )
		{
			A = tab[k][A];
			B = tab[k][B];
		}
	}

	return tab[0][A];
}

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

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

		adj[U].push_back( V );
		adj[V].push_back( U );
	}

	DFSInit( 1 , 1 );

	scanf("%d",&m);

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

		int L = LCA( A[i] , B[i] );
		groupLCA[L].push_back( i );
	}

	DFSCalculate( 1 , 1 );

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

Compilation message

election_campaign.cpp: In function 'int main()':
election_campaign.cpp:124:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d",&n);
  ~~~~~^~~~~~~~~
election_campaign.cpp:129: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:137:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d",&m);
  ~~~~~^~~~~~~~~
election_campaign.cpp:141: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 4 ms 5888 KB Output is correct
2 Correct 4 ms 5888 KB Output is correct
3 Correct 4 ms 5888 KB Output is correct
4 Correct 4 ms 6144 KB Output is correct
5 Correct 111 ms 20096 KB Output is correct
6 Correct 61 ms 27640 KB Output is correct
7 Correct 129 ms 24952 KB Output is correct
8 Correct 86 ms 20088 KB Output is correct
9 Correct 115 ms 23476 KB Output is correct
10 Correct 90 ms 20088 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5888 KB Output is correct
2 Correct 3 ms 5888 KB Output is correct
3 Correct 4 ms 6144 KB Output is correct
4 Correct 141 ms 31272 KB Output is correct
5 Correct 148 ms 31480 KB Output is correct
6 Correct 127 ms 31352 KB Output is correct
7 Correct 137 ms 31224 KB Output is correct
8 Correct 141 ms 31224 KB Output is correct
9 Correct 145 ms 31608 KB Output is correct
10 Correct 140 ms 31224 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5888 KB Output is correct
2 Correct 3 ms 5888 KB Output is correct
3 Correct 4 ms 6144 KB Output is correct
4 Correct 141 ms 31272 KB Output is correct
5 Correct 148 ms 31480 KB Output is correct
6 Correct 127 ms 31352 KB Output is correct
7 Correct 137 ms 31224 KB Output is correct
8 Correct 141 ms 31224 KB Output is correct
9 Correct 145 ms 31608 KB Output is correct
10 Correct 140 ms 31224 KB Output is correct
11 Correct 17 ms 6912 KB Output is correct
12 Correct 141 ms 31556 KB Output is correct
13 Correct 139 ms 31484 KB Output is correct
14 Correct 134 ms 31608 KB Output is correct
15 Correct 139 ms 31480 KB Output is correct
16 Correct 147 ms 31608 KB Output is correct
17 Correct 157 ms 31480 KB Output is correct
18 Correct 156 ms 31608 KB Output is correct
19 Correct 143 ms 31608 KB Output is correct
20 Correct 141 ms 31480 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 205 ms 23048 KB Output is correct
2 Correct 141 ms 31352 KB Output is correct
3 Correct 273 ms 28152 KB Output is correct
4 Correct 163 ms 23176 KB Output is correct
5 Correct 227 ms 27896 KB Output is correct
6 Correct 156 ms 23028 KB Output is correct
7 Correct 262 ms 27768 KB Output is correct
8 Correct 212 ms 23416 KB Output is correct
9 Correct 123 ms 31352 KB Output is correct
10 Correct 284 ms 26752 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5888 KB Output is correct
2 Correct 4 ms 5888 KB Output is correct
3 Correct 4 ms 5888 KB Output is correct
4 Correct 4 ms 6144 KB Output is correct
5 Correct 111 ms 20096 KB Output is correct
6 Correct 61 ms 27640 KB Output is correct
7 Correct 129 ms 24952 KB Output is correct
8 Correct 86 ms 20088 KB Output is correct
9 Correct 115 ms 23476 KB Output is correct
10 Correct 90 ms 20088 KB Output is correct
11 Correct 6 ms 6144 KB Output is correct
12 Correct 6 ms 6144 KB Output is correct
13 Correct 5 ms 6144 KB Output is correct
14 Correct 7 ms 6144 KB Output is correct
15 Correct 5 ms 6144 KB Output is correct
16 Correct 7 ms 6144 KB Output is correct
17 Correct 5 ms 6144 KB Output is correct
18 Correct 7 ms 6144 KB Output is correct
19 Correct 5 ms 6144 KB Output is correct
20 Correct 5 ms 6144 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5888 KB Output is correct
2 Correct 4 ms 5888 KB Output is correct
3 Correct 4 ms 5888 KB Output is correct
4 Correct 4 ms 6144 KB Output is correct
5 Correct 111 ms 20096 KB Output is correct
6 Correct 61 ms 27640 KB Output is correct
7 Correct 129 ms 24952 KB Output is correct
8 Correct 86 ms 20088 KB Output is correct
9 Correct 115 ms 23476 KB Output is correct
10 Correct 90 ms 20088 KB Output is correct
11 Correct 4 ms 5888 KB Output is correct
12 Correct 3 ms 5888 KB Output is correct
13 Correct 4 ms 6144 KB Output is correct
14 Correct 141 ms 31272 KB Output is correct
15 Correct 148 ms 31480 KB Output is correct
16 Correct 127 ms 31352 KB Output is correct
17 Correct 137 ms 31224 KB Output is correct
18 Correct 141 ms 31224 KB Output is correct
19 Correct 145 ms 31608 KB Output is correct
20 Correct 140 ms 31224 KB Output is correct
21 Correct 17 ms 6912 KB Output is correct
22 Correct 141 ms 31556 KB Output is correct
23 Correct 139 ms 31484 KB Output is correct
24 Correct 134 ms 31608 KB Output is correct
25 Correct 139 ms 31480 KB Output is correct
26 Correct 147 ms 31608 KB Output is correct
27 Correct 157 ms 31480 KB Output is correct
28 Correct 156 ms 31608 KB Output is correct
29 Correct 143 ms 31608 KB Output is correct
30 Correct 141 ms 31480 KB Output is correct
31 Correct 205 ms 23048 KB Output is correct
32 Correct 141 ms 31352 KB Output is correct
33 Correct 273 ms 28152 KB Output is correct
34 Correct 163 ms 23176 KB Output is correct
35 Correct 227 ms 27896 KB Output is correct
36 Correct 156 ms 23028 KB Output is correct
37 Correct 262 ms 27768 KB Output is correct
38 Correct 212 ms 23416 KB Output is correct
39 Correct 123 ms 31352 KB Output is correct
40 Correct 284 ms 26752 KB Output is correct
41 Correct 6 ms 6144 KB Output is correct
42 Correct 6 ms 6144 KB Output is correct
43 Correct 5 ms 6144 KB Output is correct
44 Correct 7 ms 6144 KB Output is correct
45 Correct 5 ms 6144 KB Output is correct
46 Correct 7 ms 6144 KB Output is correct
47 Correct 5 ms 6144 KB Output is correct
48 Correct 7 ms 6144 KB Output is correct
49 Correct 5 ms 6144 KB Output is correct
50 Correct 5 ms 6144 KB Output is correct
51 Correct 203 ms 24056 KB Output is correct
52 Correct 136 ms 31612 KB Output is correct
53 Correct 241 ms 26996 KB Output is correct
54 Correct 145 ms 23416 KB Output is correct
55 Correct 187 ms 23416 KB Output is correct
56 Correct 137 ms 31616 KB Output is correct
57 Correct 209 ms 27640 KB Output is correct
58 Correct 148 ms 23284 KB Output is correct
59 Correct 207 ms 23676 KB Output is correct
60 Correct 152 ms 31480 KB Output is correct
61 Correct 233 ms 27856 KB Output is correct
62 Correct 160 ms 23216 KB Output is correct
63 Correct 207 ms 23388 KB Output is correct
64 Correct 148 ms 31608 KB Output is correct
65 Correct 261 ms 27564 KB Output is correct
66 Correct 147 ms 23416 KB Output is correct
67 Correct 202 ms 23412 KB Output is correct
68 Correct 141 ms 31608 KB Output is correct
69 Correct 226 ms 26592 KB Output is correct
70 Correct 166 ms 23416 KB Output is correct