Submission #701380

# Submission time Handle Problem Language Result Execution time Memory
701380 2023-02-21T06:18:01 Z Quan2003 Swapping Cities (APIO20_swap) C++17
100 / 100
358 ms 62948 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 3e5 + 100;
const int INF = 1e7 + 10; 
long long n,m,q,dsz,E;
int timer = 1;
vector<int>adj[N + 1];
int fa[N + 1];
int deg[N + 1],deg1[N + 1],deg3[N + 1]; 
int up[23][N + 1],dp[N + 1],val[N + 1];
int lf[N + 1],ri[N + 1]; 
int in[N + 1],to[N + 1]; 
struct edge{
     int w,u,v;
} edges[N + 1]; 
int find(int u){
     if(u == fa[u]) return u;
     return fa[u] = find(fa[u]); 
}
void dfs_calc(int u){
    if(!val[u]) val[u] = val[up[0][u]]; 
    for(int i = 1; i < 23 ; i++){
        up[i][u] = up[i - 1][up[i - 1][u]];
    }
    for(int i = 0; i < adj[u].size(); i++){
        int v = adj[u][i]; 
        dp[v] = dp[up[0][v] = u] + 1;      
        dfs_calc(v);
    }
}
int lca(int a, int b){
    if(dp[a] < dp[b]) swap(a,b);
    int d = dp[a] - dp[b];
    for(int i = 0; i < 23 ; i++){
        if( (d >> i) & 1) a = up[i][a];
    }
    if(a == b) return a;
    for(int i = 22; i >= 0; i--){
         int tA = up[i][a]; int tB = up[i][b];
         if(tA != tB) {
            a = tA; b = tB;
         }
    } 
    return up[0][a];
} 
bool cmp(edge &a, edge &b){
     return a.w < b.w; 
}
void init(int N, int M,std::vector<int> U, std::vector<int> V, std::vector<int> W){
         for(int i = 0; i < M; i++){
              edges[i].u = ++U[i]; edges[i].v = ++V[i];
              edges[i].w = W[i]; 
         }
         sort(edges,edges + M,cmp);
         E = N;
         for(int i = 1; i <= N;i++) fa[i] = i; 
         for(int i = 0; i < M; i++){
              int x = find(edges[i].u); int y = find(edges[i].v);
              if(deg[edges[i].u] == 1) deg1[x]--; 
              if(deg[edges[i].v] == 1) deg1[y]--; 
              deg[edges[i].u]++; deg[edges[i].v]++; 
              if(deg[edges[i].u] == 1) deg1[x]++;  
              else if(deg[edges[i].u] == 3) deg3[x]++;
              if(deg[edges[i].v] == 1) deg1[y]++;
              else if(deg[edges[i].v] == 3) deg3[y]++; 
              E++; 
              if(x == y){
                   fa[x] = fa[E] = E;
                   deg3[E] += deg3[x];
                   deg1[E] += deg1[x];
                   adj[E].push_back(x); 
              }
              else{
                   ////if(x < y) swap(x,y); 
                   fa[x] = fa[y] = fa[E] = E; 
                   deg3[E] += deg3[x] + deg3[y];
                   deg1[E] += deg1[x] + deg1[y];
                   adj[E].push_back(x);
                   adj[E].push_back(y); 
              }
              if(!deg1[E] or deg3[E]) val[E] = edges[i].w;
         }
         dfs_calc(E);
}
int getMinimumFuelCapacity(int X, int Y){
      int anc = lca(++X,++Y);
      return val[anc] != 0 ? val[anc] : - 1;     
}

Compilation message

swap.cpp: In function 'void dfs_calc(int)':
swap.cpp:26:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   26 |     for(int i = 0; i < adj[u].size(); i++){
      |                    ~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7508 KB Output is correct
2 Correct 4 ms 7508 KB Output is correct
3 Correct 4 ms 7508 KB Output is correct
4 Correct 4 ms 7636 KB Output is correct
5 Correct 4 ms 7764 KB Output is correct
6 Correct 4 ms 7764 KB Output is correct
7 Correct 5 ms 7764 KB Output is correct
8 Correct 4 ms 7764 KB Output is correct
9 Correct 99 ms 29840 KB Output is correct
10 Correct 125 ms 34936 KB Output is correct
11 Correct 139 ms 34416 KB Output is correct
12 Correct 127 ms 36052 KB Output is correct
13 Correct 102 ms 39856 KB Output is correct
14 Correct 111 ms 30020 KB Output is correct
15 Correct 295 ms 36924 KB Output is correct
16 Correct 286 ms 36080 KB Output is correct
17 Correct 274 ms 37844 KB Output is correct
18 Correct 325 ms 41660 KB Output is correct
19 Correct 94 ms 15472 KB Output is correct
20 Correct 283 ms 38180 KB Output is correct
21 Correct 328 ms 37384 KB Output is correct
22 Correct 280 ms 39156 KB Output is correct
23 Correct 335 ms 43092 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7508 KB Output is correct
2 Correct 4 ms 7508 KB Output is correct
3 Correct 297 ms 44868 KB Output is correct
4 Correct 297 ms 46672 KB Output is correct
5 Correct 306 ms 45668 KB Output is correct
6 Correct 302 ms 46384 KB Output is correct
7 Correct 342 ms 46148 KB Output is correct
8 Correct 287 ms 44640 KB Output is correct
9 Correct 349 ms 45888 KB Output is correct
10 Correct 285 ms 44292 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7508 KB Output is correct
2 Correct 4 ms 7508 KB Output is correct
3 Correct 4 ms 7508 KB Output is correct
4 Correct 4 ms 7636 KB Output is correct
5 Correct 4 ms 7764 KB Output is correct
6 Correct 4 ms 7764 KB Output is correct
7 Correct 5 ms 7764 KB Output is correct
8 Correct 4 ms 7764 KB Output is correct
9 Correct 4 ms 7508 KB Output is correct
10 Correct 4 ms 7764 KB Output is correct
11 Correct 4 ms 7764 KB Output is correct
12 Correct 5 ms 7764 KB Output is correct
13 Correct 5 ms 7764 KB Output is correct
14 Correct 4 ms 7764 KB Output is correct
15 Correct 5 ms 7808 KB Output is correct
16 Correct 4 ms 7808 KB Output is correct
17 Correct 5 ms 7864 KB Output is correct
18 Correct 4 ms 7764 KB Output is correct
19 Correct 4 ms 7764 KB Output is correct
20 Correct 4 ms 7764 KB Output is correct
21 Correct 4 ms 7764 KB Output is correct
22 Correct 5 ms 7892 KB Output is correct
23 Correct 5 ms 7724 KB Output is correct
24 Correct 5 ms 7892 KB Output is correct
25 Correct 5 ms 8020 KB Output is correct
26 Correct 7 ms 7988 KB Output is correct
27 Correct 4 ms 7764 KB Output is correct
28 Correct 6 ms 8072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7508 KB Output is correct
2 Correct 4 ms 7508 KB Output is correct
3 Correct 4 ms 7508 KB Output is correct
4 Correct 4 ms 7508 KB Output is correct
5 Correct 4 ms 7636 KB Output is correct
6 Correct 4 ms 7764 KB Output is correct
7 Correct 4 ms 7764 KB Output is correct
8 Correct 5 ms 7764 KB Output is correct
9 Correct 4 ms 7764 KB Output is correct
10 Correct 99 ms 29840 KB Output is correct
11 Correct 125 ms 34936 KB Output is correct
12 Correct 139 ms 34416 KB Output is correct
13 Correct 127 ms 36052 KB Output is correct
14 Correct 102 ms 39856 KB Output is correct
15 Correct 4 ms 7764 KB Output is correct
16 Correct 4 ms 7764 KB Output is correct
17 Correct 5 ms 7764 KB Output is correct
18 Correct 5 ms 7764 KB Output is correct
19 Correct 4 ms 7764 KB Output is correct
20 Correct 5 ms 7808 KB Output is correct
21 Correct 4 ms 7808 KB Output is correct
22 Correct 5 ms 7864 KB Output is correct
23 Correct 4 ms 7764 KB Output is correct
24 Correct 4 ms 7764 KB Output is correct
25 Correct 4 ms 7764 KB Output is correct
26 Correct 4 ms 7764 KB Output is correct
27 Correct 5 ms 7892 KB Output is correct
28 Correct 5 ms 7724 KB Output is correct
29 Correct 5 ms 7892 KB Output is correct
30 Correct 5 ms 8020 KB Output is correct
31 Correct 7 ms 7988 KB Output is correct
32 Correct 4 ms 7764 KB Output is correct
33 Correct 6 ms 8072 KB Output is correct
34 Correct 14 ms 11348 KB Output is correct
35 Correct 129 ms 36064 KB Output is correct
36 Correct 126 ms 36040 KB Output is correct
37 Correct 126 ms 36028 KB Output is correct
38 Correct 130 ms 35660 KB Output is correct
39 Correct 123 ms 35504 KB Output is correct
40 Correct 113 ms 33208 KB Output is correct
41 Correct 127 ms 36008 KB Output is correct
42 Correct 125 ms 35976 KB Output is correct
43 Correct 105 ms 41168 KB Output is correct
44 Correct 129 ms 36044 KB Output is correct
45 Correct 147 ms 49224 KB Output is correct
46 Correct 128 ms 36060 KB Output is correct
47 Correct 123 ms 36184 KB Output is correct
48 Correct 130 ms 40784 KB Output is correct
49 Correct 96 ms 47608 KB Output is correct
50 Correct 80 ms 38556 KB Output is correct
51 Correct 125 ms 43620 KB Output is correct
52 Correct 176 ms 51608 KB Output is correct
53 Correct 190 ms 54344 KB Output is correct
54 Correct 201 ms 61340 KB Output is correct
55 Correct 108 ms 40752 KB Output is correct
56 Correct 187 ms 56804 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7508 KB Output is correct
2 Correct 4 ms 7508 KB Output is correct
3 Correct 4 ms 7508 KB Output is correct
4 Correct 4 ms 7636 KB Output is correct
5 Correct 4 ms 7764 KB Output is correct
6 Correct 4 ms 7764 KB Output is correct
7 Correct 5 ms 7764 KB Output is correct
8 Correct 4 ms 7764 KB Output is correct
9 Correct 99 ms 29840 KB Output is correct
10 Correct 125 ms 34936 KB Output is correct
11 Correct 139 ms 34416 KB Output is correct
12 Correct 127 ms 36052 KB Output is correct
13 Correct 102 ms 39856 KB Output is correct
14 Correct 111 ms 30020 KB Output is correct
15 Correct 295 ms 36924 KB Output is correct
16 Correct 286 ms 36080 KB Output is correct
17 Correct 274 ms 37844 KB Output is correct
18 Correct 325 ms 41660 KB Output is correct
19 Correct 297 ms 44868 KB Output is correct
20 Correct 297 ms 46672 KB Output is correct
21 Correct 306 ms 45668 KB Output is correct
22 Correct 302 ms 46384 KB Output is correct
23 Correct 342 ms 46148 KB Output is correct
24 Correct 287 ms 44640 KB Output is correct
25 Correct 349 ms 45888 KB Output is correct
26 Correct 285 ms 44292 KB Output is correct
27 Correct 4 ms 7764 KB Output is correct
28 Correct 4 ms 7764 KB Output is correct
29 Correct 5 ms 7764 KB Output is correct
30 Correct 5 ms 7764 KB Output is correct
31 Correct 4 ms 7764 KB Output is correct
32 Correct 5 ms 7808 KB Output is correct
33 Correct 4 ms 7808 KB Output is correct
34 Correct 5 ms 7864 KB Output is correct
35 Correct 4 ms 7764 KB Output is correct
36 Correct 14 ms 11348 KB Output is correct
37 Correct 129 ms 36064 KB Output is correct
38 Correct 126 ms 36040 KB Output is correct
39 Correct 126 ms 36028 KB Output is correct
40 Correct 130 ms 35660 KB Output is correct
41 Correct 123 ms 35504 KB Output is correct
42 Correct 113 ms 33208 KB Output is correct
43 Correct 127 ms 36008 KB Output is correct
44 Correct 125 ms 35976 KB Output is correct
45 Correct 105 ms 41168 KB Output is correct
46 Correct 129 ms 36044 KB Output is correct
47 Correct 21 ms 11388 KB Output is correct
48 Correct 267 ms 38636 KB Output is correct
49 Correct 264 ms 38716 KB Output is correct
50 Correct 287 ms 38484 KB Output is correct
51 Correct 262 ms 38452 KB Output is correct
52 Correct 247 ms 36924 KB Output is correct
53 Correct 227 ms 30016 KB Output is correct
54 Correct 278 ms 39160 KB Output is correct
55 Correct 263 ms 38528 KB Output is correct
56 Correct 351 ms 42596 KB Output is correct
57 Correct 266 ms 39344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7508 KB Output is correct
2 Correct 4 ms 7508 KB Output is correct
3 Correct 4 ms 7508 KB Output is correct
4 Correct 4 ms 7508 KB Output is correct
5 Correct 4 ms 7636 KB Output is correct
6 Correct 4 ms 7764 KB Output is correct
7 Correct 4 ms 7764 KB Output is correct
8 Correct 5 ms 7764 KB Output is correct
9 Correct 4 ms 7764 KB Output is correct
10 Correct 99 ms 29840 KB Output is correct
11 Correct 125 ms 34936 KB Output is correct
12 Correct 139 ms 34416 KB Output is correct
13 Correct 127 ms 36052 KB Output is correct
14 Correct 102 ms 39856 KB Output is correct
15 Correct 111 ms 30020 KB Output is correct
16 Correct 295 ms 36924 KB Output is correct
17 Correct 286 ms 36080 KB Output is correct
18 Correct 274 ms 37844 KB Output is correct
19 Correct 325 ms 41660 KB Output is correct
20 Correct 297 ms 44868 KB Output is correct
21 Correct 297 ms 46672 KB Output is correct
22 Correct 306 ms 45668 KB Output is correct
23 Correct 302 ms 46384 KB Output is correct
24 Correct 342 ms 46148 KB Output is correct
25 Correct 287 ms 44640 KB Output is correct
26 Correct 349 ms 45888 KB Output is correct
27 Correct 285 ms 44292 KB Output is correct
28 Correct 4 ms 7764 KB Output is correct
29 Correct 4 ms 7764 KB Output is correct
30 Correct 5 ms 7764 KB Output is correct
31 Correct 5 ms 7764 KB Output is correct
32 Correct 4 ms 7764 KB Output is correct
33 Correct 5 ms 7808 KB Output is correct
34 Correct 4 ms 7808 KB Output is correct
35 Correct 5 ms 7864 KB Output is correct
36 Correct 4 ms 7764 KB Output is correct
37 Correct 14 ms 11348 KB Output is correct
38 Correct 129 ms 36064 KB Output is correct
39 Correct 126 ms 36040 KB Output is correct
40 Correct 126 ms 36028 KB Output is correct
41 Correct 130 ms 35660 KB Output is correct
42 Correct 123 ms 35504 KB Output is correct
43 Correct 113 ms 33208 KB Output is correct
44 Correct 127 ms 36008 KB Output is correct
45 Correct 125 ms 35976 KB Output is correct
46 Correct 105 ms 41168 KB Output is correct
47 Correct 129 ms 36044 KB Output is correct
48 Correct 21 ms 11388 KB Output is correct
49 Correct 267 ms 38636 KB Output is correct
50 Correct 264 ms 38716 KB Output is correct
51 Correct 287 ms 38484 KB Output is correct
52 Correct 262 ms 38452 KB Output is correct
53 Correct 247 ms 36924 KB Output is correct
54 Correct 227 ms 30016 KB Output is correct
55 Correct 278 ms 39160 KB Output is correct
56 Correct 263 ms 38528 KB Output is correct
57 Correct 351 ms 42596 KB Output is correct
58 Correct 266 ms 39344 KB Output is correct
59 Correct 94 ms 15472 KB Output is correct
60 Correct 283 ms 38180 KB Output is correct
61 Correct 328 ms 37384 KB Output is correct
62 Correct 280 ms 39156 KB Output is correct
63 Correct 335 ms 43092 KB Output is correct
64 Correct 4 ms 7764 KB Output is correct
65 Correct 4 ms 7764 KB Output is correct
66 Correct 4 ms 7764 KB Output is correct
67 Correct 5 ms 7892 KB Output is correct
68 Correct 5 ms 7724 KB Output is correct
69 Correct 5 ms 7892 KB Output is correct
70 Correct 5 ms 8020 KB Output is correct
71 Correct 7 ms 7988 KB Output is correct
72 Correct 4 ms 7764 KB Output is correct
73 Correct 6 ms 8072 KB Output is correct
74 Correct 147 ms 49224 KB Output is correct
75 Correct 128 ms 36060 KB Output is correct
76 Correct 123 ms 36184 KB Output is correct
77 Correct 130 ms 40784 KB Output is correct
78 Correct 96 ms 47608 KB Output is correct
79 Correct 80 ms 38556 KB Output is correct
80 Correct 125 ms 43620 KB Output is correct
81 Correct 176 ms 51608 KB Output is correct
82 Correct 190 ms 54344 KB Output is correct
83 Correct 201 ms 61340 KB Output is correct
84 Correct 108 ms 40752 KB Output is correct
85 Correct 187 ms 56804 KB Output is correct
86 Correct 77 ms 22388 KB Output is correct
87 Correct 299 ms 38524 KB Output is correct
88 Correct 260 ms 38576 KB Output is correct
89 Correct 298 ms 41812 KB Output is correct
90 Correct 181 ms 52108 KB Output is correct
91 Correct 207 ms 48408 KB Output is correct
92 Correct 307 ms 44956 KB Output is correct
93 Correct 321 ms 53332 KB Output is correct
94 Correct 358 ms 56632 KB Output is correct
95 Correct 347 ms 62948 KB Output is correct
96 Correct 333 ms 42960 KB Output is correct
97 Correct 351 ms 49936 KB Output is correct