Submission #878991

# Submission time Handle Problem Language Result Execution time Memory
878991 2023-11-26T03:56:24 Z 12345678 Election Campaign (JOI15_election_campaign) C++17
100 / 100
163 ms 41832 KB
#include <bits/stdc++.h>

using namespace std;

const int nx=1e5+5, kx=17;

int n, m, a[nx], b[nx], c[nx], u, v, dp1[nx], dp2[nx], lvl[nx], pa[nx][kx], sm[nx];
vector<int> d[nx], l[nx];

void dfs(int u, int p)
{
    lvl[u]=lvl[p]+1;
    pa[u][0]=p;
    for (int i=1; i<kx; i++) pa[u][i]=pa[pa[u][i-1]][i-1];
    for (auto v:d[u]) if (v!=p) dfs(v, u);
}

int lca(int u, int v)
{
    if (lvl[u]>lvl[v]) swap(u, v);
    for (int i=kx-1; i>=0; i--) if (lvl[pa[v][i]]>=lvl[u]) v=pa[v][i];
    if (u==v) return v;
    for (int i=kx-1; i>=0; i--) if (pa[u][i]!=pa[v][i]) u=pa[u][i], v=pa[v][i];
    return pa[u][0];
}

void dfs2(int u, int p)
{
    for (auto v:d[u]) if (v!=p) dfs2(v, u), dp2[u]+=dp1[v];
    dp1[u]=dp2[u];
    for (auto i:l[u])
    {
        int tmp=dp2[u], x, tmp2=0;
        stack<int> s;
        while (a[i]!=u) s.push(a[i]), a[i]=pa[a[i]][0];
        while (!s.empty()) x=s.top(), s.pop(), tmp2+=sm[x], sm[x]=tmp2, pa[x][0]=u;
        tmp+=tmp2; tmp2=0;
        while (b[i]!=u) s.push(b[i]), b[i]=pa[b[i]][0];
        while (!s.empty()) x=s.top(), s.pop(), tmp2+=sm[x], sm[x]=tmp2, pa[x][0]=u;
        dp1[u]=max(dp1[u], tmp+tmp2+c[i]); 
    }
    sm[u]=dp2[u]-dp1[u];
    //cout<<u<<' '<<dp1[u]<<' '<<dp2[u]<<' '<<sm[u]<<'\n';
}

int main()
{
    cin.tie(NULL)->sync_with_stdio(false);
    cin>>n;
    for (int i=1; i<n; i++) cin>>u>>v, d[u].push_back(v), d[v].push_back(u);
    dfs(1, 1);
    cin>>m;
    for (int i=1; i<=m; i++) cin>>a[i]>>b[i]>>c[i], l[lca(a[i], b[i])].push_back(i);
    dfs2(1, 1);
    cout<<dp1[1];
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6744 KB Output is correct
2 Correct 3 ms 6748 KB Output is correct
3 Correct 1 ms 6748 KB Output is correct
4 Correct 2 ms 6748 KB Output is correct
5 Correct 49 ms 16672 KB Output is correct
6 Correct 38 ms 36688 KB Output is correct
7 Correct 56 ms 29888 KB Output is correct
8 Correct 29 ms 16468 KB Output is correct
9 Correct 59 ms 25688 KB Output is correct
10 Correct 31 ms 16652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6748 KB Output is correct
2 Correct 1 ms 6748 KB Output is correct
3 Correct 2 ms 7004 KB Output is correct
4 Correct 95 ms 38964 KB Output is correct
5 Correct 95 ms 41808 KB Output is correct
6 Correct 85 ms 41556 KB Output is correct
7 Correct 91 ms 41528 KB Output is correct
8 Correct 90 ms 41368 KB Output is correct
9 Correct 103 ms 41416 KB Output is correct
10 Correct 93 ms 41552 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6748 KB Output is correct
2 Correct 1 ms 6748 KB Output is correct
3 Correct 2 ms 7004 KB Output is correct
4 Correct 95 ms 38964 KB Output is correct
5 Correct 95 ms 41808 KB Output is correct
6 Correct 85 ms 41556 KB Output is correct
7 Correct 91 ms 41528 KB Output is correct
8 Correct 90 ms 41368 KB Output is correct
9 Correct 103 ms 41416 KB Output is correct
10 Correct 93 ms 41552 KB Output is correct
11 Correct 9 ms 7768 KB Output is correct
12 Correct 99 ms 41832 KB Output is correct
13 Correct 95 ms 41648 KB Output is correct
14 Correct 86 ms 41812 KB Output is correct
15 Correct 100 ms 41808 KB Output is correct
16 Correct 115 ms 41808 KB Output is correct
17 Correct 114 ms 41812 KB Output is correct
18 Correct 95 ms 41784 KB Output is correct
19 Correct 95 ms 41708 KB Output is correct
20 Correct 100 ms 41816 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 83 ms 18128 KB Output is correct
2 Correct 87 ms 38900 KB Output is correct
3 Correct 152 ms 31436 KB Output is correct
4 Correct 66 ms 20684 KB Output is correct
5 Correct 124 ms 32348 KB Output is correct
6 Correct 80 ms 20736 KB Output is correct
7 Correct 135 ms 31824 KB Output is correct
8 Correct 88 ms 21076 KB Output is correct
9 Correct 84 ms 41596 KB Output is correct
10 Correct 146 ms 29596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6744 KB Output is correct
2 Correct 3 ms 6748 KB Output is correct
3 Correct 1 ms 6748 KB Output is correct
4 Correct 2 ms 6748 KB Output is correct
5 Correct 49 ms 16672 KB Output is correct
6 Correct 38 ms 36688 KB Output is correct
7 Correct 56 ms 29888 KB Output is correct
8 Correct 29 ms 16468 KB Output is correct
9 Correct 59 ms 25688 KB Output is correct
10 Correct 31 ms 16652 KB Output is correct
11 Correct 2 ms 6748 KB Output is correct
12 Correct 2 ms 7004 KB Output is correct
13 Correct 2 ms 7000 KB Output is correct
14 Correct 2 ms 6744 KB Output is correct
15 Correct 2 ms 6748 KB Output is correct
16 Correct 2 ms 6748 KB Output is correct
17 Correct 2 ms 6748 KB Output is correct
18 Correct 2 ms 7004 KB Output is correct
19 Correct 2 ms 6748 KB Output is correct
20 Correct 2 ms 7004 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6744 KB Output is correct
2 Correct 3 ms 6748 KB Output is correct
3 Correct 1 ms 6748 KB Output is correct
4 Correct 2 ms 6748 KB Output is correct
5 Correct 49 ms 16672 KB Output is correct
6 Correct 38 ms 36688 KB Output is correct
7 Correct 56 ms 29888 KB Output is correct
8 Correct 29 ms 16468 KB Output is correct
9 Correct 59 ms 25688 KB Output is correct
10 Correct 31 ms 16652 KB Output is correct
11 Correct 1 ms 6748 KB Output is correct
12 Correct 1 ms 6748 KB Output is correct
13 Correct 2 ms 7004 KB Output is correct
14 Correct 95 ms 38964 KB Output is correct
15 Correct 95 ms 41808 KB Output is correct
16 Correct 85 ms 41556 KB Output is correct
17 Correct 91 ms 41528 KB Output is correct
18 Correct 90 ms 41368 KB Output is correct
19 Correct 103 ms 41416 KB Output is correct
20 Correct 93 ms 41552 KB Output is correct
21 Correct 9 ms 7768 KB Output is correct
22 Correct 99 ms 41832 KB Output is correct
23 Correct 95 ms 41648 KB Output is correct
24 Correct 86 ms 41812 KB Output is correct
25 Correct 100 ms 41808 KB Output is correct
26 Correct 115 ms 41808 KB Output is correct
27 Correct 114 ms 41812 KB Output is correct
28 Correct 95 ms 41784 KB Output is correct
29 Correct 95 ms 41708 KB Output is correct
30 Correct 100 ms 41816 KB Output is correct
31 Correct 83 ms 18128 KB Output is correct
32 Correct 87 ms 38900 KB Output is correct
33 Correct 152 ms 31436 KB Output is correct
34 Correct 66 ms 20684 KB Output is correct
35 Correct 124 ms 32348 KB Output is correct
36 Correct 80 ms 20736 KB Output is correct
37 Correct 135 ms 31824 KB Output is correct
38 Correct 88 ms 21076 KB Output is correct
39 Correct 84 ms 41596 KB Output is correct
40 Correct 146 ms 29596 KB Output is correct
41 Correct 2 ms 6748 KB Output is correct
42 Correct 2 ms 7004 KB Output is correct
43 Correct 2 ms 7000 KB Output is correct
44 Correct 2 ms 6744 KB Output is correct
45 Correct 2 ms 6748 KB Output is correct
46 Correct 2 ms 6748 KB Output is correct
47 Correct 2 ms 6748 KB Output is correct
48 Correct 2 ms 7004 KB Output is correct
49 Correct 2 ms 6748 KB Output is correct
50 Correct 2 ms 7004 KB Output is correct
51 Correct 90 ms 21528 KB Output is correct
52 Correct 98 ms 41700 KB Output is correct
53 Correct 152 ms 30304 KB Output is correct
54 Correct 74 ms 21072 KB Output is correct
55 Correct 95 ms 20896 KB Output is correct
56 Correct 101 ms 41820 KB Output is correct
57 Correct 119 ms 31608 KB Output is correct
58 Correct 69 ms 20964 KB Output is correct
59 Correct 132 ms 21520 KB Output is correct
60 Correct 95 ms 41780 KB Output is correct
61 Correct 130 ms 31828 KB Output is correct
62 Correct 67 ms 20968 KB Output is correct
63 Correct 101 ms 20968 KB Output is correct
64 Correct 93 ms 41620 KB Output is correct
65 Correct 163 ms 31692 KB Output is correct
66 Correct 68 ms 21072 KB Output is correct
67 Correct 87 ms 21068 KB Output is correct
68 Correct 107 ms 41636 KB Output is correct
69 Correct 115 ms 28720 KB Output is correct
70 Correct 70 ms 21072 KB Output is correct