답안 #982649

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
982649 2024-05-14T15:00:44 Z dimashhh 자매 도시 (APIO20_swap) C++17
37 / 100
2000 ms 52464 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])) {
        is[b]=1;
        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);
}
void merge1(int a,int b){
    a = get(a);
    b = get(b);
    p[a] = b;
}
int getMinimumFuelCapacity(int X, int Y) {
    X++;Y++;
    int f = max(first_time[X],first_time[Y]);
    if(first_time[X] == -1 || first_time[Y] == -1) return -1;
    for(int i = 1;i <= n;i++){
        p[i] = i;
    }
    for(int j = 0;j < m;j++){
        int vv = e[j][2],uu = e[j][1];
        merge1(vv,uu);
        if(get(X) == get(Y)){
            f = max(f,j);
            break;
        }
    }
    return e[f][0];
}
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 22876 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 6 ms 22876 KB Output is correct
7 Correct 7 ms 22876 KB Output is correct
8 Correct 5 ms 22876 KB Output is correct
9 Correct 50 ms 43720 KB Output is correct
10 Correct 63 ms 43612 KB Output is correct
11 Correct 63 ms 45512 KB Output is correct
12 Correct 60 ms 42184 KB Output is correct
13 Correct 53 ms 39528 KB Output is correct
14 Correct 54 ms 43596 KB Output is correct
15 Correct 105 ms 43860 KB Output is correct
16 Correct 100 ms 47252 KB Output is correct
17 Correct 106 ms 42004 KB Output is correct
18 Correct 97 ms 41440 KB Output is correct
19 Execution timed out 2060 ms 28692 KB Time limit exceeded
20 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 22876 KB Output is correct
2 Correct 5 ms 22876 KB Output is correct
3 Execution timed out 2053 ms 52464 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 22876 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 6 ms 22876 KB Output is correct
7 Correct 7 ms 22876 KB Output is correct
8 Correct 5 ms 22876 KB Output is correct
9 Correct 5 ms 22876 KB Output is correct
10 Correct 6 ms 22876 KB Output is correct
11 Correct 7 ms 22876 KB Output is correct
12 Correct 5 ms 22876 KB Output is correct
13 Correct 5 ms 22872 KB Output is correct
14 Correct 5 ms 22876 KB Output is correct
15 Correct 5 ms 22872 KB Output is correct
16 Correct 5 ms 22872 KB Output is correct
17 Correct 5 ms 22872 KB Output is correct
18 Correct 5 ms 22876 KB Output is correct
19 Correct 6 ms 22876 KB Output is correct
20 Correct 5 ms 22872 KB Output is correct
21 Correct 5 ms 22900 KB Output is correct
22 Correct 5 ms 22876 KB Output is correct
23 Correct 5 ms 22876 KB Output is correct
24 Correct 6 ms 22876 KB Output is correct
25 Correct 6 ms 22876 KB Output is correct
26 Correct 6 ms 22876 KB Output is correct
27 Correct 5 ms 22872 KB Output is correct
28 Correct 6 ms 22876 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 22876 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 6 ms 22876 KB Output is correct
8 Correct 7 ms 22876 KB Output is correct
9 Correct 5 ms 22876 KB Output is correct
10 Correct 50 ms 43720 KB Output is correct
11 Correct 63 ms 43612 KB Output is correct
12 Correct 63 ms 45512 KB Output is correct
13 Correct 60 ms 42184 KB Output is correct
14 Correct 53 ms 39528 KB Output is correct
15 Correct 6 ms 22876 KB Output is correct
16 Correct 7 ms 22876 KB Output is correct
17 Correct 5 ms 22876 KB Output is correct
18 Correct 5 ms 22872 KB Output is correct
19 Correct 5 ms 22876 KB Output is correct
20 Correct 5 ms 22872 KB Output is correct
21 Correct 5 ms 22872 KB Output is correct
22 Correct 5 ms 22872 KB Output is correct
23 Correct 5 ms 22876 KB Output is correct
24 Correct 6 ms 22876 KB Output is correct
25 Correct 5 ms 22872 KB Output is correct
26 Correct 5 ms 22900 KB Output is correct
27 Correct 5 ms 22876 KB Output is correct
28 Correct 5 ms 22876 KB Output is correct
29 Correct 6 ms 22876 KB Output is correct
30 Correct 6 ms 22876 KB Output is correct
31 Correct 6 ms 22876 KB Output is correct
32 Correct 5 ms 22872 KB Output is correct
33 Correct 6 ms 22876 KB Output is correct
34 Correct 12 ms 26268 KB Output is correct
35 Correct 67 ms 45216 KB Output is correct
36 Correct 66 ms 38764 KB Output is correct
37 Correct 75 ms 39876 KB Output is correct
38 Correct 65 ms 41464 KB Output is correct
39 Correct 63 ms 40904 KB Output is correct
40 Correct 60 ms 41924 KB Output is correct
41 Correct 68 ms 39756 KB Output is correct
42 Correct 67 ms 42440 KB Output is correct
43 Correct 67 ms 41148 KB Output is correct
44 Correct 69 ms 45048 KB Output is correct
45 Correct 83 ms 43712 KB Output is correct
46 Correct 71 ms 44452 KB Output is correct
47 Correct 66 ms 40900 KB Output is correct
48 Correct 72 ms 42700 KB Output is correct
49 Correct 58 ms 31936 KB Output is correct
50 Correct 47 ms 31684 KB Output is correct
51 Correct 70 ms 39996 KB Output is correct
52 Correct 95 ms 46316 KB Output is correct
53 Correct 90 ms 46732 KB Output is correct
54 Correct 106 ms 48576 KB Output is correct
55 Correct 59 ms 42436 KB Output is correct
56 Correct 117 ms 46640 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 22876 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 6 ms 22876 KB Output is correct
7 Correct 7 ms 22876 KB Output is correct
8 Correct 5 ms 22876 KB Output is correct
9 Correct 50 ms 43720 KB Output is correct
10 Correct 63 ms 43612 KB Output is correct
11 Correct 63 ms 45512 KB Output is correct
12 Correct 60 ms 42184 KB Output is correct
13 Correct 53 ms 39528 KB Output is correct
14 Correct 54 ms 43596 KB Output is correct
15 Correct 105 ms 43860 KB Output is correct
16 Correct 100 ms 47252 KB Output is correct
17 Correct 106 ms 42004 KB Output is correct
18 Correct 97 ms 41440 KB Output is correct
19 Execution timed out 2053 ms 52464 KB Time limit exceeded
20 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 22876 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 6 ms 22876 KB Output is correct
8 Correct 7 ms 22876 KB Output is correct
9 Correct 5 ms 22876 KB Output is correct
10 Correct 50 ms 43720 KB Output is correct
11 Correct 63 ms 43612 KB Output is correct
12 Correct 63 ms 45512 KB Output is correct
13 Correct 60 ms 42184 KB Output is correct
14 Correct 53 ms 39528 KB Output is correct
15 Correct 54 ms 43596 KB Output is correct
16 Correct 105 ms 43860 KB Output is correct
17 Correct 100 ms 47252 KB Output is correct
18 Correct 106 ms 42004 KB Output is correct
19 Correct 97 ms 41440 KB Output is correct
20 Execution timed out 2053 ms 52464 KB Time limit exceeded
21 Halted 0 ms 0 KB -