Submission #25472

# Submission time Handle Problem Language Result Execution time Memory
25472 2017-06-22T10:04:16 Z dotorya Election Campaign (JOI15_election_campaign) C++14
100 / 100
676 ms 66452 KB
#include <stdio.h>  
#include <algorithm>  
#include <assert.h>
#include <bitset>
#include <cmath>  
#include <complex>  
#include <deque>  
#include <functional>  
#include <iostream>  
#include <limits.h>  
#include <map>  
#include <math.h>  
#include <queue>  
#include <set>  
#include <stdlib.h>  
#include <string.h>  
#include <string>  
#include <time.h>  
#include <unordered_map>  
#include <unordered_set>  
#include <vector>  

#pragma warning(disable:4996)  
#pragma comment(linker, "/STACK:336777216")  
using namespace std;

#define mp make_pair  
#define Fi first  
#define Se second  
#define pb(x) push_back(x)  
#define szz(x) ((int)(x).size())  
#define rep(i, n) for(int i=0;i<n;i++)  
#define all(x) (x).begin(), (x).end()  
#define ldb ldouble  

typedef tuple<int, int, int> t3;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <ll, int> pli;
typedef pair <db, db> pdd;

int IT_MAX = 1 << 15;
const ll MOD = 1000000007;
const int INF = 0x3f3f3f3f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const db PI = acos(-1);
const db ERR = 1e-10;

vector <int> conn[100050];
vector <int> son[100050];
int par[100050][20];
int sz[100050];
int dep[100050];
bool dchk[100050];
void DFS1(int n) {
	dchk[n] = true;
	sz[n] = 1;
	for (auto it : conn[n]) {
		if (dchk[it]) continue;
		dep[it] = dep[n] + 1;
		par[it][0] = n;
		for (int i = 1; i < 20; i++) par[it][i] = par[par[it][i - 1]][i - 1];
		DFS1(it);
		son[n].push_back(it);
		sz[n] += sz[it];
	}
}
int upnode(int n, int c) {
	for (int i = 18; i >= 0; i--) if (c & (1 << i)) n = par[n][i];
	return n;
}
int lca(int a, int b) {
	if (dep[a] > dep[b]) swap(a, b);
	b = upnode(b, dep[b] - dep[a]);
	if (a == b) return a;
	for (int i = 19; i >= 0; i--) {
		if (par[a][i] != par[b][i]) {
			a = par[a][i];
			b = par[b][i];
		}
	}
	return par[a][0];
}

vector <int> Vq[100050];
int Qu[100050][3];
map <int, ll> Mx[100050];
ll off[100050];
int pos[100050];
int pc = 0;

vector <ll> Vu;

set <pll> Sx;
void DFS2(int n) {
	if (son[n].empty()) {
		pos[n] = ++pc;
		Mx[pc][n] = Mx[pc][-1] = 0;
		return;
	}
	for (auto it : son[n]) DFS2(it);

	sort(all(son[n]), [](int a, int b) {
		return sz[a] > sz[b];
	});
	
	ll sum = 0;
	for (auto it : son[n]) {
		Vu.push_back(Mx[pos[it]][-1] + off[pos[it]]);
		sum += Vu.back();
	}

	ll toff = sum - (Mx[pos[son[n][0]]][-1] + off[pos[son[n][0]]]);

	ll u = sum;
	for (auto it : Vq[n]) {
		int n1 = Qu[it][0], n2 = Qu[it][1];
		if (n2 == n) swap(n1, n2);

		ll v = sum;
		if (n1 != n) {
			int p1 = upnode(n1, dep[n1] - dep[n] - 1);
			v += Mx[pos[p1]][n1] - Mx[pos[p1]][-1];
		}
		int p2 = upnode(n2, dep[n2] - dep[n] - 1);
		v += Mx[pos[p2]][n2] - Mx[pos[p2]][-1];
		v += Qu[it][2];
		u = max(u, v);
	}
	pos[n] = pos[son[n][0]];
	int np = pos[n];
	off[np] += toff;
	Mx[np][n] = sum - off[np], Mx[np][-1] = u - off[np];
	for (int i = 1; i < son[n].size(); i++) {
		int n2 = son[n][i];
		ll v = sum - Mx[pos[n2]][-1];
		for (auto it : Mx[pos[n2]]) {
			if (it.first == -1) continue;
			Mx[np][it.first] = v + it.second - off[np];
		}
	}
}
int main() {
	srand(time(0));
	int N, Q, i, t1, t2;
	scanf("%d", &N);
	for (i = 1; i < N; i++) {
		scanf("%d %d", &t1, &t2);
		conn[t1].push_back(t2);
		conn[t2].push_back(t1);
	}

	int x = rand() % N + 1;
	for (i = 0; i < 20; i++) par[x][i] = x;
	DFS1(x);

	scanf("%d", &Q);
	for (i = 1; i <= Q; i++) {
		scanf("%d %d %d", &Qu[i][0], &Qu[i][1], &Qu[i][2]);
		Vq[lca(Qu[i][0], Qu[i][1])].push_back(i);
	}
	DFS2(x);
	return !printf("%lld\n", Mx[pos[x]][-1] + off[pos[x]]);
}

Compilation message

election_campaign.cpp:23:0: warning: ignoring #pragma warning  [-Wunknown-pragmas]
 #pragma warning(disable:4996)  
 ^
election_campaign.cpp:24:0: warning: ignoring #pragma comment  [-Wunknown-pragmas]
 #pragma comment(linker, "/STACK:336777216")  
 ^
election_campaign.cpp: In function 'void DFS2(int)':
election_campaign.cpp:138:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 1; i < son[n].size(); i++) {
                    ^
election_campaign.cpp: In function 'int main()':
election_campaign.cpp:150:17: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &N);
                 ^
election_campaign.cpp:152:27: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d", &t1, &t2);
                           ^
election_campaign.cpp:161:17: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &Q);
                 ^
election_campaign.cpp:163:53: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d %d", &Qu[i][0], &Qu[i][1], &Qu[i][2]);
                                                     ^
# Verdict Execution time Memory Grader output
1 Correct 0 ms 24796 KB Output is correct
2 Correct 0 ms 24796 KB Output is correct
3 Correct 3 ms 24796 KB Output is correct
4 Correct 3 ms 25060 KB Output is correct
5 Correct 306 ms 63984 KB Output is correct
6 Correct 119 ms 53752 KB Output is correct
7 Correct 299 ms 49816 KB Output is correct
8 Correct 216 ms 52140 KB Output is correct
9 Correct 209 ms 49812 KB Output is correct
10 Correct 143 ms 52792 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 24796 KB Output is correct
2 Correct 3 ms 24796 KB Output is correct
3 Correct 6 ms 24928 KB Output is correct
4 Correct 359 ms 53628 KB Output is correct
5 Correct 313 ms 53632 KB Output is correct
6 Correct 336 ms 53136 KB Output is correct
7 Correct 433 ms 53012 KB Output is correct
8 Correct 386 ms 53376 KB Output is correct
9 Correct 266 ms 53528 KB Output is correct
10 Correct 379 ms 54388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 24796 KB Output is correct
2 Correct 3 ms 24796 KB Output is correct
3 Correct 6 ms 24928 KB Output is correct
4 Correct 359 ms 53628 KB Output is correct
5 Correct 313 ms 53632 KB Output is correct
6 Correct 336 ms 53136 KB Output is correct
7 Correct 433 ms 53012 KB Output is correct
8 Correct 386 ms 53376 KB Output is correct
9 Correct 266 ms 53528 KB Output is correct
10 Correct 379 ms 54388 KB Output is correct
11 Correct 19 ms 25080 KB Output is correct
12 Correct 286 ms 54384 KB Output is correct
13 Correct 336 ms 54764 KB Output is correct
14 Correct 266 ms 54776 KB Output is correct
15 Correct 353 ms 54760 KB Output is correct
16 Correct 279 ms 53732 KB Output is correct
17 Correct 369 ms 53716 KB Output is correct
18 Correct 416 ms 54248 KB Output is correct
19 Correct 259 ms 54264 KB Output is correct
20 Correct 369 ms 50624 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 549 ms 64772 KB Output is correct
2 Correct 319 ms 51760 KB Output is correct
3 Correct 676 ms 51656 KB Output is correct
4 Correct 269 ms 52764 KB Output is correct
5 Correct 336 ms 52596 KB Output is correct
6 Correct 349 ms 52816 KB Output is correct
7 Correct 573 ms 51932 KB Output is correct
8 Correct 476 ms 66180 KB Output is correct
9 Correct 343 ms 50976 KB Output is correct
10 Correct 626 ms 52372 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 24796 KB Output is correct
2 Correct 0 ms 24796 KB Output is correct
3 Correct 3 ms 24796 KB Output is correct
4 Correct 3 ms 25060 KB Output is correct
5 Correct 306 ms 63984 KB Output is correct
6 Correct 119 ms 53752 KB Output is correct
7 Correct 299 ms 49816 KB Output is correct
8 Correct 216 ms 52140 KB Output is correct
9 Correct 209 ms 49812 KB Output is correct
10 Correct 143 ms 52792 KB Output is correct
11 Correct 3 ms 25060 KB Output is correct
12 Correct 3 ms 24932 KB Output is correct
13 Correct 9 ms 25060 KB Output is correct
14 Correct 6 ms 25060 KB Output is correct
15 Correct 3 ms 25060 KB Output is correct
16 Correct 6 ms 25060 KB Output is correct
17 Correct 3 ms 25060 KB Output is correct
18 Correct 3 ms 24928 KB Output is correct
19 Correct 6 ms 25060 KB Output is correct
20 Correct 0 ms 24932 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 24796 KB Output is correct
2 Correct 0 ms 24796 KB Output is correct
3 Correct 3 ms 24796 KB Output is correct
4 Correct 3 ms 25060 KB Output is correct
5 Correct 306 ms 63984 KB Output is correct
6 Correct 119 ms 53752 KB Output is correct
7 Correct 299 ms 49816 KB Output is correct
8 Correct 216 ms 52140 KB Output is correct
9 Correct 209 ms 49812 KB Output is correct
10 Correct 143 ms 52792 KB Output is correct
11 Correct 6 ms 24796 KB Output is correct
12 Correct 3 ms 24796 KB Output is correct
13 Correct 6 ms 24928 KB Output is correct
14 Correct 359 ms 53628 KB Output is correct
15 Correct 313 ms 53632 KB Output is correct
16 Correct 336 ms 53136 KB Output is correct
17 Correct 433 ms 53012 KB Output is correct
18 Correct 386 ms 53376 KB Output is correct
19 Correct 266 ms 53528 KB Output is correct
20 Correct 379 ms 54388 KB Output is correct
21 Correct 19 ms 25080 KB Output is correct
22 Correct 286 ms 54384 KB Output is correct
23 Correct 336 ms 54764 KB Output is correct
24 Correct 266 ms 54776 KB Output is correct
25 Correct 353 ms 54760 KB Output is correct
26 Correct 279 ms 53732 KB Output is correct
27 Correct 369 ms 53716 KB Output is correct
28 Correct 416 ms 54248 KB Output is correct
29 Correct 259 ms 54264 KB Output is correct
30 Correct 369 ms 50624 KB Output is correct
31 Correct 549 ms 64772 KB Output is correct
32 Correct 319 ms 51760 KB Output is correct
33 Correct 676 ms 51656 KB Output is correct
34 Correct 269 ms 52764 KB Output is correct
35 Correct 336 ms 52596 KB Output is correct
36 Correct 349 ms 52816 KB Output is correct
37 Correct 573 ms 51932 KB Output is correct
38 Correct 476 ms 66180 KB Output is correct
39 Correct 343 ms 50976 KB Output is correct
40 Correct 626 ms 52372 KB Output is correct
41 Correct 3 ms 25060 KB Output is correct
42 Correct 3 ms 24932 KB Output is correct
43 Correct 9 ms 25060 KB Output is correct
44 Correct 6 ms 25060 KB Output is correct
45 Correct 3 ms 25060 KB Output is correct
46 Correct 6 ms 25060 KB Output is correct
47 Correct 3 ms 25060 KB Output is correct
48 Correct 3 ms 24928 KB Output is correct
49 Correct 6 ms 25060 KB Output is correct
50 Correct 0 ms 24932 KB Output is correct
51 Correct 463 ms 64680 KB Output is correct
52 Correct 343 ms 54536 KB Output is correct
53 Correct 606 ms 52356 KB Output is correct
54 Correct 346 ms 52660 KB Output is correct
55 Correct 566 ms 64384 KB Output is correct
56 Correct 399 ms 52000 KB Output is correct
57 Correct 566 ms 51060 KB Output is correct
58 Correct 423 ms 52012 KB Output is correct
59 Correct 439 ms 64820 KB Output is correct
60 Correct 329 ms 54384 KB Output is correct
61 Correct 416 ms 51988 KB Output is correct
62 Correct 433 ms 52724 KB Output is correct
63 Correct 586 ms 64660 KB Output is correct
64 Correct 483 ms 54220 KB Output is correct
65 Correct 599 ms 51068 KB Output is correct
66 Correct 363 ms 53856 KB Output is correct
67 Correct 576 ms 66452 KB Output is correct
68 Correct 253 ms 53404 KB Output is correct
69 Correct 483 ms 52368 KB Output is correct
70 Correct 433 ms 52616 KB Output is correct