Submission #472906

# Submission time Handle Problem Language Result Execution time Memory
472906 2021-09-14T13:51:31 Z cs71107 Swapping Cities (APIO20_swap) C++14
100 / 100
448 ms 56420 KB
#include "swap.h"

#include <bits/stdc++.h>
#define f first
#define s second

using namespace std;
typedef pair<int,int> pii;
const int MAXN = 1e5+10;

int pp[MAXN];
int id[MAXN];
int cal[MAXN];

int chk[MAXN*3];
int val[MAXN*3];
int ch[MAXN*3][3];

int vid[MAXN*3];
int depth[MAXN*3];
int par[MAXN*3][20];

vector<pii> edge[MAXN*2];

int root(int x){
    if(pp[x]==x)return x;
    else return pp[x] = root(pp[x]);
}

void init(int N, int M,
          std::vector<int> U, std::vector<int> V, std::vector<int> W) {
        
  for(int i=0;i<N;i++){
    pp[i] = i;
    id[i] = i;
    ch[i][0] = -1;
    ch[i][1] = -1;
  }

  vector<int> pos = W;

  sort(pos.begin(),pos.end());
  pos.erase(unique(pos.begin(),pos.end()),pos.end());

  int sz = (int)pos.size();
  int idx = -1,idy = - 1;

  for(int i=0;i<M;i++){
    idx = lower_bound(pos.begin(),pos.end(),W[i])-pos.begin();
    edge[idx].push_back(pii(U[i],V[i]));
  }        

  int seq = N-1;
  int cur = 0;
  int x,y;
  int a,b;


  for(int i=0;i<sz;i++){
    cur = pos[i];
    for(int j=0;j<(int)edge[i].size();j++){
      x = edge[i][j].f;
      y = edge[i][j].s;

      cal[x]++;
      cal[y]++;

      a = root(x);
      b = root(y);

      idx = id[a];
      idy = id[b];

      seq++;

      if(a^b){
        pp[b] = a;
        id[a] = seq;

        chk[seq] = chk[idx]|chk[idy];
        
        if(cal[x]>=3||cal[y]>=3)chk[seq] = 1;

        val[seq] = cur;

        par[idx][0] = seq;
        par[idy][0] = seq;
        ch[seq][0] = idx;
        ch[seq][1] = idy;

      }
      else {
        id[a] = seq;
        chk[seq] = 1;
        val[seq] = cur;

        par[idx][0] = seq;
        ch[seq][0] = idx;
        ch[seq][1] = -1;
      }

    }

  }

  par[seq][0] = seq;
  
  vid[seq] = -1;

  for(int i=seq;i>=0;i--){
    if(chk[i])vid[i] = i;    

    if(ch[i][0]!=-1){
      vid[ch[i][0]] = vid[i];
      depth[ch[i][0]] = depth[i]+1;
    }
    if(ch[i][1]!=-1){
      vid[ch[i][1]] = vid[i];
      depth[ch[i][1]] = depth[i]+1; 
    }
  }

  for(int j=1;j<20;j++){
    for(int i=0;i<=seq;i++){
      par[i][j] = par[par[i][j-1]][j-1];
    }
  }

  /*
  for(int i=0;i<=seq;i++){
    cout<<i<<" "<<par[i][0]<<"\n";
    cout<<ch[i][0]<<" "<<ch[i][1]<<"\n";
    cout<<chk[i]<<" "<<val[i]<<" "<<vid[i]<<" "<<depth[i]<<"\n";
  }

  for(int i=0;i<=seq;i++){
    for(int j=0;j<=10;j++){
      cout<<par[i][j]<<" ";
    }
    cout<<"\n";
  }*/

  return;
}

int getMinimumFuelCapacity(int X, int Y) {
  
  if(depth[X]>depth[Y])swap(X,Y);

  int d = depth[Y]-depth[X];

  for(int i=19;~i;i--){
    if((1<<i)&d)Y = par[Y][i];
  }

  for(int i=19;~i;i--){
    if(par[X][i]^par[Y][i])X = par[X][i],Y = par[Y][i];
  }
  
  int lc = -1;

  if(X^Y)lc = par[X][0];
  else lc = X;

  //cout<<lc<<"\n";

  int ver = vid[lc];

  if(ver==-1)return -1;
  else return val[ver];
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 3 ms 4996 KB Output is correct
4 Correct 4 ms 5068 KB Output is correct
5 Correct 4 ms 5324 KB Output is correct
6 Correct 4 ms 5196 KB Output is correct
7 Correct 4 ms 5324 KB Output is correct
8 Correct 4 ms 5324 KB Output is correct
9 Correct 100 ms 28100 KB Output is correct
10 Correct 131 ms 33348 KB Output is correct
11 Correct 130 ms 32856 KB Output is correct
12 Correct 133 ms 34544 KB Output is correct
13 Correct 147 ms 34884 KB Output is correct
14 Correct 115 ms 28436 KB Output is correct
15 Correct 272 ms 37344 KB Output is correct
16 Correct 273 ms 36524 KB Output is correct
17 Correct 276 ms 38288 KB Output is correct
18 Correct 368 ms 38852 KB Output is correct
19 Correct 128 ms 14764 KB Output is correct
20 Correct 259 ms 38404 KB Output is correct
21 Correct 262 ms 37656 KB Output is correct
22 Correct 276 ms 39576 KB Output is correct
23 Correct 409 ms 39944 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 384 ms 37340 KB Output is correct
4 Correct 326 ms 38644 KB Output is correct
5 Correct 333 ms 38240 KB Output is correct
6 Correct 380 ms 38464 KB Output is correct
7 Correct 354 ms 38468 KB Output is correct
8 Correct 360 ms 37356 KB Output is correct
9 Correct 317 ms 38084 KB Output is correct
10 Correct 344 ms 37036 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 3 ms 4996 KB Output is correct
4 Correct 4 ms 5068 KB Output is correct
5 Correct 4 ms 5324 KB Output is correct
6 Correct 4 ms 5196 KB Output is correct
7 Correct 4 ms 5324 KB Output is correct
8 Correct 4 ms 5324 KB Output is correct
9 Correct 3 ms 4940 KB Output is correct
10 Correct 4 ms 5324 KB Output is correct
11 Correct 4 ms 5324 KB Output is correct
12 Correct 4 ms 5324 KB Output is correct
13 Correct 4 ms 5260 KB Output is correct
14 Correct 4 ms 5196 KB Output is correct
15 Correct 4 ms 5324 KB Output is correct
16 Correct 4 ms 5324 KB Output is correct
17 Correct 4 ms 5324 KB Output is correct
18 Correct 4 ms 5324 KB Output is correct
19 Correct 5 ms 5264 KB Output is correct
20 Correct 4 ms 5316 KB Output is correct
21 Correct 4 ms 5268 KB Output is correct
22 Correct 5 ms 5324 KB Output is correct
23 Correct 4 ms 5196 KB Output is correct
24 Correct 4 ms 5452 KB Output is correct
25 Correct 4 ms 5400 KB Output is correct
26 Correct 4 ms 5452 KB Output is correct
27 Correct 4 ms 5324 KB Output is correct
28 Correct 4 ms 5452 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 3 ms 4996 KB Output is correct
5 Correct 4 ms 5068 KB Output is correct
6 Correct 4 ms 5324 KB Output is correct
7 Correct 4 ms 5196 KB Output is correct
8 Correct 4 ms 5324 KB Output is correct
9 Correct 4 ms 5324 KB Output is correct
10 Correct 100 ms 28100 KB Output is correct
11 Correct 131 ms 33348 KB Output is correct
12 Correct 130 ms 32856 KB Output is correct
13 Correct 133 ms 34544 KB Output is correct
14 Correct 147 ms 34884 KB Output is correct
15 Correct 4 ms 5324 KB Output is correct
16 Correct 4 ms 5324 KB Output is correct
17 Correct 4 ms 5324 KB Output is correct
18 Correct 4 ms 5260 KB Output is correct
19 Correct 4 ms 5196 KB Output is correct
20 Correct 4 ms 5324 KB Output is correct
21 Correct 4 ms 5324 KB Output is correct
22 Correct 4 ms 5324 KB Output is correct
23 Correct 4 ms 5324 KB Output is correct
24 Correct 5 ms 5264 KB Output is correct
25 Correct 4 ms 5316 KB Output is correct
26 Correct 4 ms 5268 KB Output is correct
27 Correct 5 ms 5324 KB Output is correct
28 Correct 4 ms 5196 KB Output is correct
29 Correct 4 ms 5452 KB Output is correct
30 Correct 4 ms 5400 KB Output is correct
31 Correct 4 ms 5452 KB Output is correct
32 Correct 4 ms 5324 KB Output is correct
33 Correct 4 ms 5452 KB Output is correct
34 Correct 16 ms 8952 KB Output is correct
35 Correct 130 ms 34120 KB Output is correct
36 Correct 126 ms 34096 KB Output is correct
37 Correct 124 ms 33988 KB Output is correct
38 Correct 125 ms 33636 KB Output is correct
39 Correct 127 ms 33536 KB Output is correct
40 Correct 115 ms 31140 KB Output is correct
41 Correct 134 ms 34500 KB Output is correct
42 Correct 133 ms 34092 KB Output is correct
43 Correct 136 ms 33988 KB Output is correct
44 Correct 137 ms 34452 KB Output is correct
45 Correct 186 ms 40848 KB Output is correct
46 Correct 154 ms 34100 KB Output is correct
47 Correct 129 ms 34116 KB Output is correct
48 Correct 148 ms 35696 KB Output is correct
49 Correct 160 ms 33856 KB Output is correct
50 Correct 118 ms 27972 KB Output is correct
51 Correct 187 ms 35816 KB Output is correct
52 Correct 224 ms 45928 KB Output is correct
53 Correct 227 ms 49160 KB Output is correct
54 Correct 253 ms 52764 KB Output is correct
55 Correct 139 ms 34064 KB Output is correct
56 Correct 255 ms 47832 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 3 ms 4996 KB Output is correct
4 Correct 4 ms 5068 KB Output is correct
5 Correct 4 ms 5324 KB Output is correct
6 Correct 4 ms 5196 KB Output is correct
7 Correct 4 ms 5324 KB Output is correct
8 Correct 4 ms 5324 KB Output is correct
9 Correct 100 ms 28100 KB Output is correct
10 Correct 131 ms 33348 KB Output is correct
11 Correct 130 ms 32856 KB Output is correct
12 Correct 133 ms 34544 KB Output is correct
13 Correct 147 ms 34884 KB Output is correct
14 Correct 115 ms 28436 KB Output is correct
15 Correct 272 ms 37344 KB Output is correct
16 Correct 273 ms 36524 KB Output is correct
17 Correct 276 ms 38288 KB Output is correct
18 Correct 368 ms 38852 KB Output is correct
19 Correct 384 ms 37340 KB Output is correct
20 Correct 326 ms 38644 KB Output is correct
21 Correct 333 ms 38240 KB Output is correct
22 Correct 380 ms 38464 KB Output is correct
23 Correct 354 ms 38468 KB Output is correct
24 Correct 360 ms 37356 KB Output is correct
25 Correct 317 ms 38084 KB Output is correct
26 Correct 344 ms 37036 KB Output is correct
27 Correct 4 ms 5324 KB Output is correct
28 Correct 4 ms 5324 KB Output is correct
29 Correct 4 ms 5324 KB Output is correct
30 Correct 4 ms 5260 KB Output is correct
31 Correct 4 ms 5196 KB Output is correct
32 Correct 4 ms 5324 KB Output is correct
33 Correct 4 ms 5324 KB Output is correct
34 Correct 4 ms 5324 KB Output is correct
35 Correct 4 ms 5324 KB Output is correct
36 Correct 16 ms 8952 KB Output is correct
37 Correct 130 ms 34120 KB Output is correct
38 Correct 126 ms 34096 KB Output is correct
39 Correct 124 ms 33988 KB Output is correct
40 Correct 125 ms 33636 KB Output is correct
41 Correct 127 ms 33536 KB Output is correct
42 Correct 115 ms 31140 KB Output is correct
43 Correct 134 ms 34500 KB Output is correct
44 Correct 133 ms 34092 KB Output is correct
45 Correct 136 ms 33988 KB Output is correct
46 Correct 137 ms 34452 KB Output is correct
47 Correct 26 ms 9188 KB Output is correct
48 Correct 260 ms 38480 KB Output is correct
49 Correct 256 ms 38592 KB Output is correct
50 Correct 273 ms 38424 KB Output is correct
51 Correct 257 ms 38360 KB Output is correct
52 Correct 253 ms 36740 KB Output is correct
53 Correct 202 ms 30132 KB Output is correct
54 Correct 290 ms 39560 KB Output is correct
55 Correct 256 ms 38476 KB Output is correct
56 Correct 400 ms 38516 KB Output is correct
57 Correct 271 ms 39560 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 3 ms 4996 KB Output is correct
5 Correct 4 ms 5068 KB Output is correct
6 Correct 4 ms 5324 KB Output is correct
7 Correct 4 ms 5196 KB Output is correct
8 Correct 4 ms 5324 KB Output is correct
9 Correct 4 ms 5324 KB Output is correct
10 Correct 100 ms 28100 KB Output is correct
11 Correct 131 ms 33348 KB Output is correct
12 Correct 130 ms 32856 KB Output is correct
13 Correct 133 ms 34544 KB Output is correct
14 Correct 147 ms 34884 KB Output is correct
15 Correct 115 ms 28436 KB Output is correct
16 Correct 272 ms 37344 KB Output is correct
17 Correct 273 ms 36524 KB Output is correct
18 Correct 276 ms 38288 KB Output is correct
19 Correct 368 ms 38852 KB Output is correct
20 Correct 384 ms 37340 KB Output is correct
21 Correct 326 ms 38644 KB Output is correct
22 Correct 333 ms 38240 KB Output is correct
23 Correct 380 ms 38464 KB Output is correct
24 Correct 354 ms 38468 KB Output is correct
25 Correct 360 ms 37356 KB Output is correct
26 Correct 317 ms 38084 KB Output is correct
27 Correct 344 ms 37036 KB Output is correct
28 Correct 4 ms 5324 KB Output is correct
29 Correct 4 ms 5324 KB Output is correct
30 Correct 4 ms 5324 KB Output is correct
31 Correct 4 ms 5260 KB Output is correct
32 Correct 4 ms 5196 KB Output is correct
33 Correct 4 ms 5324 KB Output is correct
34 Correct 4 ms 5324 KB Output is correct
35 Correct 4 ms 5324 KB Output is correct
36 Correct 4 ms 5324 KB Output is correct
37 Correct 16 ms 8952 KB Output is correct
38 Correct 130 ms 34120 KB Output is correct
39 Correct 126 ms 34096 KB Output is correct
40 Correct 124 ms 33988 KB Output is correct
41 Correct 125 ms 33636 KB Output is correct
42 Correct 127 ms 33536 KB Output is correct
43 Correct 115 ms 31140 KB Output is correct
44 Correct 134 ms 34500 KB Output is correct
45 Correct 133 ms 34092 KB Output is correct
46 Correct 136 ms 33988 KB Output is correct
47 Correct 137 ms 34452 KB Output is correct
48 Correct 26 ms 9188 KB Output is correct
49 Correct 260 ms 38480 KB Output is correct
50 Correct 256 ms 38592 KB Output is correct
51 Correct 273 ms 38424 KB Output is correct
52 Correct 257 ms 38360 KB Output is correct
53 Correct 253 ms 36740 KB Output is correct
54 Correct 202 ms 30132 KB Output is correct
55 Correct 290 ms 39560 KB Output is correct
56 Correct 256 ms 38476 KB Output is correct
57 Correct 400 ms 38516 KB Output is correct
58 Correct 271 ms 39560 KB Output is correct
59 Correct 128 ms 14764 KB Output is correct
60 Correct 259 ms 38404 KB Output is correct
61 Correct 262 ms 37656 KB Output is correct
62 Correct 276 ms 39576 KB Output is correct
63 Correct 409 ms 39944 KB Output is correct
64 Correct 5 ms 5264 KB Output is correct
65 Correct 4 ms 5316 KB Output is correct
66 Correct 4 ms 5268 KB Output is correct
67 Correct 5 ms 5324 KB Output is correct
68 Correct 4 ms 5196 KB Output is correct
69 Correct 4 ms 5452 KB Output is correct
70 Correct 4 ms 5400 KB Output is correct
71 Correct 4 ms 5452 KB Output is correct
72 Correct 4 ms 5324 KB Output is correct
73 Correct 4 ms 5452 KB Output is correct
74 Correct 186 ms 40848 KB Output is correct
75 Correct 154 ms 34100 KB Output is correct
76 Correct 129 ms 34116 KB Output is correct
77 Correct 148 ms 35696 KB Output is correct
78 Correct 160 ms 33856 KB Output is correct
79 Correct 118 ms 27972 KB Output is correct
80 Correct 187 ms 35816 KB Output is correct
81 Correct 224 ms 45928 KB Output is correct
82 Correct 227 ms 49160 KB Output is correct
83 Correct 253 ms 52764 KB Output is correct
84 Correct 139 ms 34064 KB Output is correct
85 Correct 255 ms 47832 KB Output is correct
86 Correct 94 ms 18580 KB Output is correct
87 Correct 254 ms 38560 KB Output is correct
88 Correct 249 ms 38560 KB Output is correct
89 Correct 319 ms 38508 KB Output is correct
90 Correct 300 ms 38944 KB Output is correct
91 Correct 281 ms 37488 KB Output is correct
92 Correct 358 ms 39360 KB Output is correct
93 Correct 342 ms 49564 KB Output is correct
94 Correct 448 ms 52760 KB Output is correct
95 Correct 389 ms 56420 KB Output is correct
96 Correct 381 ms 38612 KB Output is correct
97 Correct 388 ms 45576 KB Output is correct