Submission #674720

# Submission time Handle Problem Language Result Execution time Memory
674720 2022-12-26T01:22:53 Z vjudge1 Swapping Cities (APIO20_swap) C++17
100 / 100
221 ms 40476 KB
#ifndef memset0
  #include"swap.h"
#endif
#include<bits/stdc++.h>
const int N=2e5+10;
int n,m,pos,l[N],r[N],lnk[N],anc[N],son[N],dep[N],siz[N],fa[N],top[N],cst[N];
std::vector<int> G[N],nod[N];
std::vector<std::tuple<int,int,int>> e;
int find(int x){return anc[x]==x?x:anc[x]=find(anc[x]);}
inline void add(int u,int v){
  // printf("add %d %d\n",u,v);
  G[u].push_back(v),G[v].push_back(u);
}
void dfs(int u){
  siz[u]=1;
  for(int v:G[u])if(v!=fa[u]){
    fa[v]=u,dep[v]=dep[u]+1;
    // printf("%d -> %d\n",u,v);
    dfs(v);
    siz[u]+=siz[v];
    if(siz[v]>siz[son[u]])son[u]=v;
  }
}
void dfs(int u,int tpt){
  top[u]=tpt;
  if(siz[u]==1)return;
  dfs(son[u],tpt);
  for(int v:G[u])if(v!=fa[u]&&v!=son[u])dfs(v,v);
}
int lca(int u,int v){
  while(top[u]!=top[v]){
    if(dep[top[u]]>dep[top[v]])std::swap(u,v);
    v=fa[top[v]];
  }
  return dep[u]<dep[v]?u:v;
}
void init(int N,int M,std::vector<int> U,std::vector<int> V,std::vector<int> W){
  int u,v,w;
  n=N,m=M;
  for(int i=0;i<M;i++){
    u=U[i],v=V[i],w=W[i],++u,++v;
    e.push_back({u,v,w});
  }
  std::sort(e.begin(),e.end(),[](const std::tuple<int,int,int> &a,const std::tuple<int,int,int> &b){
    return std::get<2>(a)<std::get<2>(b);
  });
  for(int i=1;i<=n;i++){
    anc[i]=l[i]=r[i]=i,lnk[i]=1;
    nod[i].push_back(i);
  }
  int cnt=n;
  for(const auto &it:e){
    std::tie(u,v,w)=it;
    int fu=find(u),fv=find(v);
    if(fu!=fv){
      // printf("> %d %d %d\n",u,v,w);
      if(lnk[fu]&&lnk[fv]&&((l[fu]==u&&l[fv]==v)||(l[fu]==u&&r[fv]==v)||(r[fu]==u&&l[fv]==v)||(r[fu]==u&&r[fv]==v))){
        int nl,nr;
        if(l[fu]==u&&l[fv]==v)nl=r[fu],nr=r[fv];
        if(l[fu]==u&&r[fv]==v)nl=r[fu],nr=l[fv];
        if(r[fu]==u&&l[fv]==v)nl=l[fu],nr=r[fv];
        if(r[fu]==u&&r[fv]==v)nl=l[fu],nr=l[fv];
        l[fv]=nl,r[fv]=nr;
        if(nod[fu].size()>nod[fv].size()){
          std::swap(nod[fu],nod[fv]);
        }
        nod[fv].insert(nod[fv].end(),nod[fu].begin(),nod[fu].end());
        nod[fu].clear();
      }else{
        lnk[fu]=lnk[fv]=0,cst[++cnt]=w;
        // printf("new node %d %d\n",cnt,w);
        for(int x:nod[fu])add(x,cnt);
        for(int x:nod[fv])add(x,cnt);
        nod[fu].clear(),nod[fv].clear();
        nod[fv].push_back(cnt);
      }
      anc[fu]=fv;
    }else{
      if(lnk[fu]){
        lnk[fu]=0,cst[++cnt]=w;
        for(int x:nod[fu])add(x,cnt);
        nod[fu].clear(),nod[fu].push_back(cnt);
      }
    }
  }
  cst[++cnt]=-1;
  // printf("new node %d %d\n",cnt,-1);
  for(int i=1;i<=n;i++)if(find(i)==i){
    for(int x:nod[i])add(x,cnt);
  }
  dfs(cnt);
  dfs(cnt,cnt);
}
int getMinimumFuelCapacity(int u,int v){
  ++u,++v;
  // printf("lca %d %d => %d %d\n",u,v,lca(u,v),cst[lca(u,v)]);
  return cst[lca(u,v)];
}
#ifdef memset0
int main() {
  freopen("../examples/01.in","r",stdin);
  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;
}
#endif
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 6 ms 9684 KB Output is correct
3 Correct 6 ms 9684 KB Output is correct
4 Correct 8 ms 9708 KB Output is correct
5 Correct 7 ms 9844 KB Output is correct
6 Correct 6 ms 9816 KB Output is correct
7 Correct 7 ms 9844 KB Output is correct
8 Correct 7 ms 9812 KB Output is correct
9 Correct 90 ms 23036 KB Output is correct
10 Correct 125 ms 25716 KB Output is correct
11 Correct 164 ms 25508 KB Output is correct
12 Correct 110 ms 26392 KB Output is correct
13 Correct 92 ms 24748 KB Output is correct
14 Correct 79 ms 23104 KB Output is correct
15 Correct 220 ms 28200 KB Output is correct
16 Correct 206 ms 27808 KB Output is correct
17 Correct 212 ms 28808 KB Output is correct
18 Correct 172 ms 27044 KB Output is correct
19 Correct 67 ms 16540 KB Output is correct
20 Correct 179 ms 32248 KB Output is correct
21 Correct 143 ms 32012 KB Output is correct
22 Correct 169 ms 33288 KB Output is correct
23 Correct 165 ms 31512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 6 ms 9684 KB Output is correct
3 Correct 181 ms 39116 KB Output is correct
4 Correct 221 ms 40476 KB Output is correct
5 Correct 176 ms 39816 KB Output is correct
6 Correct 173 ms 40372 KB Output is correct
7 Correct 158 ms 40232 KB Output is correct
8 Correct 182 ms 39148 KB Output is correct
9 Correct 157 ms 39928 KB Output is correct
10 Correct 185 ms 38856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 6 ms 9684 KB Output is correct
3 Correct 6 ms 9684 KB Output is correct
4 Correct 8 ms 9708 KB Output is correct
5 Correct 7 ms 9844 KB Output is correct
6 Correct 6 ms 9816 KB Output is correct
7 Correct 7 ms 9844 KB Output is correct
8 Correct 7 ms 9812 KB Output is correct
9 Correct 7 ms 9700 KB Output is correct
10 Correct 7 ms 9812 KB Output is correct
11 Correct 6 ms 9844 KB Output is correct
12 Correct 6 ms 9844 KB Output is correct
13 Correct 6 ms 9840 KB Output is correct
14 Correct 8 ms 9812 KB Output is correct
15 Correct 7 ms 9844 KB Output is correct
16 Correct 6 ms 9812 KB Output is correct
17 Correct 7 ms 9968 KB Output is correct
18 Correct 7 ms 9876 KB Output is correct
19 Correct 6 ms 9896 KB Output is correct
20 Correct 6 ms 9844 KB Output is correct
21 Correct 8 ms 9940 KB Output is correct
22 Correct 7 ms 9812 KB Output is correct
23 Correct 6 ms 9812 KB Output is correct
24 Correct 6 ms 9880 KB Output is correct
25 Correct 6 ms 9972 KB Output is correct
26 Correct 7 ms 9972 KB Output is correct
27 Correct 7 ms 9940 KB Output is correct
28 Correct 7 ms 9888 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9700 KB Output is correct
2 Correct 6 ms 9684 KB Output is correct
3 Correct 6 ms 9684 KB Output is correct
4 Correct 6 ms 9684 KB Output is correct
5 Correct 8 ms 9708 KB Output is correct
6 Correct 7 ms 9844 KB Output is correct
7 Correct 6 ms 9816 KB Output is correct
8 Correct 7 ms 9844 KB Output is correct
9 Correct 7 ms 9812 KB Output is correct
10 Correct 90 ms 23036 KB Output is correct
11 Correct 125 ms 25716 KB Output is correct
12 Correct 164 ms 25508 KB Output is correct
13 Correct 110 ms 26392 KB Output is correct
14 Correct 92 ms 24748 KB Output is correct
15 Correct 7 ms 9812 KB Output is correct
16 Correct 6 ms 9844 KB Output is correct
17 Correct 6 ms 9844 KB Output is correct
18 Correct 6 ms 9840 KB Output is correct
19 Correct 8 ms 9812 KB Output is correct
20 Correct 7 ms 9844 KB Output is correct
21 Correct 6 ms 9812 KB Output is correct
22 Correct 7 ms 9968 KB Output is correct
23 Correct 7 ms 9876 KB Output is correct
24 Correct 6 ms 9896 KB Output is correct
25 Correct 6 ms 9844 KB Output is correct
26 Correct 8 ms 9940 KB Output is correct
27 Correct 7 ms 9812 KB Output is correct
28 Correct 6 ms 9812 KB Output is correct
29 Correct 6 ms 9880 KB Output is correct
30 Correct 6 ms 9972 KB Output is correct
31 Correct 7 ms 9972 KB Output is correct
32 Correct 7 ms 9940 KB Output is correct
33 Correct 7 ms 9888 KB Output is correct
34 Correct 16 ms 12084 KB Output is correct
35 Correct 88 ms 26968 KB Output is correct
36 Correct 80 ms 26684 KB Output is correct
37 Correct 103 ms 26228 KB Output is correct
38 Correct 92 ms 25664 KB Output is correct
39 Correct 101 ms 25872 KB Output is correct
40 Correct 95 ms 25132 KB Output is correct
41 Correct 85 ms 27472 KB Output is correct
42 Correct 85 ms 27204 KB Output is correct
43 Correct 119 ms 34804 KB Output is correct
44 Correct 155 ms 27652 KB Output is correct
45 Correct 122 ms 29804 KB Output is correct
46 Correct 98 ms 26720 KB Output is correct
47 Correct 86 ms 25944 KB Output is correct
48 Correct 115 ms 29592 KB Output is correct
49 Correct 75 ms 19336 KB Output is correct
50 Correct 56 ms 17360 KB Output is correct
51 Correct 103 ms 26656 KB Output is correct
52 Correct 124 ms 30524 KB Output is correct
53 Correct 182 ms 33776 KB Output is correct
54 Correct 126 ms 32884 KB Output is correct
55 Correct 106 ms 34260 KB Output is correct
56 Correct 144 ms 34364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 6 ms 9684 KB Output is correct
3 Correct 6 ms 9684 KB Output is correct
4 Correct 8 ms 9708 KB Output is correct
5 Correct 7 ms 9844 KB Output is correct
6 Correct 6 ms 9816 KB Output is correct
7 Correct 7 ms 9844 KB Output is correct
8 Correct 7 ms 9812 KB Output is correct
9 Correct 90 ms 23036 KB Output is correct
10 Correct 125 ms 25716 KB Output is correct
11 Correct 164 ms 25508 KB Output is correct
12 Correct 110 ms 26392 KB Output is correct
13 Correct 92 ms 24748 KB Output is correct
14 Correct 79 ms 23104 KB Output is correct
15 Correct 220 ms 28200 KB Output is correct
16 Correct 206 ms 27808 KB Output is correct
17 Correct 212 ms 28808 KB Output is correct
18 Correct 172 ms 27044 KB Output is correct
19 Correct 181 ms 39116 KB Output is correct
20 Correct 221 ms 40476 KB Output is correct
21 Correct 176 ms 39816 KB Output is correct
22 Correct 173 ms 40372 KB Output is correct
23 Correct 158 ms 40232 KB Output is correct
24 Correct 182 ms 39148 KB Output is correct
25 Correct 157 ms 39928 KB Output is correct
26 Correct 185 ms 38856 KB Output is correct
27 Correct 7 ms 9812 KB Output is correct
28 Correct 6 ms 9844 KB Output is correct
29 Correct 6 ms 9844 KB Output is correct
30 Correct 6 ms 9840 KB Output is correct
31 Correct 8 ms 9812 KB Output is correct
32 Correct 7 ms 9844 KB Output is correct
33 Correct 6 ms 9812 KB Output is correct
34 Correct 7 ms 9968 KB Output is correct
35 Correct 7 ms 9876 KB Output is correct
36 Correct 16 ms 12084 KB Output is correct
37 Correct 88 ms 26968 KB Output is correct
38 Correct 80 ms 26684 KB Output is correct
39 Correct 103 ms 26228 KB Output is correct
40 Correct 92 ms 25664 KB Output is correct
41 Correct 101 ms 25872 KB Output is correct
42 Correct 95 ms 25132 KB Output is correct
43 Correct 85 ms 27472 KB Output is correct
44 Correct 85 ms 27204 KB Output is correct
45 Correct 119 ms 34804 KB Output is correct
46 Correct 155 ms 27652 KB Output is correct
47 Correct 25 ms 12428 KB Output is correct
48 Correct 145 ms 31936 KB Output is correct
49 Correct 144 ms 31400 KB Output is correct
50 Correct 144 ms 31292 KB Output is correct
51 Correct 168 ms 30684 KB Output is correct
52 Correct 153 ms 29936 KB Output is correct
53 Correct 138 ms 27476 KB Output is correct
54 Correct 154 ms 32868 KB Output is correct
55 Correct 139 ms 32220 KB Output is correct
56 Correct 172 ms 38620 KB Output is correct
57 Correct 207 ms 33132 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9700 KB Output is correct
2 Correct 6 ms 9684 KB Output is correct
3 Correct 6 ms 9684 KB Output is correct
4 Correct 6 ms 9684 KB Output is correct
5 Correct 8 ms 9708 KB Output is correct
6 Correct 7 ms 9844 KB Output is correct
7 Correct 6 ms 9816 KB Output is correct
8 Correct 7 ms 9844 KB Output is correct
9 Correct 7 ms 9812 KB Output is correct
10 Correct 90 ms 23036 KB Output is correct
11 Correct 125 ms 25716 KB Output is correct
12 Correct 164 ms 25508 KB Output is correct
13 Correct 110 ms 26392 KB Output is correct
14 Correct 92 ms 24748 KB Output is correct
15 Correct 79 ms 23104 KB Output is correct
16 Correct 220 ms 28200 KB Output is correct
17 Correct 206 ms 27808 KB Output is correct
18 Correct 212 ms 28808 KB Output is correct
19 Correct 172 ms 27044 KB Output is correct
20 Correct 181 ms 39116 KB Output is correct
21 Correct 221 ms 40476 KB Output is correct
22 Correct 176 ms 39816 KB Output is correct
23 Correct 173 ms 40372 KB Output is correct
24 Correct 158 ms 40232 KB Output is correct
25 Correct 182 ms 39148 KB Output is correct
26 Correct 157 ms 39928 KB Output is correct
27 Correct 185 ms 38856 KB Output is correct
28 Correct 7 ms 9812 KB Output is correct
29 Correct 6 ms 9844 KB Output is correct
30 Correct 6 ms 9844 KB Output is correct
31 Correct 6 ms 9840 KB Output is correct
32 Correct 8 ms 9812 KB Output is correct
33 Correct 7 ms 9844 KB Output is correct
34 Correct 6 ms 9812 KB Output is correct
35 Correct 7 ms 9968 KB Output is correct
36 Correct 7 ms 9876 KB Output is correct
37 Correct 16 ms 12084 KB Output is correct
38 Correct 88 ms 26968 KB Output is correct
39 Correct 80 ms 26684 KB Output is correct
40 Correct 103 ms 26228 KB Output is correct
41 Correct 92 ms 25664 KB Output is correct
42 Correct 101 ms 25872 KB Output is correct
43 Correct 95 ms 25132 KB Output is correct
44 Correct 85 ms 27472 KB Output is correct
45 Correct 85 ms 27204 KB Output is correct
46 Correct 119 ms 34804 KB Output is correct
47 Correct 155 ms 27652 KB Output is correct
48 Correct 25 ms 12428 KB Output is correct
49 Correct 145 ms 31936 KB Output is correct
50 Correct 144 ms 31400 KB Output is correct
51 Correct 144 ms 31292 KB Output is correct
52 Correct 168 ms 30684 KB Output is correct
53 Correct 153 ms 29936 KB Output is correct
54 Correct 138 ms 27476 KB Output is correct
55 Correct 154 ms 32868 KB Output is correct
56 Correct 139 ms 32220 KB Output is correct
57 Correct 172 ms 38620 KB Output is correct
58 Correct 207 ms 33132 KB Output is correct
59 Correct 67 ms 16540 KB Output is correct
60 Correct 179 ms 32248 KB Output is correct
61 Correct 143 ms 32012 KB Output is correct
62 Correct 169 ms 33288 KB Output is correct
63 Correct 165 ms 31512 KB Output is correct
64 Correct 6 ms 9896 KB Output is correct
65 Correct 6 ms 9844 KB Output is correct
66 Correct 8 ms 9940 KB Output is correct
67 Correct 7 ms 9812 KB Output is correct
68 Correct 6 ms 9812 KB Output is correct
69 Correct 6 ms 9880 KB Output is correct
70 Correct 6 ms 9972 KB Output is correct
71 Correct 7 ms 9972 KB Output is correct
72 Correct 7 ms 9940 KB Output is correct
73 Correct 7 ms 9888 KB Output is correct
74 Correct 122 ms 29804 KB Output is correct
75 Correct 98 ms 26720 KB Output is correct
76 Correct 86 ms 25944 KB Output is correct
77 Correct 115 ms 29592 KB Output is correct
78 Correct 75 ms 19336 KB Output is correct
79 Correct 56 ms 17360 KB Output is correct
80 Correct 103 ms 26656 KB Output is correct
81 Correct 124 ms 30524 KB Output is correct
82 Correct 182 ms 33776 KB Output is correct
83 Correct 126 ms 32884 KB Output is correct
84 Correct 106 ms 34260 KB Output is correct
85 Correct 144 ms 34364 KB Output is correct
86 Correct 76 ms 18164 KB Output is correct
87 Correct 158 ms 31428 KB Output is correct
88 Correct 144 ms 31288 KB Output is correct
89 Correct 182 ms 33572 KB Output is correct
90 Correct 124 ms 23148 KB Output is correct
91 Correct 130 ms 24032 KB Output is correct
92 Correct 204 ms 31456 KB Output is correct
93 Correct 175 ms 34880 KB Output is correct
94 Correct 218 ms 37440 KB Output is correct
95 Correct 186 ms 36796 KB Output is correct
96 Correct 161 ms 38916 KB Output is correct
97 Correct 198 ms 36988 KB Output is correct