답안 #310088

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
310088 2020-10-05T17:48:51 Z mohamedsobhi777 Election Campaign (JOI15_election_campaign) C++14
100 / 100
222 ms 37272 KB
#include<bits/stdc++.h>


#pragma GCC optimize("-Ofast")
//#pragma GCC optimize("trapv")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.2,popcnt,abm,mmx,avx2,tune=native")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-funroll-loops")

#define I inline void 
#define S struct 
#define vi vector<int> 
#define vii vector<pair<int,int>>
#define pii pair<int,int>
#define pll pair<ll,ll>

using namespace std ; 
using ll = long long ; 
using ld = long double ; 

const int N = 2e5 + 7 , mod = 1e9 + 7 ; 
const ll inf = 2e18 ; 

// How interesting!

int n, m, t ; 
vector<int> adj[N] ; 
int up[N][20] ;
int st[N] , en[N] ; 
vector< pair<pii,int> > vec[N] ;
ll dp[N], kdp[N] ; 

void dfs(int x, int p){
        st[x] = ++ t ; 
        up[x][0] = p ; 
        for(int i = 1; i < 20 ; ++ i)
                up[x][i] = up[up[x][i-1]][i-1] ; 
        for(auto u : adj[x]){
                if(u == p)continue ; 
                dfs(u , x) ; 
        }
        en[x] = ++ t; 
}

inline bool upper(int u, int v){return st[u] <= st[v] && en[v] <= en[u] ; }

int lca(int u, int v){
        if(upper(u , v))return u ; 
        if(upper(v , u))return v ; 
        for(int i = 19 ; ~i;  -- i)
                if(!upper(up[u][i] , v))
                        u = up[u][i] ; 
        return up[u][0] ; 
}

ll bit[N] ; 

inline void add(int pos , ll v){for(;pos<N;pos+=pos&-pos)bit[pos]+=v ;} ; 
ll upto(int pos){
        ll ret = 0 ;
        for(;pos;pos-=pos&-pos)
                ret+=bit[pos] ; 
        return ret;  
}
inline ll get(int l, int r){return upto(r) - upto(l - 1) ; }
inline ll walk(int u , int v){return get(st[v], st[u]); }

ll solve(int x, int p){
        ll ret = 0 ; 
        for(auto u : adj[x]){
                if(u == p)continue ; 
                kdp[x] += solve(u , x) ; 
        }

        for(auto u : vec[x]){
                int a = u.first.first ; 
                int b = u.first.second ; 
                ret = max(ret , walk(a , x) + walk(b , x) + u.second + kdp[x]) ; 
        }
        dp[x] =  max(ret , kdp[x]) ;
        add(st[x] , kdp[x] - dp[x]) ; 
        add(en[x] , dp[x] - kdp[x]) ; 
        return dp[x]; 
}

int main(){
        ios_base::sync_with_stdio(0) ;
        cin.tie(0) ; 
        //freopen("in.in" , "r" , stdin) ; 
        cin>> n ; 
        for(int i = 0 ;i < n - 1; ++ i){
                int u , v; 
                cin >> u >> v ; 
                adj[u].push_back(v) ; 
                adj[v].push_back(u) ;
        }
        dfs(1 , 1) ; 
        cin>> m ; 
        for(int i = 0 ; i < m; ++ i){
                int u , v , w ; 
                cin >> u >> v >> w ; 
                int lc = lca(u , v) ; 
                vec[lc].push_back({{u , v}, w}) ;
        }

        solve(1 , 1) ; 
        cout<< dp[1] ; 
        return 0 ; 
}

/*
        - bounds sir (segtree = 4N, eulerTour = 2N, ...)
        - a variable defined twice?
        - will overflow?
        - is it a good complexity?
        - don't mess up indices (0-indexed vs 1-indexed)
        - reset everything between testcases. 
*/
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 9856 KB Output is correct
2 Correct 6 ms 9856 KB Output is correct
3 Correct 6 ms 9856 KB Output is correct
4 Correct 7 ms 9984 KB Output is correct
5 Correct 105 ms 24824 KB Output is correct
6 Correct 67 ms 32504 KB Output is correct
7 Correct 117 ms 29688 KB Output is correct
8 Correct 84 ms 24420 KB Output is correct
9 Correct 115 ms 28152 KB Output is correct
10 Correct 84 ms 24312 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 9984 KB Output is correct
2 Correct 6 ms 9856 KB Output is correct
3 Correct 7 ms 9984 KB Output is correct
4 Correct 106 ms 34408 KB Output is correct
5 Correct 105 ms 37112 KB Output is correct
6 Correct 104 ms 36984 KB Output is correct
7 Correct 106 ms 36988 KB Output is correct
8 Correct 105 ms 36956 KB Output is correct
9 Correct 105 ms 36968 KB Output is correct
10 Correct 104 ms 36856 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 9984 KB Output is correct
2 Correct 6 ms 9856 KB Output is correct
3 Correct 7 ms 9984 KB Output is correct
4 Correct 106 ms 34408 KB Output is correct
5 Correct 105 ms 37112 KB Output is correct
6 Correct 104 ms 36984 KB Output is correct
7 Correct 106 ms 36988 KB Output is correct
8 Correct 105 ms 36956 KB Output is correct
9 Correct 105 ms 36968 KB Output is correct
10 Correct 104 ms 36856 KB Output is correct
11 Correct 15 ms 10752 KB Output is correct
12 Correct 111 ms 37240 KB Output is correct
13 Correct 111 ms 37272 KB Output is correct
14 Correct 108 ms 37240 KB Output is correct
15 Correct 111 ms 37240 KB Output is correct
16 Correct 110 ms 37240 KB Output is correct
17 Correct 110 ms 37212 KB Output is correct
18 Correct 107 ms 37216 KB Output is correct
19 Correct 112 ms 37240 KB Output is correct
20 Correct 115 ms 37240 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 174 ms 26288 KB Output is correct
2 Correct 104 ms 34468 KB Output is correct
3 Correct 208 ms 33912 KB Output is correct
4 Correct 137 ms 28464 KB Output is correct
5 Correct 183 ms 33528 KB Output is correct
6 Correct 140 ms 28392 KB Output is correct
7 Correct 202 ms 33204 KB Output is correct
8 Correct 175 ms 29048 KB Output is correct
9 Correct 103 ms 36984 KB Output is correct
10 Correct 203 ms 32044 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 9856 KB Output is correct
2 Correct 6 ms 9856 KB Output is correct
3 Correct 6 ms 9856 KB Output is correct
4 Correct 7 ms 9984 KB Output is correct
5 Correct 105 ms 24824 KB Output is correct
6 Correct 67 ms 32504 KB Output is correct
7 Correct 117 ms 29688 KB Output is correct
8 Correct 84 ms 24420 KB Output is correct
9 Correct 115 ms 28152 KB Output is correct
10 Correct 84 ms 24312 KB Output is correct
11 Correct 7 ms 10016 KB Output is correct
12 Correct 8 ms 9984 KB Output is correct
13 Correct 7 ms 9984 KB Output is correct
14 Correct 7 ms 9984 KB Output is correct
15 Correct 7 ms 9984 KB Output is correct
16 Correct 7 ms 9984 KB Output is correct
17 Correct 7 ms 9984 KB Output is correct
18 Correct 8 ms 9984 KB Output is correct
19 Correct 7 ms 9984 KB Output is correct
20 Correct 7 ms 9984 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 9856 KB Output is correct
2 Correct 6 ms 9856 KB Output is correct
3 Correct 6 ms 9856 KB Output is correct
4 Correct 7 ms 9984 KB Output is correct
5 Correct 105 ms 24824 KB Output is correct
6 Correct 67 ms 32504 KB Output is correct
7 Correct 117 ms 29688 KB Output is correct
8 Correct 84 ms 24420 KB Output is correct
9 Correct 115 ms 28152 KB Output is correct
10 Correct 84 ms 24312 KB Output is correct
11 Correct 7 ms 9984 KB Output is correct
12 Correct 6 ms 9856 KB Output is correct
13 Correct 7 ms 9984 KB Output is correct
14 Correct 106 ms 34408 KB Output is correct
15 Correct 105 ms 37112 KB Output is correct
16 Correct 104 ms 36984 KB Output is correct
17 Correct 106 ms 36988 KB Output is correct
18 Correct 105 ms 36956 KB Output is correct
19 Correct 105 ms 36968 KB Output is correct
20 Correct 104 ms 36856 KB Output is correct
21 Correct 15 ms 10752 KB Output is correct
22 Correct 111 ms 37240 KB Output is correct
23 Correct 111 ms 37272 KB Output is correct
24 Correct 108 ms 37240 KB Output is correct
25 Correct 111 ms 37240 KB Output is correct
26 Correct 110 ms 37240 KB Output is correct
27 Correct 110 ms 37212 KB Output is correct
28 Correct 107 ms 37216 KB Output is correct
29 Correct 112 ms 37240 KB Output is correct
30 Correct 115 ms 37240 KB Output is correct
31 Correct 174 ms 26288 KB Output is correct
32 Correct 104 ms 34468 KB Output is correct
33 Correct 208 ms 33912 KB Output is correct
34 Correct 137 ms 28464 KB Output is correct
35 Correct 183 ms 33528 KB Output is correct
36 Correct 140 ms 28392 KB Output is correct
37 Correct 202 ms 33204 KB Output is correct
38 Correct 175 ms 29048 KB Output is correct
39 Correct 103 ms 36984 KB Output is correct
40 Correct 203 ms 32044 KB Output is correct
41 Correct 7 ms 10016 KB Output is correct
42 Correct 8 ms 9984 KB Output is correct
43 Correct 7 ms 9984 KB Output is correct
44 Correct 7 ms 9984 KB Output is correct
45 Correct 7 ms 9984 KB Output is correct
46 Correct 7 ms 9984 KB Output is correct
47 Correct 7 ms 9984 KB Output is correct
48 Correct 8 ms 9984 KB Output is correct
49 Correct 7 ms 9984 KB Output is correct
50 Correct 7 ms 9984 KB Output is correct
51 Correct 180 ms 29432 KB Output is correct
52 Correct 112 ms 37160 KB Output is correct
53 Correct 222 ms 32552 KB Output is correct
54 Correct 126 ms 28720 KB Output is correct
55 Correct 177 ms 29104 KB Output is correct
56 Correct 112 ms 37240 KB Output is correct
57 Correct 191 ms 33144 KB Output is correct
58 Correct 145 ms 28588 KB Output is correct
59 Correct 184 ms 29304 KB Output is correct
60 Correct 111 ms 37240 KB Output is correct
61 Correct 193 ms 33404 KB Output is correct
62 Correct 143 ms 28368 KB Output is correct
63 Correct 180 ms 28896 KB Output is correct
64 Correct 111 ms 37240 KB Output is correct
65 Correct 210 ms 33212 KB Output is correct
66 Correct 130 ms 28852 KB Output is correct
67 Correct 170 ms 28904 KB Output is correct
68 Correct 113 ms 37240 KB Output is correct
69 Correct 190 ms 31992 KB Output is correct
70 Correct 141 ms 28892 KB Output is correct