Submission #299138

# Submission time Handle Problem Language Result Execution time Memory
299138 2020-09-14T13:56:23 Z Hehehe Min-max tree (BOI18_minmaxtree) C++14
100 / 100
367 ms 177860 KB
#include<bits/stdc++.h> //:3
using namespace std;
typedef long long ll;
#define all(a) (a).begin(), (a).end()
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define pi pair<int, int>
#define sz(x) (int)((x).size())
#define int long long

const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};

const ll inf = 2e9;
const ll mod = 1e9 + 7;
const int N = 2e6 + 11;
const int X = 1e6;
const ll INF64 = 3e18 + 1;
const double eps = 1e-14;
const double PI = acos(-1);

//ifstream in(".in");
//ofstream out(".out");

int depth[N], F[N], l[N], r[N], n, m, k;
int E[N*4], lg[N*4], first[N], viz[N], P[N], dp_min[N], dp_max[N];
vector<int> v[N], g[N];
map<int, int> poz;
pi rmq[30][N];

struct yes{
    int tip, x, y, val;
};

yes q[N], w[N];

void dfs(int nod, int p){
    E[++k] = nod;
    first[nod] = k;
    depth[nod] = depth[p] + 1;
    F[nod] = P[nod] = p;
    for(auto it : v[nod]){
        if(it == p)continue;
        dfs(it, nod);
        E[++k] = nod;
    }
}

int get_lca(int x, int y){
    int X = first[x], Y = first[y];
    if(X > Y)swap(X, Y);

    int lvl = lg[Y - X + 1];
    pi ans = min(rmq[lvl][X], rmq[lvl][Y - (1 << lvl) + 1]);
    return E[ans.ss];
}

int cpl(int nod){
    if(viz[nod])return 0;

    viz[nod] = 1;
    for(auto it : g[nod]){
        if(r[it] == 0 || cpl(r[it])){
            l[nod] = it;
            r[it] = nod;
            return 1;
        }
    }

    return 0;
}

void solve(){

    cin >> n;

    for(int i = 1, x, y; i < n; i++){
        cin >> x >> y;
        v[x].push_back(y);
        v[y].push_back(x);
    }

    dfs(1, 0);

    for(int i = 1; i <= k; i++){
        rmq[0][i] = mp(depth[E[i]], i);
    }

    for(int i = 1; (1 << i) <= k; i++){
        for(int j = 1; j <= k; j++){
            rmq[i][j] = rmq[i - 1][j];
            if (j + (1 << (i - 1)) <= k && rmq[i - 1][j + (1 << (i - 1))].ff < rmq[i][j].ff)
                rmq[i][j] = rmq[i - 1][j + (1 << (i - 1))];
        }
    }

    for(int i = 2; i <= k; i++){
        lg[i] = lg[i / 2] + 1;
    }

    cin >> m;
    int el = 0;
    for(int i = 1, x, y, val; i <= m; i++){
        char c;

        cin >> c >> x >> y >> val;

        int type = (c == 'M' ? 1 : 0);

        q[i] = {type, x, y, val};

        poz[val] = i;

        if(type)w[++el] = q[i];
    }

    sort(w + 1, w + el + 1, [](yes L, yes R){
         return L.val < R.val;
    });
    for(int i = 1; i <= el; i++){
        int x = w[i].x, y = w[i].y, val = w[i].val;

        int LCA = get_lca(x, y);

        for(int I : {x, y}){
            int nod = I;
            while (nod && depth[nod] > depth[LCA]){

                if(!dp_min[nod])dp_min[nod] = val;

                int aux = nod;
                nod = F[nod];
                F[aux] = LCA;
            }
        }
    }

    for(int i = 1; i <= n; i++)F[i] = P[i];

    el = 0;
    for(int i = 1; i <= n; i++){
        if(q[i].tip == 0)w[++el] = q[i];
    }

    sort(w + 1, w + el + 1, [](yes L, yes R){
         return L.val < R.val;
    });
    for(int i = el; i >= 1; i--){
        int x = w[i].x, y = w[i].y, val = w[i].val;

        int LCA = get_lca(x, y);

        for(int I : {x, y}){
            int nod = I;
            while (nod && depth[nod] > depth[LCA]){

                if(!dp_max[nod])dp_max[nod] = val;

                int aux = nod;
                nod = F[nod];
                F[aux] = LCA;
            }
        }
    }

    for(int i = 1; i <= n; i++){
        if(dp_max[i]){
            int idx = poz[dp_max[i]];

            g[i].push_back(n + idx);
            g[n + idx].push_back (i);
        }

        if(dp_min[i]){
            int idx = poz[dp_min[i]];

            g[i].push_back(n + idx);
            g[n + idx].push_back(i);
        }
    }

    int ok = 1;

    while(ok){
        memset(viz, 0, sizeof(viz));
        ok = 0;
        for(int i = 1; i <= n; i++){
            if(!l[i] && cpl(i))ok = 1;
        }
    }

    for(int i = 2; i <= n; i++){
        cout << i << ' ' << P[i] << ' ';
        int idx = l[i];
        cout << (idx ? q[idx - n].val : max(dp_min[i],dp_max[i])) << '\n';
    }
}

int32_t main(){
ios_base :: sync_with_stdio(0); cin.tie(0); cout.tie(0);

    //cout << setprecision(6) << fixed;

    int T = 1;
    //cin >> T;
    while(T--){
        solve();
    }
}
# Verdict Execution time Memory Grader output
1 Correct 75 ms 110072 KB Output is correct
2 Correct 76 ms 110072 KB Output is correct
3 Correct 70 ms 110076 KB Output is correct
4 Correct 72 ms 110072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 289 ms 176896 KB Output is correct
2 Correct 281 ms 173560 KB Output is correct
3 Correct 269 ms 173176 KB Output is correct
4 Correct 315 ms 177272 KB Output is correct
5 Correct 278 ms 173816 KB Output is correct
6 Correct 290 ms 175364 KB Output is correct
7 Correct 274 ms 173944 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 210 ms 166648 KB Output is correct
2 Correct 211 ms 167288 KB Output is correct
3 Correct 210 ms 167544 KB Output is correct
4 Correct 207 ms 168440 KB Output is correct
5 Correct 224 ms 167800 KB Output is correct
6 Correct 230 ms 168824 KB Output is correct
7 Correct 248 ms 168424 KB Output is correct
8 Correct 231 ms 168184 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 75 ms 110072 KB Output is correct
2 Correct 76 ms 110072 KB Output is correct
3 Correct 70 ms 110076 KB Output is correct
4 Correct 72 ms 110072 KB Output is correct
5 Correct 289 ms 176896 KB Output is correct
6 Correct 281 ms 173560 KB Output is correct
7 Correct 269 ms 173176 KB Output is correct
8 Correct 315 ms 177272 KB Output is correct
9 Correct 278 ms 173816 KB Output is correct
10 Correct 290 ms 175364 KB Output is correct
11 Correct 274 ms 173944 KB Output is correct
12 Correct 210 ms 166648 KB Output is correct
13 Correct 211 ms 167288 KB Output is correct
14 Correct 210 ms 167544 KB Output is correct
15 Correct 207 ms 168440 KB Output is correct
16 Correct 224 ms 167800 KB Output is correct
17 Correct 230 ms 168824 KB Output is correct
18 Correct 248 ms 168424 KB Output is correct
19 Correct 231 ms 168184 KB Output is correct
20 Correct 304 ms 173816 KB Output is correct
21 Correct 275 ms 171512 KB Output is correct
22 Correct 277 ms 172152 KB Output is correct
23 Correct 286 ms 172792 KB Output is correct
24 Correct 367 ms 176376 KB Output is correct
25 Correct 345 ms 177860 KB Output is correct
26 Correct 321 ms 176760 KB Output is correct
27 Correct 338 ms 175736 KB Output is correct
28 Correct 342 ms 175228 KB Output is correct
29 Correct 329 ms 175224 KB Output is correct
30 Correct 310 ms 172664 KB Output is correct
31 Correct 314 ms 173304 KB Output is correct
32 Correct 336 ms 174840 KB Output is correct
33 Correct 319 ms 174072 KB Output is correct
34 Correct 311 ms 173944 KB Output is correct
35 Correct 320 ms 173160 KB Output is correct