답안 #670495

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
670495 2022-12-09T09:41:18 Z ghostwriter Election Campaign (JOI15_election_campaign) C++17
100 / 100
154 ms 29672 KB
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include <debug.h>
#else
#define debug(...)
#endif
#define ft front
#define bk back
#define st first
#define nd second
#define ins insert
#define ers erase
#define pb push_back
#define pf push_front
#define _pb pop_back
#define _pf pop_front
#define lb lower_bound
#define ub upper_bound
#define mtp make_tuple
#define bg begin
#define ed end
#define all(x) (x).bg(), (x).ed()
#define sz(x) (int)(x).size()
typedef long long ll; typedef unsigned long long ull;
typedef double db; typedef long double ldb;
typedef pair<int, int> pi; typedef pair<ll, ll> pll;
typedef vector<int> vi; typedef vector<ll> vll; typedef vector<pi> vpi; typedef vector<pll> vpll;
typedef string str;
#define FOR(i, l, r) for (int i = (l); i <= (r); ++i)
#define FOS(i, r, l) for (int i = (r); i >= (l); --i)
#define FRN(i, n) for (int i = 0; i < (n); ++i)
#define FSN(i, n) for (int i = (n) - 1; i >= 0; --i)
#define EACH(i, x) for (auto &i : (x))
#define WHILE while
template<typename T> T gcd(T a, T b) { WHILE(b) { a = a % b; swap(a, b); } return a; }
template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
#define file "TEST"
mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());
ll rand(ll l, ll r) { return uniform_int_distribution<ll>(l, r)(rd); }
/*
----------------------------------------------------------------
    END OF TEMPLATE
----------------------------------------------------------------
    Tran The Bao - ghostwriter
    Training for VOI23 gold medal
----------------------------------------------------------------
    GOAT
----------------------------------------------------------------
*/
struct Plan {
	int a, b, c;
	Plan() {}
	Plan(int a, int b, int c) : a(a), b(b), c(c) {}
};
const int N = 1e5 + 5;
int n, m, h[N], tin[N], s[N], p[N][17], d[N], f[N], timer = 0;
vi adj[N], d1[N];
Plan P[N];
void input() {
	cin >> n;
	FOR(i, 1, n - 1) {
		int u, v;
		cin >> u >> v;
		adj[u].pb(v);
		adj[v].pb(u);
	}
	cin >> m;
	FOR(i, 1, m) cin >> P[i].a >> P[i].b >> P[i].c;
}
void dfs(int u, int p1) {
	tin[u] = ++timer;
	s[u] = 1;
	p[u][0] = p1;
	if (p1) FOR(i, 1, 16) p[u][i] = p[p[u][i - 1]][i - 1];
	EACH(v, adj[u]) {
		if (v == p1) continue;
		h[v] = h[u] + 1;
		dfs(v, u);
		s[u] += s[v];
	}
}
int lca(int a, int b) {
	if (h[a] > h[b]) swap(a, b);
	int diff = h[b] - h[a];
	FRN(i, 17)
		if (diff & (1 << i))
			b = p[b][i];
	if (a == b) return a;
	FSN(i, 17)
		if (p[a][i] != p[b][i]) {
			a = p[a][i];
			b = p[b][i];
		}
	return p[a][0];
}
int getn(int a, int h) { FSN(i, 17) if (h & (1 << i)) a = p[a][i]; return a; }
void upd(int pos, int v) { for (int i = pos; i <= n; i += (i & -i)) f[i] += v; }
void upd(int l, int r, int v) { upd(l, v); upd(r + 1, -v); }
int get(int pos) { int rs = 0; for (int i = pos; i > 0; i -= (i & -i)) rs += f[i]; return rs; }
void dfs1(int u, int p1) {
	int sum = 0;
	EACH(v, adj[u]) {
		if (v == p1) continue;
		dfs1(v, u);
		sum += d[v];
	}
	d[u] = sum;
	EACH(i, d1[u]) {
		int a = P[i].a, b = P[i].b, c = P[i].c;
		d[u] = max(d[u], get(tin[a]) + get(tin[b]) + sum + c);
	}
	upd(tin[u], tin[u] + s[u] - 1, sum - d[u]);
}
void solve() {
	dfs(1, 0);
	FOR(i, 1, m) d1[lca(P[i].a, P[i].b)].pb(i);
	dfs1(1, 0);
	cout << d[1];
}
signed main() {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    // freopen(file".inp", "r", stdin);
    // freopen(file".out", "w", stdout);
    input();
    solve();
    return 0;
}
/*
----------------------------------------------------------------
From Benq:
    stuff you should look for
        * int overflow, array bounds
        * special cases (n=1?)
        * do smth instead of nothing and stay organized
        * WRITE STUFF DOWN
        * DON'T GET STUCK ON ONE APPROACH
----------------------------------------------------------------
*/
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 5076 KB Output is correct
5 Correct 76 ms 18072 KB Output is correct
6 Correct 47 ms 25620 KB Output is correct
7 Correct 78 ms 23024 KB Output is correct
8 Correct 58 ms 18336 KB Output is correct
9 Correct 80 ms 21440 KB Output is correct
10 Correct 64 ms 18408 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 5044 KB Output is correct
3 Correct 3 ms 5204 KB Output is correct
4 Correct 82 ms 29160 KB Output is correct
5 Correct 82 ms 29284 KB Output is correct
6 Correct 81 ms 29260 KB Output is correct
7 Correct 84 ms 29332 KB Output is correct
8 Correct 85 ms 29180 KB Output is correct
9 Correct 80 ms 29284 KB Output is correct
10 Correct 87 ms 29168 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 5044 KB Output is correct
3 Correct 3 ms 5204 KB Output is correct
4 Correct 82 ms 29160 KB Output is correct
5 Correct 82 ms 29284 KB Output is correct
6 Correct 81 ms 29260 KB Output is correct
7 Correct 84 ms 29332 KB Output is correct
8 Correct 85 ms 29180 KB Output is correct
9 Correct 80 ms 29284 KB Output is correct
10 Correct 87 ms 29168 KB Output is correct
11 Correct 10 ms 5972 KB Output is correct
12 Correct 92 ms 29556 KB Output is correct
13 Correct 89 ms 29544 KB Output is correct
14 Correct 81 ms 29584 KB Output is correct
15 Correct 87 ms 29544 KB Output is correct
16 Correct 81 ms 29516 KB Output is correct
17 Correct 88 ms 29544 KB Output is correct
18 Correct 87 ms 29544 KB Output is correct
19 Correct 79 ms 29576 KB Output is correct
20 Correct 87 ms 29672 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 117 ms 21056 KB Output is correct
2 Correct 84 ms 29328 KB Output is correct
3 Correct 154 ms 26176 KB Output is correct
4 Correct 90 ms 21412 KB Output is correct
5 Correct 128 ms 25856 KB Output is correct
6 Correct 92 ms 21604 KB Output is correct
7 Correct 137 ms 25520 KB Output is correct
8 Correct 122 ms 21452 KB Output is correct
9 Correct 83 ms 29404 KB Output is correct
10 Correct 142 ms 24520 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 5076 KB Output is correct
5 Correct 76 ms 18072 KB Output is correct
6 Correct 47 ms 25620 KB Output is correct
7 Correct 78 ms 23024 KB Output is correct
8 Correct 58 ms 18336 KB Output is correct
9 Correct 80 ms 21440 KB Output is correct
10 Correct 64 ms 18408 KB Output is correct
11 Correct 3 ms 5204 KB Output is correct
12 Correct 3 ms 5204 KB Output is correct
13 Correct 3 ms 5180 KB Output is correct
14 Correct 3 ms 5204 KB Output is correct
15 Correct 3 ms 5176 KB Output is correct
16 Correct 3 ms 5180 KB Output is correct
17 Correct 3 ms 5204 KB Output is correct
18 Correct 4 ms 5204 KB Output is correct
19 Correct 4 ms 5204 KB Output is correct
20 Correct 4 ms 5204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 5076 KB Output is correct
5 Correct 76 ms 18072 KB Output is correct
6 Correct 47 ms 25620 KB Output is correct
7 Correct 78 ms 23024 KB Output is correct
8 Correct 58 ms 18336 KB Output is correct
9 Correct 80 ms 21440 KB Output is correct
10 Correct 64 ms 18408 KB Output is correct
11 Correct 3 ms 4948 KB Output is correct
12 Correct 3 ms 5044 KB Output is correct
13 Correct 3 ms 5204 KB Output is correct
14 Correct 82 ms 29160 KB Output is correct
15 Correct 82 ms 29284 KB Output is correct
16 Correct 81 ms 29260 KB Output is correct
17 Correct 84 ms 29332 KB Output is correct
18 Correct 85 ms 29180 KB Output is correct
19 Correct 80 ms 29284 KB Output is correct
20 Correct 87 ms 29168 KB Output is correct
21 Correct 10 ms 5972 KB Output is correct
22 Correct 92 ms 29556 KB Output is correct
23 Correct 89 ms 29544 KB Output is correct
24 Correct 81 ms 29584 KB Output is correct
25 Correct 87 ms 29544 KB Output is correct
26 Correct 81 ms 29516 KB Output is correct
27 Correct 88 ms 29544 KB Output is correct
28 Correct 87 ms 29544 KB Output is correct
29 Correct 79 ms 29576 KB Output is correct
30 Correct 87 ms 29672 KB Output is correct
31 Correct 117 ms 21056 KB Output is correct
32 Correct 84 ms 29328 KB Output is correct
33 Correct 154 ms 26176 KB Output is correct
34 Correct 90 ms 21412 KB Output is correct
35 Correct 128 ms 25856 KB Output is correct
36 Correct 92 ms 21604 KB Output is correct
37 Correct 137 ms 25520 KB Output is correct
38 Correct 122 ms 21452 KB Output is correct
39 Correct 83 ms 29404 KB Output is correct
40 Correct 142 ms 24520 KB Output is correct
41 Correct 3 ms 5204 KB Output is correct
42 Correct 3 ms 5204 KB Output is correct
43 Correct 3 ms 5180 KB Output is correct
44 Correct 3 ms 5204 KB Output is correct
45 Correct 3 ms 5176 KB Output is correct
46 Correct 3 ms 5180 KB Output is correct
47 Correct 3 ms 5204 KB Output is correct
48 Correct 4 ms 5204 KB Output is correct
49 Correct 4 ms 5204 KB Output is correct
50 Correct 4 ms 5204 KB Output is correct
51 Correct 145 ms 21828 KB Output is correct
52 Correct 92 ms 29472 KB Output is correct
53 Correct 139 ms 24964 KB Output is correct
54 Correct 89 ms 21708 KB Output is correct
55 Correct 115 ms 21304 KB Output is correct
56 Correct 87 ms 29528 KB Output is correct
57 Correct 135 ms 25648 KB Output is correct
58 Correct 92 ms 21664 KB Output is correct
59 Correct 134 ms 21748 KB Output is correct
60 Correct 89 ms 29548 KB Output is correct
61 Correct 134 ms 25756 KB Output is correct
62 Correct 91 ms 21644 KB Output is correct
63 Correct 119 ms 21336 KB Output is correct
64 Correct 88 ms 29516 KB Output is correct
65 Correct 145 ms 25612 KB Output is correct
66 Correct 89 ms 21708 KB Output is correct
67 Correct 120 ms 21436 KB Output is correct
68 Correct 88 ms 29520 KB Output is correct
69 Correct 139 ms 24456 KB Output is correct
70 Correct 92 ms 21728 KB Output is correct