답안 #982647

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
982647 2024-05-14T14:56:36 Z dimashhh 자매 도시 (APIO20_swap) C++17
6 / 100
209 ms 54124 KB
#include "swap.h"

#include <vector>
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5+12;

vector<pair<int,int>> g[N];
vector<int> gr[N],st[N];
int n,m,p[N],timer=0,t[N],e1[N],e2[N],first_time[N];
bool is[N];
int get(int v){
    if(p[v] == v) return v;
    return p[v] = get(p[v]);
}
int n1,tt[N],P[N];
void merge(int a,int b){
    int vv,uu;
    vv = a;
    uu = b;
    a = get(a);
    b = get(b);
    if(a == b){
        if(!is[a]){
            for(int j:st[a]){
                first_time[j]=timer;
            }
            st[a].clear();
            is[a]=1;
        }
        timer++;
        return;
    }
    if((int)st[a].size() > (int)st[b].size()){
        swap(vv,uu);
        swap(a, b);
    }
    n1++;
    tt[n1] = timer;
//    cout << n1 << ' ' << P[a] << ' ' << P[b] << "x\n";
    gr[n1].push_back(P[a]);
    gr[n1].push_back(P[b]);
    P[P[a]] = n1;
    P[P[b]] = n1;
    p[a] = b;
    for(int j:st[a]){
        st[b].push_back(j);
    }
    st[a].clear();
    if(is[b] || is[a] || (vv != e1[a] && vv != e2[a]) || (uu != e1[b] && uu != e2[b])) {
        for(int j:st[b]){
            first_time[j]=timer;
        }
        st[b].clear();
    }else{
        if(uu == e1[b]){
            e1[b] = (vv == e1[a] ? e2[a] : e1[a]);
        }else{
            e2[b] = (vv == e1[a] ? e2[a] : e1[a]);
        }
    }
    timer++;
}
int B = 20;
int up[N][20],tin[N],tout[N],tv;
void dfs(int v,int pr){
    up[v][0] = pr;
    tin[v] = tv++;
    for(int i = 1;i < B;i++){
        up[v][i] = up[up[v][i - 1]][i - 1];
    }
    for(int to:gr[v]){
        dfs(to,v);
    }
    tout[v] = tv++;
}
bool is_pr(int a,int b){
    return (tin[a] <= tin[b] && tout[a] >= tout[b]);
}
int lca(int v,int u){
    if(is_pr(v,u)) return v;
    if(is_pr(u,v)) return u;
    for(int i = B - 1;i >= 0;i--){
        if(!is_pr(up[v][i],u)){
            v = up[v][i];
        }
    }
    return up[v][0];
}
vector <array<int, 3>> e;
void init(int nn, int mm,std::vector<int> u, std::vector<int> v, std::vector<int> w) {
    memset(first_time,-1,sizeof(first_time));
    n1 = n = nn;
    m = mm;
    for (int i = 1; i <= m; i++) {
        e.push_back({w[i - 1], u[i - 1] + 1, v[i - 1] + 1});
    }
    sort(e.begin(), e.end());
    for(int i = 1;i <= n;i++) {
        P[i] =e1[i] = e2[i] = p[i] = i;
        st[i].push_back(i);
    }
    for(auto [w,a,b]:e){
        merge(a,b);
    }
    dfs(n1,n1);
}
int getMinimumFuelCapacity(int X, int Y) {
    X++;Y++;
    int f = max(first_time[X],max(first_time[Y],tt[lca(X,Y)]));
    if(first_time[X] == -1 || first_time[Y] == -1) return -1;
    return e[f][0];
}
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 22872 KB Output is correct
2 Correct 5 ms 22876 KB Output is correct
3 Correct 5 ms 22876 KB Output is correct
4 Correct 5 ms 22876 KB Output is correct
5 Correct 5 ms 22876 KB Output is correct
6 Correct 5 ms 22876 KB Output is correct
7 Correct 5 ms 22872 KB Output is correct
8 Correct 5 ms 22876 KB Output is correct
9 Correct 50 ms 43636 KB Output is correct
10 Correct 61 ms 43464 KB Output is correct
11 Correct 62 ms 45544 KB Output is correct
12 Correct 67 ms 42196 KB Output is correct
13 Correct 52 ms 39580 KB Output is correct
14 Correct 53 ms 43816 KB Output is correct
15 Correct 103 ms 43856 KB Output is correct
16 Correct 101 ms 47256 KB Output is correct
17 Correct 112 ms 41956 KB Output is correct
18 Correct 97 ms 41340 KB Output is correct
19 Correct 50 ms 30228 KB Output is correct
20 Correct 131 ms 46916 KB Output is correct
21 Correct 107 ms 48716 KB Output is correct
22 Correct 112 ms 45612 KB Output is correct
23 Correct 98 ms 42664 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 22872 KB Output is correct
2 Correct 5 ms 22876 KB Output is correct
3 Incorrect 209 ms 54124 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 22872 KB Output is correct
2 Correct 5 ms 22876 KB Output is correct
3 Correct 5 ms 22876 KB Output is correct
4 Correct 5 ms 22876 KB Output is correct
5 Correct 5 ms 22876 KB Output is correct
6 Correct 5 ms 22876 KB Output is correct
7 Correct 5 ms 22872 KB Output is correct
8 Correct 5 ms 22876 KB Output is correct
9 Correct 5 ms 22872 KB Output is correct
10 Incorrect 6 ms 22876 KB Output isn't correct
11 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 22872 KB Output is correct
2 Correct 5 ms 22872 KB Output is correct
3 Correct 5 ms 22876 KB Output is correct
4 Correct 5 ms 22876 KB Output is correct
5 Correct 5 ms 22876 KB Output is correct
6 Correct 5 ms 22876 KB Output is correct
7 Correct 5 ms 22876 KB Output is correct
8 Correct 5 ms 22872 KB Output is correct
9 Correct 5 ms 22876 KB Output is correct
10 Correct 50 ms 43636 KB Output is correct
11 Correct 61 ms 43464 KB Output is correct
12 Correct 62 ms 45544 KB Output is correct
13 Correct 67 ms 42196 KB Output is correct
14 Correct 52 ms 39580 KB Output is correct
15 Incorrect 6 ms 22876 KB Output isn't correct
16 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 22872 KB Output is correct
2 Correct 5 ms 22876 KB Output is correct
3 Correct 5 ms 22876 KB Output is correct
4 Correct 5 ms 22876 KB Output is correct
5 Correct 5 ms 22876 KB Output is correct
6 Correct 5 ms 22876 KB Output is correct
7 Correct 5 ms 22872 KB Output is correct
8 Correct 5 ms 22876 KB Output is correct
9 Correct 50 ms 43636 KB Output is correct
10 Correct 61 ms 43464 KB Output is correct
11 Correct 62 ms 45544 KB Output is correct
12 Correct 67 ms 42196 KB Output is correct
13 Correct 52 ms 39580 KB Output is correct
14 Correct 53 ms 43816 KB Output is correct
15 Correct 103 ms 43856 KB Output is correct
16 Correct 101 ms 47256 KB Output is correct
17 Correct 112 ms 41956 KB Output is correct
18 Correct 97 ms 41340 KB Output is correct
19 Incorrect 209 ms 54124 KB Output isn't correct
20 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 22872 KB Output is correct
2 Correct 5 ms 22872 KB Output is correct
3 Correct 5 ms 22876 KB Output is correct
4 Correct 5 ms 22876 KB Output is correct
5 Correct 5 ms 22876 KB Output is correct
6 Correct 5 ms 22876 KB Output is correct
7 Correct 5 ms 22876 KB Output is correct
8 Correct 5 ms 22872 KB Output is correct
9 Correct 5 ms 22876 KB Output is correct
10 Correct 50 ms 43636 KB Output is correct
11 Correct 61 ms 43464 KB Output is correct
12 Correct 62 ms 45544 KB Output is correct
13 Correct 67 ms 42196 KB Output is correct
14 Correct 52 ms 39580 KB Output is correct
15 Correct 53 ms 43816 KB Output is correct
16 Correct 103 ms 43856 KB Output is correct
17 Correct 101 ms 47256 KB Output is correct
18 Correct 112 ms 41956 KB Output is correct
19 Correct 97 ms 41340 KB Output is correct
20 Incorrect 209 ms 54124 KB Output isn't correct
21 Halted 0 ms 0 KB -