답안 #963250

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
963250 2024-04-14T19:20:27 Z BhavayGoyal Election Campaign (JOI15_election_campaign) C++14
100 / 100
169 ms 38736 KB
#include <bits/stdc++.h>
using namespace std;

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

template<class T> using oset = 
            tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define ll long long
#define ld long double
#define ar array
#define vi vector<int>
#define vii vector<vector<int>>
#define pii pair<int, int>
#define pb push_back
#define all(x) x.begin(), x.end()
#define f first
#define s second
#define endl "\n"

const int MOD = 1e9+7;
const int inf = 1e9;
const ll linf = 1e18;

const int d4i[4]={-1, 0, 1, 0}, d4j[4]={0, 1, 0, -1};
const int d8i[8]={-1, -1, 0, 1, 1, 1, 0, -1}, d8j[8]={0, 1, 1, 1, 0, -1, -1, -1};

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());


// -------------------------------------------------- Main Code --------------------------------------------------

struct fenTree {
    int n;
    vi tree;

    fenTree(int N) {n = N+5; tree = vi(n+5);}

    void update(int idx, int val) {idx++; for (idx; idx <= n; idx += (idx&-idx)) tree[idx] += val;}
    int sum(int idx) {idx++; int ans = 0; for (idx; idx; idx -= (idx&-idx)) ans += tree[idx]; return ans;}
};

const int N = 2e5+5;
vi g[N];
int parent[N][20], tin[N], tout[N], Time = 0, depth[N], dp[N][2];
vector<array<int, 3>> arr[N];
fenTree Tree(N);

void dfs2(int src, int par) {
    tin[src] = ++Time;
    parent[src][0] = par;
    for (int j = 1; j < 20; j++) parent[src][j] = parent[parent[src][j-1]][j-1];
    for (auto ch : g[src]) {
        if (ch == par) continue;
        depth[ch] = depth[src]+1;
        dfs2(ch, src);
    }
    tout[src] = ++Time;
}


int lca(int a, int b) {
    if (depth[a] > depth[b]) swap(a, b);
    int diff = depth[b]-depth[a];
    for (int j = 0; j < 20; j++) {
        if (diff&(1<<j)) b = parent[b][j];
    }

    if (a == b) return a;

    for (int j = 19; j >= 0; j--) {
        if (parent[a][j] != parent[b][j]) {
            a = parent[a][j];
            b = parent[b][j];
        }
    }

    return parent[a][0];
}

void dfs(int src, int par) {
    for (auto ch : g[src]) {
        if (ch == par) continue;
        dfs(ch, src);
        dp[src][0] += dp[ch][1];
    }

    dp[src][1] = dp[src][0];
    for (auto x : arr[src]) {
        int a = x[0], b = x[1], votes = x[2];
        dp[src][1] = max(dp[src][1], dp[src][0] + Tree.sum(tin[a]) + Tree.sum(tin[b]) + votes);
    }

    Tree.update(tin[src], dp[src][0]-dp[src][1]);
    Tree.update(tout[src], dp[src][1]-dp[src][0]);
}

void sol() {
    int n; cin >> n;
    for (int i = 1; i <= n-1; i++) {
        int a, b; cin >> a >> b;
        g[a].pb(b);
        g[b].pb(a);
    }

    dfs2(1, 0);

    int m; cin >> m;
    for (int i = 1; i <= m; i++) {
        int a, b, c; cin >> a >> b >> c;
        int l = lca(a, b);
        arr[l].pb({a, b, c});
    }

    dfs(1, 0);

    cout << max(dp[1][0], dp[1][1]) << endl;
}

int main () {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int t = 1;
    // cin >> t; 
    while (t--) {
        sol();
    }
    return 0;
}

Compilation message

election_campaign.cpp: In member function 'void fenTree::update(int, int)':
election_campaign.cpp:41:48: warning: statement has no effect [-Wunused-value]
   41 |     void update(int idx, int val) {idx++; for (idx; idx <= n; idx += (idx&-idx)) tree[idx] += val;}
      |                                                ^~~
election_campaign.cpp: In member function 'int fenTree::sum(int)':
election_campaign.cpp:42:48: warning: statement has no effect [-Wunused-value]
   42 |     int sum(int idx) {idx++; int ans = 0; for (idx; idx; idx -= (idx&-idx)) ans += tree[idx]; return ans;}
      |                                                ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 15704 KB Output is correct
2 Correct 3 ms 15708 KB Output is correct
3 Correct 3 ms 15708 KB Output is correct
4 Correct 3 ms 15708 KB Output is correct
5 Correct 70 ms 26376 KB Output is correct
6 Correct 38 ms 35248 KB Output is correct
7 Correct 79 ms 32316 KB Output is correct
8 Correct 43 ms 27676 KB Output is correct
9 Correct 87 ms 30904 KB Output is correct
10 Correct 47 ms 27748 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 15704 KB Output is correct
2 Correct 3 ms 15708 KB Output is correct
3 Correct 5 ms 15960 KB Output is correct
4 Correct 79 ms 35920 KB Output is correct
5 Correct 89 ms 36024 KB Output is correct
6 Correct 78 ms 35924 KB Output is correct
7 Correct 81 ms 36024 KB Output is correct
8 Correct 80 ms 35924 KB Output is correct
9 Correct 69 ms 35920 KB Output is correct
10 Correct 81 ms 35908 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 15704 KB Output is correct
2 Correct 3 ms 15708 KB Output is correct
3 Correct 5 ms 15960 KB Output is correct
4 Correct 79 ms 35920 KB Output is correct
5 Correct 89 ms 36024 KB Output is correct
6 Correct 78 ms 35924 KB Output is correct
7 Correct 81 ms 36024 KB Output is correct
8 Correct 80 ms 35924 KB Output is correct
9 Correct 69 ms 35920 KB Output is correct
10 Correct 81 ms 35908 KB Output is correct
11 Correct 10 ms 16356 KB Output is correct
12 Correct 77 ms 36152 KB Output is correct
13 Correct 80 ms 35920 KB Output is correct
14 Correct 71 ms 35876 KB Output is correct
15 Correct 92 ms 35908 KB Output is correct
16 Correct 85 ms 36072 KB Output is correct
17 Correct 78 ms 35916 KB Output is correct
18 Correct 78 ms 35988 KB Output is correct
19 Correct 75 ms 35924 KB Output is correct
20 Correct 84 ms 35964 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 102 ms 27656 KB Output is correct
2 Correct 82 ms 38376 KB Output is correct
3 Correct 169 ms 35244 KB Output is correct
4 Correct 90 ms 30728 KB Output is correct
5 Correct 107 ms 34864 KB Output is correct
6 Correct 76 ms 30820 KB Output is correct
7 Correct 108 ms 34516 KB Output is correct
8 Correct 118 ms 30548 KB Output is correct
9 Correct 70 ms 38452 KB Output is correct
10 Correct 138 ms 33548 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 15704 KB Output is correct
2 Correct 3 ms 15708 KB Output is correct
3 Correct 3 ms 15708 KB Output is correct
4 Correct 3 ms 15708 KB Output is correct
5 Correct 70 ms 26376 KB Output is correct
6 Correct 38 ms 35248 KB Output is correct
7 Correct 79 ms 32316 KB Output is correct
8 Correct 43 ms 27676 KB Output is correct
9 Correct 87 ms 30904 KB Output is correct
10 Correct 47 ms 27748 KB Output is correct
11 Correct 4 ms 15708 KB Output is correct
12 Correct 4 ms 15964 KB Output is correct
13 Correct 4 ms 15964 KB Output is correct
14 Correct 4 ms 15920 KB Output is correct
15 Correct 4 ms 15872 KB Output is correct
16 Correct 4 ms 15868 KB Output is correct
17 Correct 4 ms 15872 KB Output is correct
18 Correct 4 ms 15800 KB Output is correct
19 Correct 4 ms 15964 KB Output is correct
20 Correct 4 ms 15864 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 15704 KB Output is correct
2 Correct 3 ms 15708 KB Output is correct
3 Correct 3 ms 15708 KB Output is correct
4 Correct 3 ms 15708 KB Output is correct
5 Correct 70 ms 26376 KB Output is correct
6 Correct 38 ms 35248 KB Output is correct
7 Correct 79 ms 32316 KB Output is correct
8 Correct 43 ms 27676 KB Output is correct
9 Correct 87 ms 30904 KB Output is correct
10 Correct 47 ms 27748 KB Output is correct
11 Correct 3 ms 15704 KB Output is correct
12 Correct 3 ms 15708 KB Output is correct
13 Correct 5 ms 15960 KB Output is correct
14 Correct 79 ms 35920 KB Output is correct
15 Correct 89 ms 36024 KB Output is correct
16 Correct 78 ms 35924 KB Output is correct
17 Correct 81 ms 36024 KB Output is correct
18 Correct 80 ms 35924 KB Output is correct
19 Correct 69 ms 35920 KB Output is correct
20 Correct 81 ms 35908 KB Output is correct
21 Correct 10 ms 16356 KB Output is correct
22 Correct 77 ms 36152 KB Output is correct
23 Correct 80 ms 35920 KB Output is correct
24 Correct 71 ms 35876 KB Output is correct
25 Correct 92 ms 35908 KB Output is correct
26 Correct 85 ms 36072 KB Output is correct
27 Correct 78 ms 35916 KB Output is correct
28 Correct 78 ms 35988 KB Output is correct
29 Correct 75 ms 35924 KB Output is correct
30 Correct 84 ms 35964 KB Output is correct
31 Correct 102 ms 27656 KB Output is correct
32 Correct 82 ms 38376 KB Output is correct
33 Correct 169 ms 35244 KB Output is correct
34 Correct 90 ms 30728 KB Output is correct
35 Correct 107 ms 34864 KB Output is correct
36 Correct 76 ms 30820 KB Output is correct
37 Correct 108 ms 34516 KB Output is correct
38 Correct 118 ms 30548 KB Output is correct
39 Correct 70 ms 38452 KB Output is correct
40 Correct 138 ms 33548 KB Output is correct
41 Correct 4 ms 15708 KB Output is correct
42 Correct 4 ms 15964 KB Output is correct
43 Correct 4 ms 15964 KB Output is correct
44 Correct 4 ms 15920 KB Output is correct
45 Correct 4 ms 15872 KB Output is correct
46 Correct 4 ms 15868 KB Output is correct
47 Correct 4 ms 15872 KB Output is correct
48 Correct 4 ms 15800 KB Output is correct
49 Correct 4 ms 15964 KB Output is correct
50 Correct 4 ms 15864 KB Output is correct
51 Correct 134 ms 31220 KB Output is correct
52 Correct 94 ms 38688 KB Output is correct
53 Correct 117 ms 34056 KB Output is correct
54 Correct 83 ms 30920 KB Output is correct
55 Correct 97 ms 30724 KB Output is correct
56 Correct 80 ms 38624 KB Output is correct
57 Correct 117 ms 34540 KB Output is correct
58 Correct 94 ms 30924 KB Output is correct
59 Correct 112 ms 31056 KB Output is correct
60 Correct 82 ms 38736 KB Output is correct
61 Correct 110 ms 34640 KB Output is correct
62 Correct 98 ms 30872 KB Output is correct
63 Correct 104 ms 30520 KB Output is correct
64 Correct 80 ms 38736 KB Output is correct
65 Correct 123 ms 34552 KB Output is correct
66 Correct 94 ms 31008 KB Output is correct
67 Correct 116 ms 30924 KB Output is correct
68 Correct 79 ms 38568 KB Output is correct
69 Correct 101 ms 33616 KB Output is correct
70 Correct 84 ms 31060 KB Output is correct