Submission #964594

# Submission time Handle Problem Language Result Execution time Memory
964594 2024-04-17T07:30:27 Z angella Factories (JOI14_factories) C++17
100 / 100
3632 ms 194100 KB
/*
    IN THE NAME OF GOD
*/
#include <bits/stdc++.h>
#include "factories.h"

// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")

using namespace std;

typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef long double ld;

#define F                                      first
#define S                                      second
#define Mp                                     make_pair
#define pb                                     push_back
#define pf                                     push_front
#define size(x)                                ((ll)x.size())
#define all(x)                                 (x).begin(),(x).end()
#define kill(x)		                           cout << x << '\n', exit(0);
#define fuck(x)                                cout << "(" << #x << " , " << x << ")" << endl
#define endl                                   '\n'

const int N = 5e5+23, lg = 19;
ll Mod = 1e9+7; //998244353;

inline ll MOD(ll a, ll mod=Mod) {a%=mod; (a<0)&&(a+=mod); return a;}
inline ll poww(ll a, ll b, ll mod=Mod) {
    ll ans = 1;
    a=MOD(a, mod);
    while (b) {
        if (b & 1) ans = MOD(ans*a, mod);
        b >>= 1;
        a = MOD(a*a, mod);
    }
    return ans;
}

int n, tin[N], tout[N], tim, par[lg][N], h[N], mark[N];
ll wh[N], dp[2][N];
vector<pll> adj[N];
vector<int> tree[N];

void init(int v, int p) {
    par[0][v] = p, tin[v] = ++tim;
    for(int i=1; i<lg; i++) par[i][v] = par[i-1][par[i-1][v]];
    for(auto u : adj[v]) {
        if(u.F == p) continue;
        h[u.F] = h[v] + 1, wh[u.F] = wh[v] + u.S;
        init(u.F, v);
    }
    tout[v] = tim+1;
}

int getPar(int v, int dist) {
    for(int i=0; i<lg; i++) if((dist>>i)%2 == 1) v=par[i][v];
    return v;
}

int LCA(int v, int u) {
    if(h[v] > h[u]) swap(v, u);
    u = getPar(u, h[u]-h[v]);
    if(v == u) return v;
    for(int i=lg-1; i>=0; i--) {
        if(par[i][v] != par[i][u]) v=par[i][v], u=par[i][u];
    }
    return par[0][v];
}

void Init(int _n, int _a[], int _b[], int _d[]) {
    n=_n;
    for(int i=0; i<n-1; i++) {
        _a[i] ++, _b[i] ++;
adj[_a[i]].pb({_b[i], _d[i]}); adj[_b[i]].pb({_a[i], _d[i]});
    }
    init(1, 0);
}

bool cmp(int i, int j) {
    return (tin[i]<tin[j] ? true : false);
}
ll ans = 1e18;

void dfs(int v, int p=0) {
    dp[0][v]=dp[1][v]=1e18;
    if(mark[v]==1) dp[0][v]=0;
    if(mark[v]==2) dp[1][v]=0;
    for(auto u : tree[v]) {
        dfs(u);
        dp[0][v]=min(dp[0][v], dp[0][u]+wh[u]-wh[v]);
        dp[1][v]=min(dp[1][v], dp[1][u]+wh[u]-wh[v]);
    }
    ans = min(ans, dp[0][v]+dp[1][v]);
}

ll Query(int s, int V[], int t, int U[]) {
    //return 2;
    vector<int> vec;
    for(int i=0;i<s;i++) {
        V[i]++; mark[V[i]] = 1; vec.pb(V[i]);
    }
    for(int i=0;i<t;i++) {
        U[i]++; mark[U[i]] = 2; vec.pb(U[i]);
    }
    sort(all(vec), cmp);
    int ttt = size(vec);
    for(int i=0; i<ttt-1; i++) vec.pb(LCA(vec[i],vec[i+1]));
    
    sort(all(vec));
    vec.resize(unique(all(vec)) - vec.begin());
    sort(all(vec), cmp);
    
    vector<int> st; st.pb(vec[0]);
    //for(auto it : vec) fuck(it);

    for(int i=1; i<size(vec); i++) {
        while(tout[st.back()]<=tin[vec[i]]) st.pop_back();
        tree[st.back()].pb(vec[i]);
        st.pb(vec[i]);
    }
    //fuck("here");
    ans = 1e18;
    dfs(st[0]);
    for(auto it : vec) {
        tree[it].clear();
        mark[it] = 0;
    }
    return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 40 ms 80720 KB Output is correct
2 Correct 840 ms 88656 KB Output is correct
3 Correct 888 ms 88648 KB Output is correct
4 Correct 846 ms 91040 KB Output is correct
5 Correct 750 ms 91152 KB Output is correct
6 Correct 637 ms 90636 KB Output is correct
7 Correct 874 ms 90712 KB Output is correct
8 Correct 870 ms 90820 KB Output is correct
9 Correct 781 ms 91216 KB Output is correct
10 Correct 638 ms 92664 KB Output is correct
11 Correct 940 ms 90704 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 82792 KB Output is correct
2 Correct 1567 ms 142044 KB Output is correct
3 Correct 2216 ms 150996 KB Output is correct
4 Correct 1268 ms 143652 KB Output is correct
5 Correct 2086 ms 191696 KB Output is correct
6 Correct 2507 ms 151764 KB Output is correct
7 Correct 1471 ms 99844 KB Output is correct
8 Correct 892 ms 97160 KB Output is correct
9 Correct 1308 ms 106260 KB Output is correct
10 Correct 1450 ms 100476 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 40 ms 80720 KB Output is correct
2 Correct 840 ms 88656 KB Output is correct
3 Correct 888 ms 88648 KB Output is correct
4 Correct 846 ms 91040 KB Output is correct
5 Correct 750 ms 91152 KB Output is correct
6 Correct 637 ms 90636 KB Output is correct
7 Correct 874 ms 90712 KB Output is correct
8 Correct 870 ms 90820 KB Output is correct
9 Correct 781 ms 91216 KB Output is correct
10 Correct 638 ms 92664 KB Output is correct
11 Correct 940 ms 90704 KB Output is correct
12 Correct 14 ms 82792 KB Output is correct
13 Correct 1567 ms 142044 KB Output is correct
14 Correct 2216 ms 150996 KB Output is correct
15 Correct 1268 ms 143652 KB Output is correct
16 Correct 2086 ms 191696 KB Output is correct
17 Correct 2507 ms 151764 KB Output is correct
18 Correct 1471 ms 99844 KB Output is correct
19 Correct 892 ms 97160 KB Output is correct
20 Correct 1308 ms 106260 KB Output is correct
21 Correct 1450 ms 100476 KB Output is correct
22 Correct 2875 ms 157868 KB Output is correct
23 Correct 2739 ms 157784 KB Output is correct
24 Correct 3143 ms 163532 KB Output is correct
25 Correct 3346 ms 165608 KB Output is correct
26 Correct 3463 ms 158028 KB Output is correct
27 Correct 2850 ms 194100 KB Output is correct
28 Correct 2103 ms 152708 KB Output is correct
29 Correct 3518 ms 156752 KB Output is correct
30 Correct 3446 ms 155904 KB Output is correct
31 Correct 3632 ms 156324 KB Output is correct
32 Correct 1215 ms 105696 KB Output is correct
33 Correct 1047 ms 98020 KB Output is correct
34 Correct 1602 ms 94124 KB Output is correct
35 Correct 1386 ms 92352 KB Output is correct
36 Correct 1784 ms 94968 KB Output is correct
37 Correct 1648 ms 96464 KB Output is correct