Submission #723223

# Submission time Handle Problem Language Result Execution time Memory
723223 2023-04-13T11:07:49 Z lukameladze Swapping Cities (APIO20_swap) C++14
100 / 100
675 ms 78556 KB
#include "swap.h"
# include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
//#define int long long
#define pii pair <int, int>
#define pb push_back
const int N = 3e5 + 5, inf = 1e9;
int t,n,m,par[N],sz[N],is_chain[N],bec[N],in_deg[N],mx[N];
set < pii > s[N];
vector <int> comp[N];
struct edge {
    int x, y, c;
} a[N];
bool cmp(edge a, edge b) {
    return a.c < b.c;
}
void my_init() {
    for (int i = 1; i <= n; i++) {
        par[i] = i; sz[i] = 1; comp[i].pb(i); is_chain[i] = 1;
        s[i].insert({0, i});
        bec[i] = inf;
        is_chain[i] = 1;
    }
}
int get_col(int a) {
    return par[a];
}
void go(int a, int c){
    for (int x : comp[a]) {
        bec[x] = c;
    }
    is_chain[a] = false;
}
void col(int a, int b, int c) {
    a = get_col(a);
    b = get_col(b);
    if (a == b) {
        if (is_chain[a]) {
            go(a,c);
            is_chain[a] = false;
        }
        return ;   
    }
    if (sz[a] < sz[b]) swap(a,b);
    sz[a] += sz[b]; sz[b] = 0;
    mx[a] = max(mx[a], mx[b]);
    if (is_chain[a] && is_chain[b]) {
        if (mx[a] >= 3) {
            go(a,c); go(b,c);
        }
    } else
    if (is_chain[a] && !is_chain[b]) go(a,c), is_chain[a] = 0;
    else
    if (is_chain[b] && !is_chain[a]) go(b,c), is_chain[a] = 0;
    for (int vert : comp[b]) comp[a].pb(vert), par[vert] = a, s[vert].insert({c, a});//, cout<<vert<<" --- > "<<a<<" "<<c<<"dros\n";
    comp[b].clear();
}
int check(int mid, int a, int b) {
    int compa = (*(--s[a].upper_bound({mid,1e9}))).s;
    int compb = (*(--s[b].upper_bound({mid, 1e9}))).s;
    if (compa == compb && bec[a] <= mid && bec[b] <= mid) return 1;
    else return 0;
}
int solve(int x, int y) {
    int le = 1, ri = m, ans = 0;
    while (le <= ri) {
        int mid = (le + ri) / 2;
        if(check(mid, x,y)) {
            ans = mid;
            ri = mid - 1;
        } else le = mid + 1;
    }
    if (ans == 0) return -1;
    return a[ans].c;
}
void init(int N, int M, std::vector<int> U, std::vector<int> V, std::vector<int> W) {
    n = N; m = M;
    for (int i = 1; i <= m; i++) {
//        cin>>a[i].x>>a[i].y>>a[i].c;
        a[i].x = U[i - 1] + 1; a[i].y = V[i - 1] + 1; a[i].c = W[i - 1];
    }
    sort(a + 1, a + m + 1, cmp);
    my_init();
    for (int i = 1; i <= m; i++) {
        in_deg[a[i].x]++; in_deg[a[i].y]++;
        mx[get_col(a[i].x)] = max(mx[get_col(a[i].x)], in_deg[a[i].x]);
        mx[get_col(a[i].y)] = max(mx[get_col(a[i].y)], in_deg[a[i].y]);
        col(a[i].x, a[i].y, i);
    }
    
}
int getMinimumFuelCapacity(int X, int Y) {
  int a = X + 1; int b = Y + 1;
  return solve(a,b);
}
# Verdict Execution time Memory Grader output
1 Correct 11 ms 21460 KB Output is correct
2 Correct 11 ms 21460 KB Output is correct
3 Correct 11 ms 21460 KB Output is correct
4 Correct 12 ms 21588 KB Output is correct
5 Correct 13 ms 21704 KB Output is correct
6 Correct 12 ms 21716 KB Output is correct
7 Correct 16 ms 21728 KB Output is correct
8 Correct 13 ms 21588 KB Output is correct
9 Correct 239 ms 58000 KB Output is correct
10 Correct 324 ms 65404 KB Output is correct
11 Correct 291 ms 64464 KB Output is correct
12 Correct 325 ms 67528 KB Output is correct
13 Correct 131 ms 44956 KB Output is correct
14 Correct 260 ms 57716 KB Output is correct
15 Correct 653 ms 70640 KB Output is correct
16 Correct 638 ms 69120 KB Output is correct
17 Correct 670 ms 72272 KB Output is correct
18 Correct 296 ms 48956 KB Output is correct
19 Correct 186 ms 32980 KB Output is correct
20 Correct 616 ms 71740 KB Output is correct
21 Correct 627 ms 70452 KB Output is correct
22 Correct 636 ms 73464 KB Output is correct
23 Correct 297 ms 50412 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 21460 KB Output is correct
2 Correct 11 ms 21460 KB Output is correct
3 Correct 254 ms 46156 KB Output is correct
4 Correct 257 ms 46872 KB Output is correct
5 Correct 267 ms 46684 KB Output is correct
6 Correct 256 ms 46788 KB Output is correct
7 Correct 260 ms 46884 KB Output is correct
8 Correct 263 ms 46124 KB Output is correct
9 Correct 279 ms 46604 KB Output is correct
10 Correct 244 ms 45880 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 21460 KB Output is correct
2 Correct 11 ms 21460 KB Output is correct
3 Correct 11 ms 21460 KB Output is correct
4 Correct 12 ms 21588 KB Output is correct
5 Correct 13 ms 21704 KB Output is correct
6 Correct 12 ms 21716 KB Output is correct
7 Correct 16 ms 21728 KB Output is correct
8 Correct 13 ms 21588 KB Output is correct
9 Correct 11 ms 21460 KB Output is correct
10 Correct 12 ms 21716 KB Output is correct
11 Correct 11 ms 21716 KB Output is correct
12 Correct 13 ms 21720 KB Output is correct
13 Correct 12 ms 21716 KB Output is correct
14 Correct 12 ms 21720 KB Output is correct
15 Correct 13 ms 21816 KB Output is correct
16 Correct 13 ms 21716 KB Output is correct
17 Correct 11 ms 21588 KB Output is correct
18 Correct 11 ms 21760 KB Output is correct
19 Correct 10 ms 21588 KB Output is correct
20 Correct 11 ms 21720 KB Output is correct
21 Correct 11 ms 21716 KB Output is correct
22 Correct 12 ms 21588 KB Output is correct
23 Correct 12 ms 21588 KB Output is correct
24 Correct 12 ms 21844 KB Output is correct
25 Correct 13 ms 21732 KB Output is correct
26 Correct 13 ms 21848 KB Output is correct
27 Correct 11 ms 21640 KB Output is correct
28 Correct 12 ms 21716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 21460 KB Output is correct
2 Correct 11 ms 21460 KB Output is correct
3 Correct 11 ms 21460 KB Output is correct
4 Correct 11 ms 21460 KB Output is correct
5 Correct 12 ms 21588 KB Output is correct
6 Correct 13 ms 21704 KB Output is correct
7 Correct 12 ms 21716 KB Output is correct
8 Correct 16 ms 21728 KB Output is correct
9 Correct 13 ms 21588 KB Output is correct
10 Correct 239 ms 58000 KB Output is correct
11 Correct 324 ms 65404 KB Output is correct
12 Correct 291 ms 64464 KB Output is correct
13 Correct 325 ms 67528 KB Output is correct
14 Correct 131 ms 44956 KB Output is correct
15 Correct 12 ms 21716 KB Output is correct
16 Correct 11 ms 21716 KB Output is correct
17 Correct 13 ms 21720 KB Output is correct
18 Correct 12 ms 21716 KB Output is correct
19 Correct 12 ms 21720 KB Output is correct
20 Correct 13 ms 21816 KB Output is correct
21 Correct 13 ms 21716 KB Output is correct
22 Correct 11 ms 21588 KB Output is correct
23 Correct 11 ms 21760 KB Output is correct
24 Correct 10 ms 21588 KB Output is correct
25 Correct 11 ms 21720 KB Output is correct
26 Correct 11 ms 21716 KB Output is correct
27 Correct 12 ms 21588 KB Output is correct
28 Correct 12 ms 21588 KB Output is correct
29 Correct 12 ms 21844 KB Output is correct
30 Correct 13 ms 21732 KB Output is correct
31 Correct 13 ms 21848 KB Output is correct
32 Correct 11 ms 21640 KB Output is correct
33 Correct 12 ms 21716 KB Output is correct
34 Correct 24 ms 26200 KB Output is correct
35 Correct 309 ms 66880 KB Output is correct
36 Correct 296 ms 65728 KB Output is correct
37 Correct 281 ms 64108 KB Output is correct
38 Correct 275 ms 61292 KB Output is correct
39 Correct 270 ms 60336 KB Output is correct
40 Correct 220 ms 55624 KB Output is correct
41 Correct 329 ms 68428 KB Output is correct
42 Correct 307 ms 68228 KB Output is correct
43 Correct 114 ms 43728 KB Output is correct
44 Correct 285 ms 59336 KB Output is correct
45 Correct 152 ms 46240 KB Output is correct
46 Correct 338 ms 65832 KB Output is correct
47 Correct 209 ms 55236 KB Output is correct
48 Correct 152 ms 47688 KB Output is correct
49 Correct 72 ms 29948 KB Output is correct
50 Correct 63 ms 29504 KB Output is correct
51 Correct 127 ms 42012 KB Output is correct
52 Correct 300 ms 66524 KB Output is correct
53 Correct 211 ms 55496 KB Output is correct
54 Correct 357 ms 73412 KB Output is correct
55 Correct 117 ms 44096 KB Output is correct
56 Correct 199 ms 52500 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 21460 KB Output is correct
2 Correct 11 ms 21460 KB Output is correct
3 Correct 11 ms 21460 KB Output is correct
4 Correct 12 ms 21588 KB Output is correct
5 Correct 13 ms 21704 KB Output is correct
6 Correct 12 ms 21716 KB Output is correct
7 Correct 16 ms 21728 KB Output is correct
8 Correct 13 ms 21588 KB Output is correct
9 Correct 239 ms 58000 KB Output is correct
10 Correct 324 ms 65404 KB Output is correct
11 Correct 291 ms 64464 KB Output is correct
12 Correct 325 ms 67528 KB Output is correct
13 Correct 131 ms 44956 KB Output is correct
14 Correct 260 ms 57716 KB Output is correct
15 Correct 653 ms 70640 KB Output is correct
16 Correct 638 ms 69120 KB Output is correct
17 Correct 670 ms 72272 KB Output is correct
18 Correct 296 ms 48956 KB Output is correct
19 Correct 254 ms 46156 KB Output is correct
20 Correct 257 ms 46872 KB Output is correct
21 Correct 267 ms 46684 KB Output is correct
22 Correct 256 ms 46788 KB Output is correct
23 Correct 260 ms 46884 KB Output is correct
24 Correct 263 ms 46124 KB Output is correct
25 Correct 279 ms 46604 KB Output is correct
26 Correct 244 ms 45880 KB Output is correct
27 Correct 12 ms 21716 KB Output is correct
28 Correct 11 ms 21716 KB Output is correct
29 Correct 13 ms 21720 KB Output is correct
30 Correct 12 ms 21716 KB Output is correct
31 Correct 12 ms 21720 KB Output is correct
32 Correct 13 ms 21816 KB Output is correct
33 Correct 13 ms 21716 KB Output is correct
34 Correct 11 ms 21588 KB Output is correct
35 Correct 11 ms 21760 KB Output is correct
36 Correct 24 ms 26200 KB Output is correct
37 Correct 309 ms 66880 KB Output is correct
38 Correct 296 ms 65728 KB Output is correct
39 Correct 281 ms 64108 KB Output is correct
40 Correct 275 ms 61292 KB Output is correct
41 Correct 270 ms 60336 KB Output is correct
42 Correct 220 ms 55624 KB Output is correct
43 Correct 329 ms 68428 KB Output is correct
44 Correct 307 ms 68228 KB Output is correct
45 Correct 114 ms 43728 KB Output is correct
46 Correct 285 ms 59336 KB Output is correct
47 Correct 39 ms 26400 KB Output is correct
48 Correct 625 ms 71852 KB Output is correct
49 Correct 578 ms 69980 KB Output is correct
50 Correct 604 ms 69592 KB Output is correct
51 Correct 572 ms 67384 KB Output is correct
52 Correct 488 ms 63112 KB Output is correct
53 Correct 389 ms 51448 KB Output is correct
54 Correct 624 ms 72492 KB Output is correct
55 Correct 642 ms 73656 KB Output is correct
56 Correct 295 ms 49492 KB Output is correct
57 Correct 493 ms 63784 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 21460 KB Output is correct
2 Correct 11 ms 21460 KB Output is correct
3 Correct 11 ms 21460 KB Output is correct
4 Correct 11 ms 21460 KB Output is correct
5 Correct 12 ms 21588 KB Output is correct
6 Correct 13 ms 21704 KB Output is correct
7 Correct 12 ms 21716 KB Output is correct
8 Correct 16 ms 21728 KB Output is correct
9 Correct 13 ms 21588 KB Output is correct
10 Correct 239 ms 58000 KB Output is correct
11 Correct 324 ms 65404 KB Output is correct
12 Correct 291 ms 64464 KB Output is correct
13 Correct 325 ms 67528 KB Output is correct
14 Correct 131 ms 44956 KB Output is correct
15 Correct 260 ms 57716 KB Output is correct
16 Correct 653 ms 70640 KB Output is correct
17 Correct 638 ms 69120 KB Output is correct
18 Correct 670 ms 72272 KB Output is correct
19 Correct 296 ms 48956 KB Output is correct
20 Correct 254 ms 46156 KB Output is correct
21 Correct 257 ms 46872 KB Output is correct
22 Correct 267 ms 46684 KB Output is correct
23 Correct 256 ms 46788 KB Output is correct
24 Correct 260 ms 46884 KB Output is correct
25 Correct 263 ms 46124 KB Output is correct
26 Correct 279 ms 46604 KB Output is correct
27 Correct 244 ms 45880 KB Output is correct
28 Correct 12 ms 21716 KB Output is correct
29 Correct 11 ms 21716 KB Output is correct
30 Correct 13 ms 21720 KB Output is correct
31 Correct 12 ms 21716 KB Output is correct
32 Correct 12 ms 21720 KB Output is correct
33 Correct 13 ms 21816 KB Output is correct
34 Correct 13 ms 21716 KB Output is correct
35 Correct 11 ms 21588 KB Output is correct
36 Correct 11 ms 21760 KB Output is correct
37 Correct 24 ms 26200 KB Output is correct
38 Correct 309 ms 66880 KB Output is correct
39 Correct 296 ms 65728 KB Output is correct
40 Correct 281 ms 64108 KB Output is correct
41 Correct 275 ms 61292 KB Output is correct
42 Correct 270 ms 60336 KB Output is correct
43 Correct 220 ms 55624 KB Output is correct
44 Correct 329 ms 68428 KB Output is correct
45 Correct 307 ms 68228 KB Output is correct
46 Correct 114 ms 43728 KB Output is correct
47 Correct 285 ms 59336 KB Output is correct
48 Correct 39 ms 26400 KB Output is correct
49 Correct 625 ms 71852 KB Output is correct
50 Correct 578 ms 69980 KB Output is correct
51 Correct 604 ms 69592 KB Output is correct
52 Correct 572 ms 67384 KB Output is correct
53 Correct 488 ms 63112 KB Output is correct
54 Correct 389 ms 51448 KB Output is correct
55 Correct 624 ms 72492 KB Output is correct
56 Correct 642 ms 73656 KB Output is correct
57 Correct 295 ms 49492 KB Output is correct
58 Correct 493 ms 63784 KB Output is correct
59 Correct 186 ms 32980 KB Output is correct
60 Correct 616 ms 71740 KB Output is correct
61 Correct 627 ms 70452 KB Output is correct
62 Correct 636 ms 73464 KB Output is correct
63 Correct 297 ms 50412 KB Output is correct
64 Correct 10 ms 21588 KB Output is correct
65 Correct 11 ms 21720 KB Output is correct
66 Correct 11 ms 21716 KB Output is correct
67 Correct 12 ms 21588 KB Output is correct
68 Correct 12 ms 21588 KB Output is correct
69 Correct 12 ms 21844 KB Output is correct
70 Correct 13 ms 21732 KB Output is correct
71 Correct 13 ms 21848 KB Output is correct
72 Correct 11 ms 21640 KB Output is correct
73 Correct 12 ms 21716 KB Output is correct
74 Correct 152 ms 46240 KB Output is correct
75 Correct 338 ms 65832 KB Output is correct
76 Correct 209 ms 55236 KB Output is correct
77 Correct 152 ms 47688 KB Output is correct
78 Correct 72 ms 29948 KB Output is correct
79 Correct 63 ms 29504 KB Output is correct
80 Correct 127 ms 42012 KB Output is correct
81 Correct 300 ms 66524 KB Output is correct
82 Correct 211 ms 55496 KB Output is correct
83 Correct 357 ms 73412 KB Output is correct
84 Correct 117 ms 44096 KB Output is correct
85 Correct 199 ms 52500 KB Output is correct
86 Correct 89 ms 31808 KB Output is correct
87 Correct 582 ms 68124 KB Output is correct
88 Correct 615 ms 68904 KB Output is correct
89 Correct 342 ms 51116 KB Output is correct
90 Correct 201 ms 34596 KB Output is correct
91 Correct 206 ms 36436 KB Output is correct
92 Correct 291 ms 47044 KB Output is correct
93 Correct 525 ms 69084 KB Output is correct
94 Correct 415 ms 61100 KB Output is correct
95 Correct 675 ms 78556 KB Output is correct
96 Correct 278 ms 49304 KB Output is correct
97 Correct 356 ms 56116 KB Output is correct