Submission #878989

# Submission time Handle Problem Language Result Execution time Memory
878989 2023-11-26T03:40:06 Z 12345678 Election Campaign (JOI15_election_campaign) C++17
20 / 100
1000 ms 30552 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];
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];
        while (a[i]!=u) tmp+=dp2[a[i]]-dp1[a[i]], a[i]=pa[a[i]][0];
        while (b[i]!=u) tmp+=dp2[b[i]]-dp1[b[i]], b[i]=pa[b[i]][0];
        dp1[u]=max(dp1[u], tmp+c[i]); 
        //cout<<i<<' '<<tmp<<' '<<c[i]<<'\n';
    }
    //cout<<"value"<<u<<' '<<dp1[u]<<' '<<dp2[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 2 ms 6744 KB Output is correct
3 Correct 1 ms 6748 KB Output is correct
4 Correct 1 ms 6748 KB Output is correct
5 Correct 35 ms 17440 KB Output is correct
6 Correct 34 ms 26456 KB Output is correct
7 Correct 56 ms 23180 KB Output is correct
8 Correct 31 ms 17244 KB Output is correct
9 Correct 54 ms 21544 KB Output is correct
10 Correct 36 ms 17244 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6744 KB Output is correct
2 Correct 1 ms 6748 KB Output is correct
3 Correct 2 ms 6748 KB Output is correct
4 Execution timed out 1040 ms 30372 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6744 KB Output is correct
2 Correct 1 ms 6748 KB Output is correct
3 Correct 2 ms 6748 KB Output is correct
4 Execution timed out 1040 ms 30372 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 107 ms 20452 KB Output is correct
2 Correct 990 ms 30552 KB Output is correct
3 Execution timed out 1032 ms 26452 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6744 KB Output is correct
2 Correct 2 ms 6744 KB Output is correct
3 Correct 1 ms 6748 KB Output is correct
4 Correct 1 ms 6748 KB Output is correct
5 Correct 35 ms 17440 KB Output is correct
6 Correct 34 ms 26456 KB Output is correct
7 Correct 56 ms 23180 KB Output is correct
8 Correct 31 ms 17244 KB Output is correct
9 Correct 54 ms 21544 KB Output is correct
10 Correct 36 ms 17244 KB Output is correct
11 Correct 2 ms 6748 KB Output is correct
12 Correct 3 ms 7004 KB Output is correct
13 Correct 2 ms 6744 KB Output is correct
14 Correct 2 ms 6748 KB Output is correct
15 Correct 2 ms 6744 KB Output is correct
16 Correct 2 ms 6748 KB Output is correct
17 Correct 2 ms 6748 KB Output is correct
18 Correct 4 ms 6748 KB Output is correct
19 Correct 3 ms 6748 KB Output is correct
20 Correct 3 ms 7004 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6744 KB Output is correct
2 Correct 2 ms 6744 KB Output is correct
3 Correct 1 ms 6748 KB Output is correct
4 Correct 1 ms 6748 KB Output is correct
5 Correct 35 ms 17440 KB Output is correct
6 Correct 34 ms 26456 KB Output is correct
7 Correct 56 ms 23180 KB Output is correct
8 Correct 31 ms 17244 KB Output is correct
9 Correct 54 ms 21544 KB Output is correct
10 Correct 36 ms 17244 KB Output is correct
11 Correct 1 ms 6744 KB Output is correct
12 Correct 1 ms 6748 KB Output is correct
13 Correct 2 ms 6748 KB Output is correct
14 Execution timed out 1040 ms 30372 KB Time limit exceeded
15 Halted 0 ms 0 KB -