Submission #550254

# Submission time Handle Problem Language Result Execution time Memory
550254 2022-04-17T17:56:40 Z farhan132 Election Campaign (JOI15_election_campaign) C++17
100 / 100
566 ms 58156 KB
/***Farhan132***/
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
//#pragma GCC optimization ("unroll-loops")
//#pragma GCC optimize("Ofast,fast-math")
 
 
#include <bits/stdc++.h>
 
 
 
using namespace std;
 
typedef int ll;
typedef unsigned long long ull;
typedef double dd;
typedef vector<ll> vll;
typedef pair<ll , ll> ii;
typedef vector< ii > vii;
typedef pair < pair < ll , ll > , pair < ll , ll > > cm; 
typedef tuple < ll,  ll, ll > tp;
 
#define ff first
#define ss second
#define pb push_back
#define in insert
#define f0(b) for(int i=0;i<(b);i++)
#define f00(b) for(int j=0;j<(b);j++)
#define f1(b) for(int i=1;i<=(b);i++)
#define f11(b) for(int j=1;j<=(b);j++)
#define f2(a,b) for(int i=(a);i<=(b);i++)
#define f22(a,b) for(int j=(a);j<=(b);j++)
#define sf(a) scanf("%lld",&a)
#define sff(a,b) scanf("%lld %lld", &a , &b)
#define pf(a) printf("%lld\n",a)
#define pff(a,b) printf("%lld %lld\n", a , b)
#define bug printf("**!\n")
#define mem(a , b) memset(a, b ,sizeof(a))
#define front_zero(n) __builtin_clzll(n)
#define back_zero(n) __builtin_ctzll(n)
#define total_one(n) __builtin_popcount(n)
#define clean fflush(stdout)
 
const ll mod =  (ll) 998244353;
//const ll mod =  (ll) 1e9 + 7;
const ll maxn = (ll)1e8 + 5;
const int nnn = 1048590;
const int inf = numeric_limits<int>::max()-1;
//const ll INF = numeric_limits<ll>::max()-1;
const ll INF = (ll)1e18;
 
ll dx[]={0,1,0,-1};
ll dy[]={1,0,-1,0};
ll dxx[]={0,1,0,-1,1,1,-1,-1};
ll dyy[]={1,0,-1,0,1,-1,1,-1};
 
bool USACO = 0;
 
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
 
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
 
template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

const ll N = 1e5 + 5;

ll par[N][18];
ll sub[N], dp[N];
vector < ll > v[N];
vector < ll > g[N];
ll sub_sum[N][18], dp_sum[N][18];
ll mx[N], d[N];
vector < tuple < ll , ll , ll > > Q[N];
ll lift(ll s, ll k){
    for(ll i = 17; i >= 0; i--){
        if(k & (1 << i)) s = par[s][i];
    }
    return s;
}
ll get(ll s, ll k, bool ok = 0){
    ll ans = 0;
    for(ll i = 17; i >= 0; i--){
        if(k & (1 << i)){
            ans += sub_sum[s][i] - dp_sum[s][i];
            s = par[s][i];
        }
    }
   // cout << ans << '\n';
    return ans;
}
ll lca(ll x, ll y){
    if(d[x] < d[y]) swap(x, y);
    for(ll i = 17; i >= 0; i--){
        if(d[x] - (1 << i) >= d[y]) x = par[x][i];
    }
    if(x == y) return x;
    for(ll i = 17; i >= 0; i--){
        if(par[x][i] != par[y][i]) x = par[x][i], y = par[y][i];
    }
    return par[x][0];
}
void dfs(ll s, ll p, ll dth){
    d[s] = dth;
    par[s][0] = p;
    for(ll i = 1; i <= 17; i++) par[s][i] = par[par[s][i - 1]][i - 1];
    for(auto u : v[s]){
        if(u - p){
            dfs(u, s , dth + 1);
        }
    }
    return;
}


void final(ll s, ll p){
    dp[s] = 0, sub[s] = 0;
    for(auto u : v[s]){
        if(u - p){
            final(u, s);
            sub[s] += dp[u];
        }
    }
    dp[s] = sub[s];

    for(auto [x, y, c] : Q[s]){
        ll res = sub[s] + c;
        if(x > y) swap(x, y);
        if(x != s) res += get(x, d[x] - d[s]);
        if(y != s) res += get(y, d[y] - d[s]);
        
        //cout << x << ' ' << y<< ' ' << c << ' ' << res << '\n';
        dp[s] = max(dp[s], res);
    }
    dp_sum[s][0] = dp[s];
    sub_sum[s][0] = sub[s];
    mx[s] = 0;
    g[lift(s, 1)].pb(s);
    sort(g[s].begin(), g[s].end(), [](ll i, ll j){
        return mx[i] < mx[j];
    });
    for(auto u : g[s]){
        mx[u]++;
        dp_sum[u][mx[u]] = dp_sum[u][mx[u] - 1] + dp_sum[par[u][mx[u] - 1]][mx[u] - 1];
        sub_sum[u][mx[u]] = sub_sum[u][mx[u] - 1] + sub_sum[par[u][mx[u] - 1]][mx[u] - 1];
        g[lift(u, ( (1 << (mx[u] + 1)) - 1))].pb(u);
    }
    //cout << "! " << s << ' ' << dp[s] << '\n';
    return;
}

void solve(){

  ll n;
  cin >> n;

  for(ll i = 1; i < n; i++){
    ll x, y;
    cin >> x >> y;
    v[x].pb(y);
    v[y].pb(x);
  }
  mem(par, 0);
  mem(dp_sum, 0);
  mem(sub_sum, 0);

  ll q; cin >> q;
  dfs(1, 0, 1);

  for(ll i = 1; i <= q; i++){
    ll x,y,c; cin >> x >> y >> c;
    Q[lca(x,y)].pb({x, y, c});
  }
  final(1, 0);
  cout << dp[1] << '\n';
  
  return;
}
 
int main() {
    
    
    #ifdef LOCAL
        freopen("in", "r", stdin);
        freopen("out", "w", stdout);
        auto start_time = clock();
    #else 
       ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL);
    #endif
 
    //pre(N);
 
    ll T = 1, CT = 0;  //cin >> T; 
 
    while(T--){
       //  cout << "Case #" << ++CT <<": " ;
        solve();
    }
    #ifdef LOCAL
        auto end_time = clock();
        cerr<< "Execution time: "<<(end_time - start_time)*(int)1e3/CLOCKS_PER_SEC<<" ms\n";
    #endif
 
    return 0;
}

Compilation message

election_campaign.cpp: In function 'int main()':
election_campaign.cpp:194:15: warning: unused variable 'CT' [-Wunused-variable]
  194 |     ll T = 1, CT = 0;  //cin >> T;
      |               ^~
# Verdict Execution time Memory Grader output
1 Correct 13 ms 28500 KB Output is correct
2 Correct 13 ms 28508 KB Output is correct
3 Correct 13 ms 28500 KB Output is correct
4 Correct 13 ms 28480 KB Output is correct
5 Correct 147 ms 37844 KB Output is correct
6 Correct 260 ms 54512 KB Output is correct
7 Correct 436 ms 51548 KB Output is correct
8 Correct 121 ms 36528 KB Output is correct
9 Correct 407 ms 49340 KB Output is correct
10 Correct 106 ms 36320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 28512 KB Output is correct
2 Correct 17 ms 28508 KB Output is correct
3 Correct 16 ms 28720 KB Output is correct
4 Correct 342 ms 57764 KB Output is correct
5 Correct 357 ms 57692 KB Output is correct
6 Correct 300 ms 57716 KB Output is correct
7 Correct 354 ms 57724 KB Output is correct
8 Correct 314 ms 57772 KB Output is correct
9 Correct 330 ms 57892 KB Output is correct
10 Correct 323 ms 57760 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 28512 KB Output is correct
2 Correct 17 ms 28508 KB Output is correct
3 Correct 16 ms 28720 KB Output is correct
4 Correct 342 ms 57764 KB Output is correct
5 Correct 357 ms 57692 KB Output is correct
6 Correct 300 ms 57716 KB Output is correct
7 Correct 354 ms 57724 KB Output is correct
8 Correct 314 ms 57772 KB Output is correct
9 Correct 330 ms 57892 KB Output is correct
10 Correct 323 ms 57760 KB Output is correct
11 Correct 24 ms 29384 KB Output is correct
12 Correct 357 ms 57980 KB Output is correct
13 Correct 347 ms 58044 KB Output is correct
14 Correct 319 ms 58056 KB Output is correct
15 Correct 355 ms 57976 KB Output is correct
16 Correct 322 ms 58036 KB Output is correct
17 Correct 346 ms 58100 KB Output is correct
18 Correct 331 ms 58060 KB Output is correct
19 Correct 311 ms 57964 KB Output is correct
20 Correct 333 ms 58100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 220 ms 40940 KB Output is correct
2 Correct 324 ms 57728 KB Output is correct
3 Correct 566 ms 54292 KB Output is correct
4 Correct 179 ms 39300 KB Output is correct
5 Correct 479 ms 53556 KB Output is correct
6 Correct 155 ms 39468 KB Output is correct
7 Correct 554 ms 53276 KB Output is correct
8 Correct 212 ms 41200 KB Output is correct
9 Correct 321 ms 57748 KB Output is correct
10 Correct 492 ms 51716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 28500 KB Output is correct
2 Correct 13 ms 28508 KB Output is correct
3 Correct 13 ms 28500 KB Output is correct
4 Correct 13 ms 28480 KB Output is correct
5 Correct 147 ms 37844 KB Output is correct
6 Correct 260 ms 54512 KB Output is correct
7 Correct 436 ms 51548 KB Output is correct
8 Correct 121 ms 36528 KB Output is correct
9 Correct 407 ms 49340 KB Output is correct
10 Correct 106 ms 36320 KB Output is correct
11 Correct 14 ms 28644 KB Output is correct
12 Correct 14 ms 28756 KB Output is correct
13 Correct 15 ms 28628 KB Output is correct
14 Correct 14 ms 28512 KB Output is correct
15 Correct 14 ms 28628 KB Output is correct
16 Correct 15 ms 28656 KB Output is correct
17 Correct 14 ms 28524 KB Output is correct
18 Correct 14 ms 28648 KB Output is correct
19 Correct 15 ms 28628 KB Output is correct
20 Correct 14 ms 28780 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 28500 KB Output is correct
2 Correct 13 ms 28508 KB Output is correct
3 Correct 13 ms 28500 KB Output is correct
4 Correct 13 ms 28480 KB Output is correct
5 Correct 147 ms 37844 KB Output is correct
6 Correct 260 ms 54512 KB Output is correct
7 Correct 436 ms 51548 KB Output is correct
8 Correct 121 ms 36528 KB Output is correct
9 Correct 407 ms 49340 KB Output is correct
10 Correct 106 ms 36320 KB Output is correct
11 Correct 13 ms 28512 KB Output is correct
12 Correct 17 ms 28508 KB Output is correct
13 Correct 16 ms 28720 KB Output is correct
14 Correct 342 ms 57764 KB Output is correct
15 Correct 357 ms 57692 KB Output is correct
16 Correct 300 ms 57716 KB Output is correct
17 Correct 354 ms 57724 KB Output is correct
18 Correct 314 ms 57772 KB Output is correct
19 Correct 330 ms 57892 KB Output is correct
20 Correct 323 ms 57760 KB Output is correct
21 Correct 24 ms 29384 KB Output is correct
22 Correct 357 ms 57980 KB Output is correct
23 Correct 347 ms 58044 KB Output is correct
24 Correct 319 ms 58056 KB Output is correct
25 Correct 355 ms 57976 KB Output is correct
26 Correct 322 ms 58036 KB Output is correct
27 Correct 346 ms 58100 KB Output is correct
28 Correct 331 ms 58060 KB Output is correct
29 Correct 311 ms 57964 KB Output is correct
30 Correct 333 ms 58100 KB Output is correct
31 Correct 220 ms 40940 KB Output is correct
32 Correct 324 ms 57728 KB Output is correct
33 Correct 566 ms 54292 KB Output is correct
34 Correct 179 ms 39300 KB Output is correct
35 Correct 479 ms 53556 KB Output is correct
36 Correct 155 ms 39468 KB Output is correct
37 Correct 554 ms 53276 KB Output is correct
38 Correct 212 ms 41200 KB Output is correct
39 Correct 321 ms 57748 KB Output is correct
40 Correct 492 ms 51716 KB Output is correct
41 Correct 14 ms 28644 KB Output is correct
42 Correct 14 ms 28756 KB Output is correct
43 Correct 15 ms 28628 KB Output is correct
44 Correct 14 ms 28512 KB Output is correct
45 Correct 14 ms 28628 KB Output is correct
46 Correct 15 ms 28656 KB Output is correct
47 Correct 14 ms 28524 KB Output is correct
48 Correct 14 ms 28648 KB Output is correct
49 Correct 15 ms 28628 KB Output is correct
50 Correct 14 ms 28780 KB Output is correct
51 Correct 231 ms 41368 KB Output is correct
52 Correct 319 ms 58128 KB Output is correct
53 Correct 555 ms 52384 KB Output is correct
54 Correct 153 ms 39652 KB Output is correct
55 Correct 274 ms 41160 KB Output is correct
56 Correct 305 ms 58044 KB Output is correct
57 Correct 497 ms 53156 KB Output is correct
58 Correct 152 ms 39732 KB Output is correct
59 Correct 226 ms 41348 KB Output is correct
60 Correct 361 ms 58156 KB Output is correct
61 Correct 510 ms 53052 KB Output is correct
62 Correct 149 ms 39572 KB Output is correct
63 Correct 212 ms 41260 KB Output is correct
64 Correct 334 ms 57996 KB Output is correct
65 Correct 509 ms 53024 KB Output is correct
66 Correct 154 ms 39636 KB Output is correct
67 Correct 204 ms 41268 KB Output is correct
68 Correct 324 ms 58048 KB Output is correct
69 Correct 476 ms 51416 KB Output is correct
70 Correct 155 ms 39680 KB Output is correct