답안 #924082

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
924082 2024-02-08T11:46:22 Z KiaRez Election Campaign (JOI15_election_campaign) C++17
100 / 100
302 ms 48844 KB
/*
    IN THE NAME OF GOD
*/
#include <bits/stdc++.h>

// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")

using namespace std;

typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef long double ld;

#define F                                      first
#define S                                      second
#define Mp                                     make_pair
#define pb                                     push_back
#define pf                                     push_front
#define size(x)                                ((ll)x.size())
#define all(x)                                 (x).begin(),(x).end()
#define kill(x)		                           cout << x << '\n', exit(0);
#define fuck(x)                                cout << "(" << #x << " , " << x << ")" << endl
#define endl                                   '\n'

const int N = 2e5+23, lg = 18;
ll Mod = 1e9+7; //998244353;

inline ll MOD(ll a, ll mod=Mod) {a%=mod; (a<0)&&(a+=mod); return a;}
inline ll poww(ll a, ll b, ll mod=Mod) {
    ll ans = 1;
    a=MOD(a, mod);
    while (b) {
        if (b & 1) ans = MOD(ans*a, mod);
        b >>= 1;
        a = MOD(a*a, mod);
    }
    return ans;
}

ll n, m, fen[N], dp[N], sum[N];
int tim, tin[N], tout[N], h[N], par[lg][N];
vector<int> adj[N];
vector<vector<ll>> vec[N];

void upd(int pos, ll val) {while(pos<N) {fen[pos]+=val; pos+=(pos&(-pos));}}
ll qry(int pos) {
	ll res = 0; while(pos > 0) {res += fen[pos]; pos-=(pos&(-pos));} return res;
}
void add(int l, int r, ll val) {upd(l, val); upd(r, -val);}

int getPar(int v, int dist) {
	for(int i=0; i<lg; i++) {
		if((dist>>i)%2==1) v=par[i][v];
	} return v;
}

int LCA(int v, int u) {
	if(h[u]<h[v]) swap(v,u);
	u = getPar(u, h[u]-h[v]);
	if(v==u) return v;
	for(int i=lg-1; i>=0; i--) {
		if(par[i][v] != par[i][u]) v=par[i][v], u=par[i][u];
	}
	return par[0][v];
}

void init(int v, int p=0) {
	par[0][v] = p, h[v] = h[p] + 1, tin[v] = ++tim;
	for(int i=1; i<lg; i++) {
		par[i][v] = par[i-1][par[i-1][v]];
	}
	for(int u : adj[v]) {
		if(u == p) continue;
		init(u, v);
	}
	tout[v] = tim+1;
}

void dfs(int v, int p=0) {
	for(int u : adj[v]) {
		if(u == p) continue;
		dfs(u, v);
		dp[v] += dp[u];
		sum[v] += dp[u];
	}
	for(auto it : vec[v]) {
		int a,b;
		if(it[0]!=v) a=getPar(it[0],h[it[0]]-h[v]-1);
		else a = 0;
		if(it[1]!=v) b=getPar(it[1],h[it[1]]-h[v]-1);
		else b = 0;
		dp[v] = max(dp[v], qry(tin[it[0]])+qry(tin[it[1]])+it[2]+sum[v]-dp[a]-dp[b]);
	}
	add(tin[v], tin[v]+1, sum[v]);
	for(int u : adj[v]) {
		if(u == p) continue;
		add(tin[u], tout[u], sum[v]-dp[u]);
	}
}

int main () {
	// ios_base::sync_with_stdio(false), cin.tie(0);

	cin>>n;
	for(int v,u,i=1; i<n; i++) {
		cin>>v>>u;
		adj[v].pb(u); adj[u].pb(v);
	}

	init(1);
	cin>>m;
	for(int a,b,c,i=1; i<=m; i++) {
		cin>>a>>b>>c;
		vec[LCA(a, b)].pb({a, b, c});
	}

	dfs(1);
	cout<<dp[1]<<endl;

	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 23132 KB Output is correct
2 Correct 4 ms 23132 KB Output is correct
3 Correct 4 ms 23132 KB Output is correct
4 Correct 5 ms 23132 KB Output is correct
5 Correct 89 ms 31312 KB Output is correct
6 Correct 67 ms 40532 KB Output is correct
7 Correct 86 ms 37508 KB Output is correct
8 Correct 72 ms 30176 KB Output is correct
9 Correct 82 ms 35408 KB Output is correct
10 Correct 71 ms 30200 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 23132 KB Output is correct
2 Correct 4 ms 23132 KB Output is correct
3 Correct 5 ms 23388 KB Output is correct
4 Correct 166 ms 48464 KB Output is correct
5 Correct 164 ms 48528 KB Output is correct
6 Correct 156 ms 48372 KB Output is correct
7 Correct 166 ms 48408 KB Output is correct
8 Correct 164 ms 48464 KB Output is correct
9 Correct 162 ms 48468 KB Output is correct
10 Correct 175 ms 48368 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 23132 KB Output is correct
2 Correct 4 ms 23132 KB Output is correct
3 Correct 5 ms 23388 KB Output is correct
4 Correct 166 ms 48464 KB Output is correct
5 Correct 164 ms 48528 KB Output is correct
6 Correct 156 ms 48372 KB Output is correct
7 Correct 166 ms 48408 KB Output is correct
8 Correct 164 ms 48464 KB Output is correct
9 Correct 162 ms 48468 KB Output is correct
10 Correct 175 ms 48368 KB Output is correct
11 Correct 21 ms 25180 KB Output is correct
12 Correct 182 ms 48592 KB Output is correct
13 Correct 172 ms 48624 KB Output is correct
14 Correct 164 ms 48720 KB Output is correct
15 Correct 169 ms 48820 KB Output is correct
16 Correct 164 ms 48720 KB Output is correct
17 Correct 172 ms 48724 KB Output is correct
18 Correct 173 ms 48732 KB Output is correct
19 Correct 172 ms 48684 KB Output is correct
20 Correct 168 ms 48744 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 182 ms 38412 KB Output is correct
2 Correct 152 ms 48348 KB Output is correct
3 Correct 232 ms 44680 KB Output is correct
4 Correct 154 ms 37124 KB Output is correct
5 Correct 203 ms 44172 KB Output is correct
6 Correct 164 ms 37416 KB Output is correct
7 Correct 220 ms 43788 KB Output is correct
8 Correct 190 ms 39008 KB Output is correct
9 Correct 154 ms 48408 KB Output is correct
10 Correct 227 ms 42704 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 23132 KB Output is correct
2 Correct 4 ms 23132 KB Output is correct
3 Correct 4 ms 23132 KB Output is correct
4 Correct 5 ms 23132 KB Output is correct
5 Correct 89 ms 31312 KB Output is correct
6 Correct 67 ms 40532 KB Output is correct
7 Correct 86 ms 37508 KB Output is correct
8 Correct 72 ms 30176 KB Output is correct
9 Correct 82 ms 35408 KB Output is correct
10 Correct 71 ms 30200 KB Output is correct
11 Correct 6 ms 23388 KB Output is correct
12 Correct 5 ms 23388 KB Output is correct
13 Correct 5 ms 23388 KB Output is correct
14 Correct 6 ms 23388 KB Output is correct
15 Correct 5 ms 23388 KB Output is correct
16 Correct 5 ms 23388 KB Output is correct
17 Correct 6 ms 23388 KB Output is correct
18 Correct 6 ms 23436 KB Output is correct
19 Correct 5 ms 23220 KB Output is correct
20 Correct 5 ms 23388 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 23132 KB Output is correct
2 Correct 4 ms 23132 KB Output is correct
3 Correct 4 ms 23132 KB Output is correct
4 Correct 5 ms 23132 KB Output is correct
5 Correct 89 ms 31312 KB Output is correct
6 Correct 67 ms 40532 KB Output is correct
7 Correct 86 ms 37508 KB Output is correct
8 Correct 72 ms 30176 KB Output is correct
9 Correct 82 ms 35408 KB Output is correct
10 Correct 71 ms 30200 KB Output is correct
11 Correct 4 ms 23132 KB Output is correct
12 Correct 4 ms 23132 KB Output is correct
13 Correct 5 ms 23388 KB Output is correct
14 Correct 166 ms 48464 KB Output is correct
15 Correct 164 ms 48528 KB Output is correct
16 Correct 156 ms 48372 KB Output is correct
17 Correct 166 ms 48408 KB Output is correct
18 Correct 164 ms 48464 KB Output is correct
19 Correct 162 ms 48468 KB Output is correct
20 Correct 175 ms 48368 KB Output is correct
21 Correct 21 ms 25180 KB Output is correct
22 Correct 182 ms 48592 KB Output is correct
23 Correct 172 ms 48624 KB Output is correct
24 Correct 164 ms 48720 KB Output is correct
25 Correct 169 ms 48820 KB Output is correct
26 Correct 164 ms 48720 KB Output is correct
27 Correct 172 ms 48724 KB Output is correct
28 Correct 173 ms 48732 KB Output is correct
29 Correct 172 ms 48684 KB Output is correct
30 Correct 168 ms 48744 KB Output is correct
31 Correct 182 ms 38412 KB Output is correct
32 Correct 152 ms 48348 KB Output is correct
33 Correct 232 ms 44680 KB Output is correct
34 Correct 154 ms 37124 KB Output is correct
35 Correct 203 ms 44172 KB Output is correct
36 Correct 164 ms 37416 KB Output is correct
37 Correct 220 ms 43788 KB Output is correct
38 Correct 190 ms 39008 KB Output is correct
39 Correct 154 ms 48408 KB Output is correct
40 Correct 227 ms 42704 KB Output is correct
41 Correct 6 ms 23388 KB Output is correct
42 Correct 5 ms 23388 KB Output is correct
43 Correct 5 ms 23388 KB Output is correct
44 Correct 6 ms 23388 KB Output is correct
45 Correct 5 ms 23388 KB Output is correct
46 Correct 5 ms 23388 KB Output is correct
47 Correct 6 ms 23388 KB Output is correct
48 Correct 6 ms 23436 KB Output is correct
49 Correct 5 ms 23220 KB Output is correct
50 Correct 5 ms 23388 KB Output is correct
51 Correct 199 ms 39192 KB Output is correct
52 Correct 194 ms 48844 KB Output is correct
53 Correct 302 ms 43012 KB Output is correct
54 Correct 151 ms 37448 KB Output is correct
55 Correct 200 ms 38912 KB Output is correct
56 Correct 169 ms 48660 KB Output is correct
57 Correct 228 ms 43844 KB Output is correct
58 Correct 176 ms 37532 KB Output is correct
59 Correct 241 ms 39104 KB Output is correct
60 Correct 172 ms 48724 KB Output is correct
61 Correct 209 ms 43848 KB Output is correct
62 Correct 161 ms 37284 KB Output is correct
63 Correct 194 ms 38532 KB Output is correct
64 Correct 178 ms 48788 KB Output is correct
65 Correct 240 ms 43856 KB Output is correct
66 Correct 149 ms 37268 KB Output is correct
67 Correct 180 ms 38592 KB Output is correct
68 Correct 170 ms 48720 KB Output is correct
69 Correct 204 ms 42340 KB Output is correct
70 Correct 175 ms 37264 KB Output is correct