Submission #88879

# Submission time Handle Problem Language Result Execution time Memory
88879 2018-12-09T17:31:21 Z Mercenary Election Campaign (JOI15_election_campaign) C++11
100 / 100
312 ms 149588 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 = 1e5 + 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);
    int jump = h[u] - h[v];
    for(int i = 0 ; i < 20 ; ++i)if((jump >> i) & 1)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)
{
    int now = 0;
    for(int c : adj[u])
        if(c != par)
        {
            DFS1(c , u);
            now += dp[c];
        }
    dp[u] = now;
    for(int i : q[u])
        dp[u] = max(dp[u] , now + query(in[qa[i]]) + query(in[qb[i]]) + qc[i]);
    update(in[u],out[u],now-dp[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:81: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:81: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 6 ms 5112 KB Output is correct
2 Correct 6 ms 5124 KB Output is correct
3 Correct 6 ms 5180 KB Output is correct
4 Correct 7 ms 5328 KB Output is correct
5 Correct 114 ms 19592 KB Output is correct
6 Correct 72 ms 28336 KB Output is correct
7 Correct 108 ms 28336 KB Output is correct
8 Correct 105 ms 28336 KB Output is correct
9 Correct 124 ms 28336 KB Output is correct
10 Correct 99 ms 28336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 28336 KB Output is correct
2 Correct 7 ms 28336 KB Output is correct
3 Correct 8 ms 28336 KB Output is correct
4 Correct 175 ms 37764 KB Output is correct
5 Correct 166 ms 40288 KB Output is correct
6 Correct 152 ms 42824 KB Output is correct
7 Correct 164 ms 45120 KB Output is correct
8 Correct 179 ms 47608 KB Output is correct
9 Correct 152 ms 50248 KB Output is correct
10 Correct 183 ms 52504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 28336 KB Output is correct
2 Correct 7 ms 28336 KB Output is correct
3 Correct 8 ms 28336 KB Output is correct
4 Correct 175 ms 37764 KB Output is correct
5 Correct 166 ms 40288 KB Output is correct
6 Correct 152 ms 42824 KB Output is correct
7 Correct 164 ms 45120 KB Output is correct
8 Correct 179 ms 47608 KB Output is correct
9 Correct 152 ms 50248 KB Output is correct
10 Correct 183 ms 52504 KB Output is correct
11 Correct 18 ms 52504 KB Output is correct
12 Correct 171 ms 55648 KB Output is correct
13 Correct 164 ms 58288 KB Output is correct
14 Correct 169 ms 61120 KB Output is correct
15 Correct 164 ms 63924 KB Output is correct
16 Correct 162 ms 66664 KB Output is correct
17 Correct 166 ms 69272 KB Output is correct
18 Correct 162 ms 72060 KB Output is correct
19 Correct 157 ms 74860 KB Output is correct
20 Correct 149 ms 77692 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 186 ms 77692 KB Output is correct
2 Correct 165 ms 80100 KB Output is correct
3 Correct 283 ms 80100 KB Output is correct
4 Correct 133 ms 80100 KB Output is correct
5 Correct 215 ms 84028 KB Output is correct
6 Correct 168 ms 84028 KB Output is correct
7 Correct 269 ms 88552 KB Output is correct
8 Correct 209 ms 88552 KB Output is correct
9 Correct 157 ms 97400 KB Output is correct
10 Correct 254 ms 97400 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 6 ms 5124 KB Output is correct
3 Correct 6 ms 5180 KB Output is correct
4 Correct 7 ms 5328 KB Output is correct
5 Correct 114 ms 19592 KB Output is correct
6 Correct 72 ms 28336 KB Output is correct
7 Correct 108 ms 28336 KB Output is correct
8 Correct 105 ms 28336 KB Output is correct
9 Correct 124 ms 28336 KB Output is correct
10 Correct 99 ms 28336 KB Output is correct
11 Correct 7 ms 97400 KB Output is correct
12 Correct 7 ms 97400 KB Output is correct
13 Correct 8 ms 97400 KB Output is correct
14 Correct 7 ms 97400 KB Output is correct
15 Correct 7 ms 97400 KB Output is correct
16 Correct 7 ms 97400 KB Output is correct
17 Correct 7 ms 97400 KB Output is correct
18 Correct 8 ms 97400 KB Output is correct
19 Correct 7 ms 97400 KB Output is correct
20 Correct 8 ms 97400 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 6 ms 5124 KB Output is correct
3 Correct 6 ms 5180 KB Output is correct
4 Correct 7 ms 5328 KB Output is correct
5 Correct 114 ms 19592 KB Output is correct
6 Correct 72 ms 28336 KB Output is correct
7 Correct 108 ms 28336 KB Output is correct
8 Correct 105 ms 28336 KB Output is correct
9 Correct 124 ms 28336 KB Output is correct
10 Correct 99 ms 28336 KB Output is correct
11 Correct 7 ms 28336 KB Output is correct
12 Correct 7 ms 28336 KB Output is correct
13 Correct 8 ms 28336 KB Output is correct
14 Correct 175 ms 37764 KB Output is correct
15 Correct 166 ms 40288 KB Output is correct
16 Correct 152 ms 42824 KB Output is correct
17 Correct 164 ms 45120 KB Output is correct
18 Correct 179 ms 47608 KB Output is correct
19 Correct 152 ms 50248 KB Output is correct
20 Correct 183 ms 52504 KB Output is correct
21 Correct 18 ms 52504 KB Output is correct
22 Correct 171 ms 55648 KB Output is correct
23 Correct 164 ms 58288 KB Output is correct
24 Correct 169 ms 61120 KB Output is correct
25 Correct 164 ms 63924 KB Output is correct
26 Correct 162 ms 66664 KB Output is correct
27 Correct 166 ms 69272 KB Output is correct
28 Correct 162 ms 72060 KB Output is correct
29 Correct 157 ms 74860 KB Output is correct
30 Correct 149 ms 77692 KB Output is correct
31 Correct 186 ms 77692 KB Output is correct
32 Correct 165 ms 80100 KB Output is correct
33 Correct 283 ms 80100 KB Output is correct
34 Correct 133 ms 80100 KB Output is correct
35 Correct 215 ms 84028 KB Output is correct
36 Correct 168 ms 84028 KB Output is correct
37 Correct 269 ms 88552 KB Output is correct
38 Correct 209 ms 88552 KB Output is correct
39 Correct 157 ms 97400 KB Output is correct
40 Correct 254 ms 97400 KB Output is correct
41 Correct 7 ms 97400 KB Output is correct
42 Correct 7 ms 97400 KB Output is correct
43 Correct 8 ms 97400 KB Output is correct
44 Correct 7 ms 97400 KB Output is correct
45 Correct 7 ms 97400 KB Output is correct
46 Correct 7 ms 97400 KB Output is correct
47 Correct 7 ms 97400 KB Output is correct
48 Correct 8 ms 97400 KB Output is correct
49 Correct 7 ms 97400 KB Output is correct
50 Correct 8 ms 97400 KB Output is correct
51 Correct 206 ms 97400 KB Output is correct
52 Correct 174 ms 105416 KB Output is correct
53 Correct 312 ms 105416 KB Output is correct
54 Correct 169 ms 105416 KB Output is correct
55 Correct 207 ms 105712 KB Output is correct
56 Correct 182 ms 116568 KB Output is correct
57 Correct 248 ms 116568 KB Output is correct
58 Correct 155 ms 116568 KB Output is correct
59 Correct 227 ms 117020 KB Output is correct
60 Correct 165 ms 127464 KB Output is correct
61 Correct 199 ms 127464 KB Output is correct
62 Correct 165 ms 127464 KB Output is correct
63 Correct 214 ms 127648 KB Output is correct
64 Correct 170 ms 138636 KB Output is correct
65 Correct 271 ms 138636 KB Output is correct
66 Correct 171 ms 138636 KB Output is correct
67 Correct 190 ms 138648 KB Output is correct
68 Correct 163 ms 149588 KB Output is correct
69 Correct 242 ms 149588 KB Output is correct
70 Correct 155 ms 149588 KB Output is correct