답안 #651485

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
651485 2022-10-19T01:29:38 Z perchuts 자매 도시 (APIO20_swap) C++17
100 / 100
411 ms 40208 KB
#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define sz(x) (int) x.size()
#define endl '\n'
#define pb push_back
#define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

using namespace std;

using ll = long long;
using ull = unsigned long long;
using ii = pair<int,int>;
using iii = tuple<int,int,int>;

const int inf = 2e9+1;
const int mod = 1e9+7;
const int maxn = 1e5+100;

template<typename X, typename Y> bool ckmin(X& x, const Y& y) { return (y < x) ? (x=y,1):0; }
template<typename X, typename Y> bool ckmax(X& x, const Y& y) { return (x < y) ? (x=y,1):0; }

vector<tuple<int,int,int>>edges;

int lvl[maxn], par[maxn], when[maxn];

ii pontas[maxn];

vector<int>comp[maxn];
vector<ii>parents[maxn];

int findp(int x){return par[x] = (par[x]==x?x:findp(par[x]));}

void merge(int w,int u,int v){
    int pu = findp(u), pv = findp(v), ciclo = 0;
    if(lvl[pu]<lvl[pv])swap(pu,pv), swap(u,v);
    if(pu==pv)ciclo = 1;
    //check if someone's component turned into a non-path component
    par[pv] = pu;  
    if(when[pu]==inf&&when[pv]!=inf){
        for(auto x:comp[pu])when[x] = w;
    }else if(when[pu]!=inf&&when[pv]==inf){
        for(auto x:comp[pv])when[x] = w;
    }else if((ciclo||(u!=pontas[pu].first&&u!=pontas[pu].second)||
    (v!=pontas[pv].first&&v!=pontas[pv].second))&&when[pu]==inf&&
    when[pv]==inf){
        for(auto x:comp[pu])when[x] = w;
        if(!ciclo)for(auto x:comp[pv])when[x] = w;
    }
    if(ciclo)return;
    for(auto x:comp[pv]){
        comp[pu].pb(x);
        parents[x].pb({w,pu});
    }
    if(when[pu]==when[pv]&&when[pu]==inf){
        if(pontas[pu].first==u)swap(pontas[pu].first,pontas[pu].second);
        if(pontas[pv].first!=v)pontas[pu].second = pontas[pv].first;
        else pontas[pu].second = pontas[pv].second;
    }
    if(lvl[pu]==lvl[pv])++lvl[pu];
}

void init(int N, int M,vector<int> U, vector<int> V, vector<int> W) {

    for(int i=0;i<M;++i){
        edges.pb({W[i], U[i], V[i]});
    }

    for(int i=0;i<N;++i){
        par[i] = i, pontas[i] = {i,i}, when[i] = inf;
        parents[i].pb({0,i}), comp[i].pb(i);
    }

    sort(all(edges));

    for(auto [w,u,v]:edges){
        merge(w,u,v);
    }
    
    for(int i=0;i<N;++i)reverse(all(parents[i]));
}

int getMinimumFuelCapacity(int X, int Y) {
    if(when[X]==inf)return -1;
    int last = 0;
    while(last!=min(sz(parents[X]), sz(parents[Y]))-1){
        if(parents[X][last]!=parents[Y][last])break;
        ++last;
    }
    if(parents[X][last].second!=parents[Y][last].second)--last;//corner fudido
    auto [custo,pai] = parents[X][last];
    ckmax(custo, parents[Y][last].first);
    return max(custo, min(when[X], when[Y]));
}

// int main() {
//   int N, M; 
//   assert(2 == scanf("%d %d", &N, &M));
  
//   std::vector<int> U(M), V(M), W(M);
//   for (int i = 0; i < M; ++i) {
//     assert(3 == scanf("%d %d %d", &U[i], &V[i], &W[i]));
//   }

//   int Q;
//   assert(1 == scanf("%d", &Q));

//   std::vector<int> X(Q), Y(Q);
//   for (int i = 0; i < Q; ++i) {
//     assert(2 == scanf("%d %d", &X[i], &Y[i]));
//   }

//   init(N, M, U, V, W);
  
//   std::vector<int> minimum_fuel_capacities(Q);
//   for (int i = 0; i < Q; ++i) {
//     minimum_fuel_capacities[i] = getMinimumFuelCapacity(X[i], Y[i]);
//   }

//   for (int i = 0; i < Q; ++i) {
//     printf("%d\n", minimum_fuel_capacities[i]);
//   }
  
//   return 0;
// }
# 결과 실행 시간 메모리 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 5204 KB Output is correct
6 Correct 3 ms 5204 KB Output is correct
7 Correct 3 ms 5204 KB Output is correct
8 Correct 3 ms 5076 KB Output is correct
9 Correct 156 ms 23804 KB Output is correct
10 Correct 209 ms 27756 KB Output is correct
11 Correct 190 ms 26772 KB Output is correct
12 Correct 184 ms 28396 KB Output is correct
13 Correct 144 ms 21604 KB Output is correct
14 Correct 123 ms 23616 KB Output is correct
15 Correct 257 ms 30128 KB Output is correct
16 Correct 222 ms 29240 KB Output is correct
17 Correct 240 ms 30604 KB Output is correct
18 Correct 190 ms 23588 KB Output is correct
19 Correct 62 ms 11880 KB Output is correct
20 Correct 255 ms 30628 KB Output is correct
21 Correct 240 ms 30348 KB Output is correct
22 Correct 264 ms 31812 KB Output is correct
23 Correct 172 ms 22480 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 160 ms 19564 KB Output is correct
4 Correct 155 ms 19976 KB Output is correct
5 Correct 167 ms 20124 KB Output is correct
6 Correct 155 ms 19984 KB Output is correct
7 Correct 168 ms 20212 KB Output is correct
8 Correct 156 ms 19584 KB Output is correct
9 Correct 152 ms 19848 KB Output is correct
10 Correct 162 ms 19452 KB Output is correct
# 결과 실행 시간 메모리 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 5204 KB Output is correct
6 Correct 3 ms 5204 KB Output is correct
7 Correct 3 ms 5204 KB Output is correct
8 Correct 3 ms 5076 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 3 ms 5076 KB Output is correct
11 Correct 3 ms 5204 KB Output is correct
12 Correct 4 ms 5204 KB Output is correct
13 Correct 3 ms 5076 KB Output is correct
14 Correct 3 ms 5076 KB Output is correct
15 Correct 3 ms 5204 KB Output is correct
16 Correct 4 ms 5204 KB Output is correct
17 Correct 3 ms 5076 KB Output is correct
18 Correct 4 ms 5204 KB Output is correct
19 Correct 4 ms 5076 KB Output is correct
20 Correct 4 ms 5204 KB Output is correct
21 Correct 4 ms 5144 KB Output is correct
22 Correct 3 ms 5080 KB Output is correct
23 Correct 4 ms 5076 KB Output is correct
24 Correct 4 ms 5204 KB Output is correct
25 Correct 4 ms 5216 KB Output is correct
26 Correct 4 ms 5204 KB Output is correct
27 Correct 3 ms 5076 KB Output is correct
28 Correct 3 ms 5204 KB Output is correct
# 결과 실행 시간 메모리 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 4948 KB Output is correct
5 Correct 3 ms 5076 KB Output is correct
6 Correct 3 ms 5204 KB Output is correct
7 Correct 3 ms 5204 KB Output is correct
8 Correct 3 ms 5204 KB Output is correct
9 Correct 3 ms 5076 KB Output is correct
10 Correct 156 ms 23804 KB Output is correct
11 Correct 209 ms 27756 KB Output is correct
12 Correct 190 ms 26772 KB Output is correct
13 Correct 184 ms 28396 KB Output is correct
14 Correct 144 ms 21604 KB Output is correct
15 Correct 3 ms 5076 KB Output is correct
16 Correct 3 ms 5204 KB Output is correct
17 Correct 4 ms 5204 KB Output is correct
18 Correct 3 ms 5076 KB Output is correct
19 Correct 3 ms 5076 KB Output is correct
20 Correct 3 ms 5204 KB Output is correct
21 Correct 4 ms 5204 KB Output is correct
22 Correct 3 ms 5076 KB Output is correct
23 Correct 4 ms 5204 KB Output is correct
24 Correct 4 ms 5076 KB Output is correct
25 Correct 4 ms 5204 KB Output is correct
26 Correct 4 ms 5144 KB Output is correct
27 Correct 3 ms 5080 KB Output is correct
28 Correct 4 ms 5076 KB Output is correct
29 Correct 4 ms 5204 KB Output is correct
30 Correct 4 ms 5216 KB Output is correct
31 Correct 4 ms 5204 KB Output is correct
32 Correct 3 ms 5076 KB Output is correct
33 Correct 3 ms 5204 KB Output is correct
34 Correct 16 ms 7752 KB Output is correct
35 Correct 189 ms 28836 KB Output is correct
36 Correct 189 ms 28160 KB Output is correct
37 Correct 183 ms 27072 KB Output is correct
38 Correct 188 ms 26056 KB Output is correct
39 Correct 227 ms 26304 KB Output is correct
40 Correct 161 ms 23616 KB Output is correct
41 Correct 181 ms 28996 KB Output is correct
42 Correct 219 ms 29080 KB Output is correct
43 Correct 113 ms 20416 KB Output is correct
44 Correct 186 ms 26128 KB Output is correct
45 Correct 136 ms 19836 KB Output is correct
46 Correct 207 ms 28336 KB Output is correct
47 Correct 144 ms 23932 KB Output is correct
48 Correct 130 ms 20852 KB Output is correct
49 Correct 59 ms 11840 KB Output is correct
50 Correct 56 ms 10136 KB Output is correct
51 Correct 109 ms 17240 KB Output is correct
52 Correct 230 ms 28872 KB Output is correct
53 Correct 181 ms 24364 KB Output is correct
54 Correct 233 ms 31924 KB Output is correct
55 Correct 99 ms 19060 KB Output is correct
56 Correct 151 ms 23356 KB Output is correct
# 결과 실행 시간 메모리 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 5204 KB Output is correct
6 Correct 3 ms 5204 KB Output is correct
7 Correct 3 ms 5204 KB Output is correct
8 Correct 3 ms 5076 KB Output is correct
9 Correct 156 ms 23804 KB Output is correct
10 Correct 209 ms 27756 KB Output is correct
11 Correct 190 ms 26772 KB Output is correct
12 Correct 184 ms 28396 KB Output is correct
13 Correct 144 ms 21604 KB Output is correct
14 Correct 123 ms 23616 KB Output is correct
15 Correct 257 ms 30128 KB Output is correct
16 Correct 222 ms 29240 KB Output is correct
17 Correct 240 ms 30604 KB Output is correct
18 Correct 190 ms 23588 KB Output is correct
19 Correct 160 ms 19564 KB Output is correct
20 Correct 155 ms 19976 KB Output is correct
21 Correct 167 ms 20124 KB Output is correct
22 Correct 155 ms 19984 KB Output is correct
23 Correct 168 ms 20212 KB Output is correct
24 Correct 156 ms 19584 KB Output is correct
25 Correct 152 ms 19848 KB Output is correct
26 Correct 162 ms 19452 KB Output is correct
27 Correct 3 ms 5076 KB Output is correct
28 Correct 3 ms 5204 KB Output is correct
29 Correct 4 ms 5204 KB Output is correct
30 Correct 3 ms 5076 KB Output is correct
31 Correct 3 ms 5076 KB Output is correct
32 Correct 3 ms 5204 KB Output is correct
33 Correct 4 ms 5204 KB Output is correct
34 Correct 3 ms 5076 KB Output is correct
35 Correct 4 ms 5204 KB Output is correct
36 Correct 16 ms 7752 KB Output is correct
37 Correct 189 ms 28836 KB Output is correct
38 Correct 189 ms 28160 KB Output is correct
39 Correct 183 ms 27072 KB Output is correct
40 Correct 188 ms 26056 KB Output is correct
41 Correct 227 ms 26304 KB Output is correct
42 Correct 161 ms 23616 KB Output is correct
43 Correct 181 ms 28996 KB Output is correct
44 Correct 219 ms 29080 KB Output is correct
45 Correct 113 ms 20416 KB Output is correct
46 Correct 186 ms 26128 KB Output is correct
47 Correct 18 ms 7828 KB Output is correct
48 Correct 259 ms 30616 KB Output is correct
49 Correct 245 ms 31264 KB Output is correct
50 Correct 241 ms 29988 KB Output is correct
51 Correct 265 ms 29364 KB Output is correct
52 Correct 239 ms 31200 KB Output is correct
53 Correct 186 ms 25652 KB Output is correct
54 Correct 282 ms 35952 KB Output is correct
55 Correct 282 ms 35980 KB Output is correct
56 Correct 159 ms 26388 KB Output is correct
57 Correct 242 ms 32660 KB Output is correct
# 결과 실행 시간 메모리 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 4948 KB Output is correct
5 Correct 3 ms 5076 KB Output is correct
6 Correct 3 ms 5204 KB Output is correct
7 Correct 3 ms 5204 KB Output is correct
8 Correct 3 ms 5204 KB Output is correct
9 Correct 3 ms 5076 KB Output is correct
10 Correct 156 ms 23804 KB Output is correct
11 Correct 209 ms 27756 KB Output is correct
12 Correct 190 ms 26772 KB Output is correct
13 Correct 184 ms 28396 KB Output is correct
14 Correct 144 ms 21604 KB Output is correct
15 Correct 123 ms 23616 KB Output is correct
16 Correct 257 ms 30128 KB Output is correct
17 Correct 222 ms 29240 KB Output is correct
18 Correct 240 ms 30604 KB Output is correct
19 Correct 190 ms 23588 KB Output is correct
20 Correct 160 ms 19564 KB Output is correct
21 Correct 155 ms 19976 KB Output is correct
22 Correct 167 ms 20124 KB Output is correct
23 Correct 155 ms 19984 KB Output is correct
24 Correct 168 ms 20212 KB Output is correct
25 Correct 156 ms 19584 KB Output is correct
26 Correct 152 ms 19848 KB Output is correct
27 Correct 162 ms 19452 KB Output is correct
28 Correct 3 ms 5076 KB Output is correct
29 Correct 3 ms 5204 KB Output is correct
30 Correct 4 ms 5204 KB Output is correct
31 Correct 3 ms 5076 KB Output is correct
32 Correct 3 ms 5076 KB Output is correct
33 Correct 3 ms 5204 KB Output is correct
34 Correct 4 ms 5204 KB Output is correct
35 Correct 3 ms 5076 KB Output is correct
36 Correct 4 ms 5204 KB Output is correct
37 Correct 16 ms 7752 KB Output is correct
38 Correct 189 ms 28836 KB Output is correct
39 Correct 189 ms 28160 KB Output is correct
40 Correct 183 ms 27072 KB Output is correct
41 Correct 188 ms 26056 KB Output is correct
42 Correct 227 ms 26304 KB Output is correct
43 Correct 161 ms 23616 KB Output is correct
44 Correct 181 ms 28996 KB Output is correct
45 Correct 219 ms 29080 KB Output is correct
46 Correct 113 ms 20416 KB Output is correct
47 Correct 186 ms 26128 KB Output is correct
48 Correct 18 ms 7828 KB Output is correct
49 Correct 259 ms 30616 KB Output is correct
50 Correct 245 ms 31264 KB Output is correct
51 Correct 241 ms 29988 KB Output is correct
52 Correct 265 ms 29364 KB Output is correct
53 Correct 239 ms 31200 KB Output is correct
54 Correct 186 ms 25652 KB Output is correct
55 Correct 282 ms 35952 KB Output is correct
56 Correct 282 ms 35980 KB Output is correct
57 Correct 159 ms 26388 KB Output is correct
58 Correct 242 ms 32660 KB Output is correct
59 Correct 62 ms 11880 KB Output is correct
60 Correct 255 ms 30628 KB Output is correct
61 Correct 240 ms 30348 KB Output is correct
62 Correct 264 ms 31812 KB Output is correct
63 Correct 172 ms 22480 KB Output is correct
64 Correct 4 ms 5076 KB Output is correct
65 Correct 4 ms 5204 KB Output is correct
66 Correct 4 ms 5144 KB Output is correct
67 Correct 3 ms 5080 KB Output is correct
68 Correct 4 ms 5076 KB Output is correct
69 Correct 4 ms 5204 KB Output is correct
70 Correct 4 ms 5216 KB Output is correct
71 Correct 4 ms 5204 KB Output is correct
72 Correct 3 ms 5076 KB Output is correct
73 Correct 3 ms 5204 KB Output is correct
74 Correct 136 ms 19836 KB Output is correct
75 Correct 207 ms 28336 KB Output is correct
76 Correct 144 ms 23932 KB Output is correct
77 Correct 130 ms 20852 KB Output is correct
78 Correct 59 ms 11840 KB Output is correct
79 Correct 56 ms 10136 KB Output is correct
80 Correct 109 ms 17240 KB Output is correct
81 Correct 230 ms 28872 KB Output is correct
82 Correct 181 ms 24364 KB Output is correct
83 Correct 233 ms 31924 KB Output is correct
84 Correct 99 ms 19060 KB Output is correct
85 Correct 151 ms 23356 KB Output is correct
86 Correct 72 ms 12676 KB Output is correct
87 Correct 262 ms 33452 KB Output is correct
88 Correct 305 ms 33528 KB Output is correct
89 Correct 190 ms 26776 KB Output is correct
90 Correct 126 ms 18456 KB Output is correct
91 Correct 138 ms 18928 KB Output is correct
92 Correct 191 ms 24744 KB Output is correct
93 Correct 359 ms 36388 KB Output is correct
94 Correct 335 ms 33112 KB Output is correct
95 Correct 411 ms 40208 KB Output is correct
96 Correct 222 ms 26152 KB Output is correct
97 Correct 288 ms 30184 KB Output is correct