Submission #751860

# Submission time Handle Problem Language Result Execution time Memory
751860 2023-06-01T16:50:38 Z snpmrnhlol Swapping Cities (APIO20_swap) C++17
13 / 100
407 ms 40228 KB
#include "swap.h"
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5;
const int M = 2e5;
const int inf = 2e9;
int id[M],deg[N];
struct nodedata{
    int parent,active;
};
vector <nodedata> nodes;///parent,active
vector <int> e[2*N - 1];
int p2[2*N - 1][20];
int dpth[2*N - 1];
struct dsu{
    int id,parent,active,sz;
};
dsu v[N];
int leader(int node){
    if(node == v[node].parent)return node;
    int l = leader(v[node].parent);
    v[node].parent = l;
    return v[node].parent;
}
void connect(int a,int b,int w){
    deg[a]++;deg[b]++;
    int active = inf;
    if(deg[a] >= 3 || deg[b] >= 3)active = min(active,w);
    a = leader(a);b = leader(b);
    if(a == b){
        nodes[v[a].id].active = min(nodes[v[a].id].active,w);
        v[a].active = min(v[a].active,w);
    }else{
        nodes[v[a].id].parent = nodes.size();
        nodes[v[b].id].parent = nodes.size();
        if(v[a].sz < v[b].sz)swap(a,b);
        v[b].parent = a;
        v[a].sz+=v[b].sz;
        v[a].id = nodes.size();
        nodes.push_back({-1,active});
        v[a].active = min(v[b].active,v[a].active);
    }
}
void dfs(int node,int p = -1,int d = 0){
    dpth[node] = d;
    p2[node][0] = p;
    for(auto i:e[node]){
        if(i == p)continue;
        dfs(i,node,d + 1);
    }
}
void init(int n, int m,vector<int> u,vector<int> u2,vector<int> w) {
    int i,j;
    for(i = 0;i < m;i++)id[i] = i;
    for(i = 0;i < n;i++){
        nodes.push_back({-1,inf});
        v[i] = {i,i,inf,1};
    }
    sort(id,id + m,[&](int a,int b){
        return w[a] < w[b];
    });
    for(i = 0;i < m;i++){
        ///connect u[id[i]] with v[id[i]]
        connect(u[id[i]],u2[id[i]],w[id[i]]);
        //cout<<u[id[i]]<<' '<<u2[id[i]]<<'\n';
    }
    int cnt = 0;
    for(auto i:nodes){
        if(i.parent != -1){
            e[i.parent].push_back(cnt);
        }
        cnt++;
    }
    dfs(nodes.size() - 1,-1);
    for(j = 1;j < 20;j++){
        for(i = 0;i < 2*n - 1;i++){
            if(p2[i][j - 1] == -1)p2[i][j] = -1;
            else p2[i][j] = p2[p2[i][j - 1]][j - 1];
        }
    }
    /*for(i = 0;i < 2*n - 1;i++){
        for(j = 0;j < 20;j++)cout<<p2[i][j]<<' ';
        cout<<'\n';
    }
    for(i = 0;i < 2*n - 1;i++){
        cout<<p2[i][0]<<' '<<dpth[i]<<' '<<nodes[i].active<<' '<<i<<'\n';
    }*/
}

int getMinimumFuelCapacity(int x, int y) {
    if(dpth[x] < dpth[y])swap(x,y);
    for(int i = 19;i >= 0;i--){
        if(dpth[x] - (1<<i) >= dpth[y]){
            x = p2[x][i];
        }
    }
    for(int i = 19;i >= 0;i--){
        if(p2[x][i] != p2[y][i]){
            x = p2[x][i];y = p2[y][i];
        }
    }
    if(x != y)x = p2[x][0];
    //cout<<x<<'\n';
    for(int i = 19;i >= 0;i--){
        //cout<<x<<' '<<p2[x][i]<<'\n';
        if(p2[x][i] != -1 && nodes[p2[x][i]].active == inf){
            x = p2[x][i];
        }
    }
    if(nodes[x].active == inf){
        x = p2[x][0];
    }
    return (x==-1?-1:nodes[x].active);
}
/**
5 6
0 1 4
0 2 4
1 2 1
1 3 2
1 4 10
2 3 3
3
1 2
2 4
0 1

3 2
0 1 5
0 2 5
1
1 2
**/
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 5076 KB Output is correct
5 Correct 3 ms 5280 KB Output is correct
6 Correct 4 ms 5156 KB Output is correct
7 Correct 4 ms 5204 KB Output is correct
8 Correct 3 ms 5204 KB Output is correct
9 Correct 103 ms 26968 KB Output is correct
10 Correct 114 ms 31804 KB Output is correct
11 Correct 112 ms 31412 KB Output is correct
12 Correct 137 ms 33044 KB Output is correct
13 Correct 124 ms 34360 KB Output is correct
14 Correct 116 ms 27152 KB Output is correct
15 Correct 293 ms 36064 KB Output is correct
16 Correct 226 ms 35312 KB Output is correct
17 Correct 250 ms 37028 KB Output is correct
18 Correct 334 ms 38348 KB Output is correct
19 Correct 93 ms 14536 KB Output is correct
20 Correct 246 ms 37240 KB Output is correct
21 Correct 300 ms 36596 KB Output is correct
22 Correct 293 ms 38468 KB Output is correct
23 Correct 361 ms 39756 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 382 ms 38856 KB Output is correct
4 Correct 367 ms 40228 KB Output is correct
5 Correct 357 ms 39636 KB Output is correct
6 Correct 359 ms 40060 KB Output is correct
7 Correct 336 ms 40024 KB Output is correct
8 Correct 354 ms 38748 KB Output is correct
9 Correct 359 ms 39628 KB Output is correct
10 Correct 407 ms 38508 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 5076 KB Output is correct
5 Correct 3 ms 5280 KB Output is correct
6 Correct 4 ms 5156 KB Output is correct
7 Correct 4 ms 5204 KB Output is correct
8 Correct 3 ms 5204 KB Output is correct
9 Correct 4 ms 4948 KB Output is correct
10 Incorrect 4 ms 5204 KB Output isn't correct
11 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 3 ms 5076 KB Output is correct
6 Correct 3 ms 5280 KB Output is correct
7 Correct 4 ms 5156 KB Output is correct
8 Correct 4 ms 5204 KB Output is correct
9 Correct 3 ms 5204 KB Output is correct
10 Correct 103 ms 26968 KB Output is correct
11 Correct 114 ms 31804 KB Output is correct
12 Correct 112 ms 31412 KB Output is correct
13 Correct 137 ms 33044 KB Output is correct
14 Correct 124 ms 34360 KB Output is correct
15 Incorrect 4 ms 5204 KB Output isn't correct
16 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 5076 KB Output is correct
5 Correct 3 ms 5280 KB Output is correct
6 Correct 4 ms 5156 KB Output is correct
7 Correct 4 ms 5204 KB Output is correct
8 Correct 3 ms 5204 KB Output is correct
9 Correct 103 ms 26968 KB Output is correct
10 Correct 114 ms 31804 KB Output is correct
11 Correct 112 ms 31412 KB Output is correct
12 Correct 137 ms 33044 KB Output is correct
13 Correct 124 ms 34360 KB Output is correct
14 Correct 116 ms 27152 KB Output is correct
15 Correct 293 ms 36064 KB Output is correct
16 Correct 226 ms 35312 KB Output is correct
17 Correct 250 ms 37028 KB Output is correct
18 Correct 334 ms 38348 KB Output is correct
19 Correct 382 ms 38856 KB Output is correct
20 Correct 367 ms 40228 KB Output is correct
21 Correct 357 ms 39636 KB Output is correct
22 Correct 359 ms 40060 KB Output is correct
23 Correct 336 ms 40024 KB Output is correct
24 Correct 354 ms 38748 KB Output is correct
25 Correct 359 ms 39628 KB Output is correct
26 Correct 407 ms 38508 KB Output is correct
27 Incorrect 4 ms 5204 KB Output isn't correct
28 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 3 ms 5076 KB Output is correct
6 Correct 3 ms 5280 KB Output is correct
7 Correct 4 ms 5156 KB Output is correct
8 Correct 4 ms 5204 KB Output is correct
9 Correct 3 ms 5204 KB Output is correct
10 Correct 103 ms 26968 KB Output is correct
11 Correct 114 ms 31804 KB Output is correct
12 Correct 112 ms 31412 KB Output is correct
13 Correct 137 ms 33044 KB Output is correct
14 Correct 124 ms 34360 KB Output is correct
15 Correct 116 ms 27152 KB Output is correct
16 Correct 293 ms 36064 KB Output is correct
17 Correct 226 ms 35312 KB Output is correct
18 Correct 250 ms 37028 KB Output is correct
19 Correct 334 ms 38348 KB Output is correct
20 Correct 382 ms 38856 KB Output is correct
21 Correct 367 ms 40228 KB Output is correct
22 Correct 357 ms 39636 KB Output is correct
23 Correct 359 ms 40060 KB Output is correct
24 Correct 336 ms 40024 KB Output is correct
25 Correct 354 ms 38748 KB Output is correct
26 Correct 359 ms 39628 KB Output is correct
27 Correct 407 ms 38508 KB Output is correct
28 Incorrect 4 ms 5204 KB Output isn't correct
29 Halted 0 ms 0 KB -