답안 #484125

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
484125 2021-11-02T06:58:39 Z S2speed Election Campaign (JOI15_election_campaign) C++17
100 / 100
243 ms 65764 KB
#include<bits/stdc++.h>

using namespace std;

#pragma GCC optimize ("Ofast")

#define all(x) x.begin() , x.end()
#define sze(x) (ll)(x.size())
#define mp(x , y) make_pair(x , y)
#define wall cerr<<"--------------------------------------"<<endl
typedef long long int ll;
typedef pair<ll , ll> pll;
typedef pair<int , int> pii;
typedef long double db;
typedef pair<pll , ll> plll;
typedef pair<int , pii> piii;
typedef pair<pll , pll> pllll;

const ll maxn = 2e5 + 16 , md = 1e9 + 7 , inf = 2e9;

ll gcd(ll a , ll b){
	if(a < b) swap(a , b);
	if(b == 0) return a;
	return gcd(b , a % b);
}

ll tav(ll n , ll k){
	ll res = 1;
	while(k > 0){
		if(k & 1){
			res *= n; res %= md;
		}
		n *= n; n %= md;
		k >>= 1;
	}
	return res;
}

struct fentree {

	ll sz;
	vector<ll> val;

	void init(ll n){
		sz = n;
		val.assign(sz , 0);
		return;
	}

	void add(ll i , ll k){
		ll h = i;
		while(h < sz){
			val[h] += k;
			h |= (h + 1);
		}
		return;
	}

	ll cal(ll i){
		ll h = i , res = 0;
		while(h > -1){
			res += val[h];
			h &= (h + 1); h--;
		}
		return res;
	}

};

fentree ff , fd;
ll f[maxn] , dp[maxn];
vector<ll> adj[maxn] , a[maxn];
ll v[maxn] , u[maxn] , c[maxn];
ll jad[maxn][20] , pr[maxn] , dis[maxn] , dep = 0 , bt[maxn] , ft[maxn] , tme = 0;

void pDFS(ll r , ll par){
	jad[r][0] = pr[r] = par;
	for(ll j = 1 ; j < 20 ; j++){
		if(jad[r][j - 1] == -1) break;
		jad[r][j] = jad[jad[r][j - 1]][j - 1];
	}
	dis[r] = dep++;
	bt[r] = tme++;
	for(auto i : adj[r]){
		if(i == par) continue;
		pDFS(i , r);
	}
	ft[r] = tme;
	dis[r] = dep--;
	return;
}

ll find_jad(ll v , ll d){
	d = dis[v] - d;
	for(ll j = 0 ; j < 20 ; j++){
		if(d & (1 << j)){
			v = jad[v][j];
		}
	}
	return v;
}

ll lca(ll v , ll u){
	if(dis[v] > dis[u]) swap(v , u);
	u = find_jad(u , dis[v]);
	if(v == u) return v;
	for(ll j = 19 ; j >= 0 ; j--){
		if(jad[v][j] != jad[u][j]){
			v = jad[v][j]; u = jad[u][j];
		}
	}
	return pr[v];
}

void dDFS(ll r , ll par){
	for(auto i : adj[r]){
		if(i == par) continue;
		dDFS(i , r);
		dp[r] += dp[i];
	}
	f[r] = dp[r];
	for(auto j : a[r]){
		ll h = ff.cal(bt[v[j]]) + ff.cal(bt[u[j]]) + f[r] - fd.cal(bt[v[j]]) - fd.cal(bt[u[j]]) + c[j];
		dp[r] = max(dp[r] , h);
	}
	ff.add(bt[r] , f[r]); ff.add(ft[r] , -f[r]);
	fd.add(bt[r] , dp[r]); fd.add(ft[r] , -dp[r]);
	return;
}

int main(){
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

	memset(dp , 0 , sizeof(dp));
	memset(jad , -1 , sizeof(jad));
	ll n , m;
	cin>>n;
	for(ll i = 1 ; i < n ; i++){
		ll v , u;
		cin>>v>>u; v--; u--;
		adj[v].push_back(u); adj[u].push_back(v);
	}
	pDFS(0 , -1);
	cin>>m;
	for(ll i = 0 ; i < m ; i++){
		cin>>v[i]>>u[i]>>c[i]; v[i]--; u[i]--;
		ll l = lca(v[i] , u[i]);
		a[l].push_back(i);
	}
	ff.init(n); fd.init(n);
	dDFS(0 , -1);
//	for(ll i = 0 ; i < n ; i++){
//		cout<<dp[i]<<' ';
//	}
//	cout<<'\n';
	cout<<dp[0]<<'\n';
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 42572 KB Output is correct
2 Correct 17 ms 42536 KB Output is correct
3 Correct 17 ms 42648 KB Output is correct
4 Correct 17 ms 42700 KB Output is correct
5 Correct 91 ms 53124 KB Output is correct
6 Correct 62 ms 60100 KB Output is correct
7 Correct 108 ms 57816 KB Output is correct
8 Correct 70 ms 53300 KB Output is correct
9 Correct 98 ms 56332 KB Output is correct
10 Correct 64 ms 53188 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 42576 KB Output is correct
2 Correct 17 ms 42636 KB Output is correct
3 Correct 17 ms 42700 KB Output is correct
4 Correct 127 ms 65416 KB Output is correct
5 Correct 132 ms 65476 KB Output is correct
6 Correct 112 ms 65468 KB Output is correct
7 Correct 119 ms 65348 KB Output is correct
8 Correct 127 ms 65348 KB Output is correct
9 Correct 113 ms 65392 KB Output is correct
10 Correct 134 ms 65360 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 42576 KB Output is correct
2 Correct 17 ms 42636 KB Output is correct
3 Correct 17 ms 42700 KB Output is correct
4 Correct 127 ms 65416 KB Output is correct
5 Correct 132 ms 65476 KB Output is correct
6 Correct 112 ms 65468 KB Output is correct
7 Correct 119 ms 65348 KB Output is correct
8 Correct 127 ms 65348 KB Output is correct
9 Correct 113 ms 65392 KB Output is correct
10 Correct 134 ms 65360 KB Output is correct
11 Correct 25 ms 43844 KB Output is correct
12 Correct 129 ms 65644 KB Output is correct
13 Correct 133 ms 65628 KB Output is correct
14 Correct 117 ms 65732 KB Output is correct
15 Correct 140 ms 65732 KB Output is correct
16 Correct 112 ms 65656 KB Output is correct
17 Correct 140 ms 65656 KB Output is correct
18 Correct 124 ms 65652 KB Output is correct
19 Correct 112 ms 65712 KB Output is correct
20 Correct 136 ms 65604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 148 ms 57696 KB Output is correct
2 Correct 111 ms 65396 KB Output is correct
3 Correct 231 ms 62660 KB Output is correct
4 Correct 116 ms 58012 KB Output is correct
5 Correct 187 ms 62296 KB Output is correct
6 Correct 110 ms 57860 KB Output is correct
7 Correct 223 ms 61980 KB Output is correct
8 Correct 161 ms 57996 KB Output is correct
9 Correct 113 ms 65448 KB Output is correct
10 Correct 219 ms 60964 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 42572 KB Output is correct
2 Correct 17 ms 42536 KB Output is correct
3 Correct 17 ms 42648 KB Output is correct
4 Correct 17 ms 42700 KB Output is correct
5 Correct 91 ms 53124 KB Output is correct
6 Correct 62 ms 60100 KB Output is correct
7 Correct 108 ms 57816 KB Output is correct
8 Correct 70 ms 53300 KB Output is correct
9 Correct 98 ms 56332 KB Output is correct
10 Correct 64 ms 53188 KB Output is correct
11 Correct 18 ms 42704 KB Output is correct
12 Correct 19 ms 42840 KB Output is correct
13 Correct 19 ms 42720 KB Output is correct
14 Correct 20 ms 42740 KB Output is correct
15 Correct 21 ms 42724 KB Output is correct
16 Correct 18 ms 42700 KB Output is correct
17 Correct 17 ms 42708 KB Output is correct
18 Correct 18 ms 42828 KB Output is correct
19 Correct 17 ms 42724 KB Output is correct
20 Correct 18 ms 42752 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 42572 KB Output is correct
2 Correct 17 ms 42536 KB Output is correct
3 Correct 17 ms 42648 KB Output is correct
4 Correct 17 ms 42700 KB Output is correct
5 Correct 91 ms 53124 KB Output is correct
6 Correct 62 ms 60100 KB Output is correct
7 Correct 108 ms 57816 KB Output is correct
8 Correct 70 ms 53300 KB Output is correct
9 Correct 98 ms 56332 KB Output is correct
10 Correct 64 ms 53188 KB Output is correct
11 Correct 19 ms 42576 KB Output is correct
12 Correct 17 ms 42636 KB Output is correct
13 Correct 17 ms 42700 KB Output is correct
14 Correct 127 ms 65416 KB Output is correct
15 Correct 132 ms 65476 KB Output is correct
16 Correct 112 ms 65468 KB Output is correct
17 Correct 119 ms 65348 KB Output is correct
18 Correct 127 ms 65348 KB Output is correct
19 Correct 113 ms 65392 KB Output is correct
20 Correct 134 ms 65360 KB Output is correct
21 Correct 25 ms 43844 KB Output is correct
22 Correct 129 ms 65644 KB Output is correct
23 Correct 133 ms 65628 KB Output is correct
24 Correct 117 ms 65732 KB Output is correct
25 Correct 140 ms 65732 KB Output is correct
26 Correct 112 ms 65656 KB Output is correct
27 Correct 140 ms 65656 KB Output is correct
28 Correct 124 ms 65652 KB Output is correct
29 Correct 112 ms 65712 KB Output is correct
30 Correct 136 ms 65604 KB Output is correct
31 Correct 148 ms 57696 KB Output is correct
32 Correct 111 ms 65396 KB Output is correct
33 Correct 231 ms 62660 KB Output is correct
34 Correct 116 ms 58012 KB Output is correct
35 Correct 187 ms 62296 KB Output is correct
36 Correct 110 ms 57860 KB Output is correct
37 Correct 223 ms 61980 KB Output is correct
38 Correct 161 ms 57996 KB Output is correct
39 Correct 113 ms 65448 KB Output is correct
40 Correct 219 ms 60964 KB Output is correct
41 Correct 18 ms 42704 KB Output is correct
42 Correct 19 ms 42840 KB Output is correct
43 Correct 19 ms 42720 KB Output is correct
44 Correct 20 ms 42740 KB Output is correct
45 Correct 21 ms 42724 KB Output is correct
46 Correct 18 ms 42700 KB Output is correct
47 Correct 17 ms 42708 KB Output is correct
48 Correct 18 ms 42828 KB Output is correct
49 Correct 17 ms 42724 KB Output is correct
50 Correct 18 ms 42752 KB Output is correct
51 Correct 160 ms 58380 KB Output is correct
52 Correct 150 ms 65704 KB Output is correct
53 Correct 231 ms 61400 KB Output is correct
54 Correct 107 ms 58100 KB Output is correct
55 Correct 145 ms 58040 KB Output is correct
56 Correct 125 ms 65604 KB Output is correct
57 Correct 188 ms 62276 KB Output is correct
58 Correct 114 ms 58172 KB Output is correct
59 Correct 155 ms 58348 KB Output is correct
60 Correct 132 ms 65716 KB Output is correct
61 Correct 190 ms 62248 KB Output is correct
62 Correct 115 ms 58092 KB Output is correct
63 Correct 149 ms 57872 KB Output is correct
64 Correct 128 ms 65764 KB Output is correct
65 Correct 243 ms 62048 KB Output is correct
66 Correct 113 ms 58304 KB Output is correct
67 Correct 148 ms 57912 KB Output is correct
68 Correct 126 ms 65684 KB Output is correct
69 Correct 187 ms 60992 KB Output is correct
70 Correct 119 ms 58320 KB Output is correct