Submission #88870

# Submission time Handle Problem Language Result Execution time Memory
88870 2018-12-09T16:54:41 Z Mercenary Election Campaign (JOI15_election_campaign) C++11
0 / 100
214 ms 27788 KB
#include<bits/stdc++.h>

using namespace std;
#define taskname "TEST"
#define pb	push_back
typedef long double ld;
typedef long long ll;
const int maxn = 2e5 + 5;

int n , m;
int dp[maxn] , lost[maxn];
int P[maxn][20] , h[maxn];
int in[maxn] , out[maxn];
int qa[maxn] , qb[maxn] , qc[maxn];
vector<int> adj[maxn] , q[maxn];
void DFS(int u , int par)
{
    static int nTime = 0;
    in[u] = ++nTime;
    for(int c : adj[u])
        if(c != par){
            h[c] = h[u] + 1;
            P[c][0] = u;
            for(int i = 1 ; i < 20 ; ++i)
                P[c][i] = P[P[c][i - 1]][i - 1];
            DFS(c , u);
        }
    out[u] = nTime;
}

int LCA(int u , int v)
{
    if(h[u] < h[v])swap(u ,v);
    for(int i = 0 ; i < 20 ; ++i)
        if(h[u] - (1 << i) >= h[v])u = P[u][i];
    if(u == v)return u;
    for(int i = 19 ; i >= 0 ; --i)
        if(P[u][i] != P[v][i])u = P[u][i] , v = P[v][i];
    return P[u][0];
}

int bit[maxn];

int query(int x)
{
    int res = 0;
    for( ; x > 0 ; x &= x - 1)
        res += bit[x];
    return res;
}

void update(int l , int h , int c)
{
    for( ; l <= n ; l += l & -l)
        bit[l] += c;
    for( ++h ; h <= n ; h += h & -h)
        bit[h] -= c;
}

void DFS1(int u , int par)
{
    for(int c : adj[u])
        if(c != par)
        {
            DFS1(c , u);
            dp[u] += dp[c];
        }
    for(int c : q[u])
        lost[u] = max(lost[u] , qc[c] - query(in[qb[c]]) - query(in[qa[c]]));
    update(in[u],out[u],lost[u]);
    dp[u] += lost[u];
}

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	if(fopen(taskname".INP","r"))
        freopen(taskname".INP", "r",stdin) ,
        freopen(taskname".OUT", "w",stdout);
    cin >> n;
    for(int i = 1 ; i < n ; ++i)
    {
        int u , v;cin >> u >> v;
        adj[u].pb(v);adj[v].pb(u);
    }
    DFS(1 , 0);
    cin >> m;
    for(int i = 1 ; i <= m ; ++i)
    {
        cin >> qa[i] >> qb[i] >> qc[i];
        q[LCA(qa[i] , qb[i])].pb(i);
    }
    DFS1(1 , 0);
    cout << dp[1];
}

Compilation message

election_campaign.cpp: In function 'int main()':
election_campaign.cpp:79:44: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
         freopen(taskname".INP", "r",stdin) ,
         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~
         freopen(taskname".OUT", "w",stdout);
         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
election_campaign.cpp:79:44: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
# Verdict Execution time Memory Grader output
1 Correct 11 ms 9976 KB Output is correct
2 Correct 11 ms 10112 KB Output is correct
3 Incorrect 11 ms 10140 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 10 ms 10140 KB Output is correct
2 Incorrect 10 ms 10140 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 10 ms 10140 KB Output is correct
2 Incorrect 10 ms 10140 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 214 ms 27788 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 11 ms 9976 KB Output is correct
2 Correct 11 ms 10112 KB Output is correct
3 Incorrect 11 ms 10140 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 11 ms 9976 KB Output is correct
2 Correct 11 ms 10112 KB Output is correct
3 Incorrect 11 ms 10140 KB Output isn't correct
4 Halted 0 ms 0 KB -