Submission #580773

# Submission time Handle Problem Language Result Execution time Memory
580773 2022-06-21T19:01:51 Z MohammadAghil Swapping Cities (APIO20_swap) C++17
100 / 100
701 ms 65728 KB
      #include <bits/stdc++.h>
     #include "swap.h"
//   #pragma GCC optimize ("Ofast,unroll-loops")
// #pragma GCC target ("avx2")
    using namespace std;
  typedef long long ll;
   typedef pair<int, int> pp;
    #define er(args ...) cerr << __LINE__ << ": ", err(new istringstream(string(#args)), args), cerr << endl
      #define per(i,r,l) for(int i = (r); i >= (l); i--)
        #define rep(i,l,r) for(int i = (l); i < (r); i++)
           #define all(x) x.begin(), x.end()
              #define sz(x) (int)(x).size()
                  #define pb push_back
                      #define ss second
                           #define ff first
                                   void err(istringstream *iss){}template<typename T,typename ...Args> void err(istringstream *iss,const T &_val, const Args&...args){string _name;*iss>>_name;if(_name.back()==',')_name.pop_back();cerr<<_name<<" = "<<_val<<", ",err(iss,args...);}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll mod = 1e9 + 7, maxn = 4e5 + 5, lg = 22, inf = ll(1e18) + 5;
ll pw(ll a,ll b,ll md=mod){if(!b)return 1;ll k=pw(a,b>>1ll);return k*k%md*(b&1ll?a:1)%md;}
 
struct Edge{
     int u, v, w;
};
 
int ext_path[maxn], par[maxn], br[maxn][2];
vector<int> grp[maxn];
vector<pp> adj[maxn];
 
int get(int x){
     return par[x] == -1? x: par[x] = get(par[x]);
}
 
bool unite(int u, int v, int w){
     int a = u, b = v;
     u = get(u), v = get(v);
     if(u == v){
          if(ext_path[u] == -1){
               for(int c: grp[u]) ext_path[c] = w;
          }
          return false;
     }
     if(sz(grp[u]) < sz(grp[v])) swap(u, v), swap(a, b);
     par[v] = u;
     if(ext_path[u] + 1){
          if(ext_path[v] == -1) for(int c: grp[v]) ext_path[c] = w;
     }else{
          if(ext_path[v] + 1) for(int c: grp[u]) ext_path[c] = w;
          else{
               int t1 = -1, t2 = -1;
               rep(i,0,2) if(br[u][i] == a) t1 = i;
               rep(i,0,2) if(br[v][i] == b) t2 = i;
               if(t1 == -1 || t2 == -1){
                    for(int c: grp[u]) ext_path[c] = w;
                    for(int c: grp[v]) ext_path[c] = w;
               }else{
                    br[u][t1] = br[v][t2^1];
               }
          }
     }
     for(int c: grp[v]) grp[u].pb(c); grp[v].clear();
     return true;
}
 
pp rmq[maxn][lg];
int h[maxn];
void dfs(int r, int p){
     rep(i,1,lg){
          rmq[r][i].ff = rmq[rmq[r][i-1].ff][i-1].ff;
          rmq[r][i].ss = max(rmq[r][i-1].ss, rmq[rmq[r][i-1].ff][i-1].ss);
     }
     for(auto[c, w]: adj[r]) if(c - p){
          h[c] = h[r] + 1, rmq[c][0] = {r, w};
          dfs(c, r);
     }
}
 
int lca(int u, int v){
     if(h[u] > h[v]) swap(u, v);
     per(i,lg-1,0) if(h[rmq[v][i].ff] >= h[u]) v = rmq[v][i].ff;
     if(u == v) return u;
     per(i,lg-1,0) if(rmq[u][i].ff - rmq[v][i].ff) v = rmq[v][i].ff, u = rmq[u][i].ff;
     return rmq[u][0].ff;
}
 
int query(int u, int l){
     int ans = 0;
     per(i,lg-1,0) if(h[rmq[u][i].ff] >= h[l]) ans = max(ans, rmq[u][i].ss), u = rmq[u][i].ff;
     return ans;
}
 
// int mx = -1;
 
int getMinimumFuelCapacity(int u, int v){
     if(ext_path[u] == -1) return -1;
     // return mx;
     int l = lca(u, v);
     return max({query(u, l), query(v, l), ext_path[u]});
}
 
void init(int n, int m, vector<int> u, vector<int> v, vector<int> w){
     vector<Edge> edg;
     rep(i,0,m){
          // int u, v, w; cin >> u >> v >> w;
          edg.pb({u[i], v[i], w[i]});
     } sort(all(edg), [&](Edge a, Edge b){ return a.w < b.w; });
     rep(i,0,n) par[i] = ext_path[i] = -1, br[i][0] = br[i][1] = i, grp[i].pb(i);
     for(auto[u, v, w]: edg){
          if(unite(u, v, w)) {
               // er(u, v, w);
               adj[u].pb({v, w}), adj[v].pb({u, w});
          }
     } dfs(0, 0);
}
 
// int main(){
//      cin.tie(0) -> sync_with_stdio(0);
//      int n, m; cin >> n >> m;
//      vector<Edge> edg;
//      rep(i,0,m){
//           int u, v, w; cin >> u >> v >> w;
//           edg.pb({u, v, w});
//      } sort(all(edg), [](Edge a, Edge b){ return a.w < b.w; });
//      rep(i,0,n) par[i] = ext_path[i] = -1, br[i][0] = br[i][1] = i, grp[i].pb(i);
//      for(auto[u, v, w]: edg){
//           if(unite(u, v, w)) {
//                // er(u, v, w);
//                adj[u].pb({v, w}), adj[v].pb({u, w});
//           }
//      } dfs(0, 0);
//      // rep(i,0,n) er(i, ext_path[i]);
//      int q; cin >> q;
//      while(q--){
//           int u, v; cin >> u >> v; 
//           cout << getMinimumFuelCapacity(u, v) << '\n';
//      }
//      return 0; 
// }

Compilation message

swap.cpp: In function 'bool unite(int, int, int)':
swap.cpp:60:6: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
   60 |      for(int c: grp[v]) grp[u].pb(c); grp[v].clear();
      |      ^~~
swap.cpp:60:39: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
   60 |      for(int c: grp[v]) grp[u].pb(c); grp[v].clear();
      |                                       ^~~
# Verdict Execution time Memory Grader output
1 Correct 13 ms 19028 KB Output is correct
2 Correct 14 ms 19040 KB Output is correct
3 Correct 10 ms 19028 KB Output is correct
4 Correct 10 ms 19156 KB Output is correct
5 Correct 11 ms 19412 KB Output is correct
6 Correct 11 ms 19412 KB Output is correct
7 Correct 11 ms 19444 KB Output is correct
8 Correct 11 ms 19412 KB Output is correct
9 Correct 180 ms 49656 KB Output is correct
10 Correct 189 ms 56880 KB Output is correct
11 Correct 212 ms 56084 KB Output is correct
12 Correct 194 ms 58432 KB Output is correct
13 Correct 162 ms 57220 KB Output is correct
14 Correct 179 ms 49296 KB Output is correct
15 Correct 258 ms 61152 KB Output is correct
16 Correct 260 ms 58736 KB Output is correct
17 Correct 253 ms 63908 KB Output is correct
18 Correct 228 ms 59880 KB Output is correct
19 Correct 153 ms 30284 KB Output is correct
20 Correct 481 ms 60820 KB Output is correct
21 Correct 546 ms 59128 KB Output is correct
22 Correct 559 ms 62652 KB Output is correct
23 Correct 485 ms 59988 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 19028 KB Output is correct
2 Correct 14 ms 19040 KB Output is correct
3 Correct 255 ms 49500 KB Output is correct
4 Correct 271 ms 50780 KB Output is correct
5 Correct 259 ms 50104 KB Output is correct
6 Correct 261 ms 50696 KB Output is correct
7 Correct 255 ms 50528 KB Output is correct
8 Correct 266 ms 49308 KB Output is correct
9 Correct 260 ms 50304 KB Output is correct
10 Correct 266 ms 49204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 19028 KB Output is correct
2 Correct 14 ms 19040 KB Output is correct
3 Correct 10 ms 19028 KB Output is correct
4 Correct 10 ms 19156 KB Output is correct
5 Correct 11 ms 19412 KB Output is correct
6 Correct 11 ms 19412 KB Output is correct
7 Correct 11 ms 19444 KB Output is correct
8 Correct 11 ms 19412 KB Output is correct
9 Correct 10 ms 19040 KB Output is correct
10 Correct 11 ms 19392 KB Output is correct
11 Correct 11 ms 19412 KB Output is correct
12 Correct 14 ms 19412 KB Output is correct
13 Correct 12 ms 19412 KB Output is correct
14 Correct 13 ms 19304 KB Output is correct
15 Correct 12 ms 19412 KB Output is correct
16 Correct 11 ms 19472 KB Output is correct
17 Correct 11 ms 19412 KB Output is correct
18 Correct 11 ms 19380 KB Output is correct
19 Correct 12 ms 19356 KB Output is correct
20 Correct 11 ms 19376 KB Output is correct
21 Correct 11 ms 19412 KB Output is correct
22 Correct 11 ms 19284 KB Output is correct
23 Correct 11 ms 19284 KB Output is correct
24 Correct 11 ms 19504 KB Output is correct
25 Correct 11 ms 19520 KB Output is correct
26 Correct 12 ms 19432 KB Output is correct
27 Correct 11 ms 19424 KB Output is correct
28 Correct 14 ms 19412 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 19040 KB Output is correct
2 Correct 13 ms 19028 KB Output is correct
3 Correct 14 ms 19040 KB Output is correct
4 Correct 10 ms 19028 KB Output is correct
5 Correct 10 ms 19156 KB Output is correct
6 Correct 11 ms 19412 KB Output is correct
7 Correct 11 ms 19412 KB Output is correct
8 Correct 11 ms 19444 KB Output is correct
9 Correct 11 ms 19412 KB Output is correct
10 Correct 180 ms 49656 KB Output is correct
11 Correct 189 ms 56880 KB Output is correct
12 Correct 212 ms 56084 KB Output is correct
13 Correct 194 ms 58432 KB Output is correct
14 Correct 162 ms 57220 KB Output is correct
15 Correct 11 ms 19392 KB Output is correct
16 Correct 11 ms 19412 KB Output is correct
17 Correct 14 ms 19412 KB Output is correct
18 Correct 12 ms 19412 KB Output is correct
19 Correct 13 ms 19304 KB Output is correct
20 Correct 12 ms 19412 KB Output is correct
21 Correct 11 ms 19472 KB Output is correct
22 Correct 11 ms 19412 KB Output is correct
23 Correct 11 ms 19380 KB Output is correct
24 Correct 12 ms 19356 KB Output is correct
25 Correct 11 ms 19376 KB Output is correct
26 Correct 11 ms 19412 KB Output is correct
27 Correct 11 ms 19284 KB Output is correct
28 Correct 11 ms 19284 KB Output is correct
29 Correct 11 ms 19504 KB Output is correct
30 Correct 11 ms 19520 KB Output is correct
31 Correct 12 ms 19432 KB Output is correct
32 Correct 11 ms 19424 KB Output is correct
33 Correct 14 ms 19412 KB Output is correct
34 Correct 24 ms 23700 KB Output is correct
35 Correct 195 ms 57752 KB Output is correct
36 Correct 185 ms 55580 KB Output is correct
37 Correct 201 ms 53556 KB Output is correct
38 Correct 176 ms 52640 KB Output is correct
39 Correct 156 ms 51884 KB Output is correct
40 Correct 153 ms 49340 KB Output is correct
41 Correct 190 ms 56732 KB Output is correct
42 Correct 196 ms 57448 KB Output is correct
43 Correct 171 ms 55552 KB Output is correct
44 Correct 157 ms 53032 KB Output is correct
45 Correct 148 ms 48452 KB Output is correct
46 Correct 178 ms 54696 KB Output is correct
47 Correct 158 ms 51796 KB Output is correct
48 Correct 154 ms 50848 KB Output is correct
49 Correct 76 ms 28704 KB Output is correct
50 Correct 61 ms 27672 KB Output is correct
51 Correct 116 ms 43380 KB Output is correct
52 Correct 204 ms 58104 KB Output is correct
53 Correct 193 ms 56896 KB Output is correct
54 Correct 234 ms 61912 KB Output is correct
55 Correct 158 ms 56488 KB Output is correct
56 Correct 182 ms 55480 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 19028 KB Output is correct
2 Correct 14 ms 19040 KB Output is correct
3 Correct 10 ms 19028 KB Output is correct
4 Correct 10 ms 19156 KB Output is correct
5 Correct 11 ms 19412 KB Output is correct
6 Correct 11 ms 19412 KB Output is correct
7 Correct 11 ms 19444 KB Output is correct
8 Correct 11 ms 19412 KB Output is correct
9 Correct 180 ms 49656 KB Output is correct
10 Correct 189 ms 56880 KB Output is correct
11 Correct 212 ms 56084 KB Output is correct
12 Correct 194 ms 58432 KB Output is correct
13 Correct 162 ms 57220 KB Output is correct
14 Correct 179 ms 49296 KB Output is correct
15 Correct 258 ms 61152 KB Output is correct
16 Correct 260 ms 58736 KB Output is correct
17 Correct 253 ms 63908 KB Output is correct
18 Correct 228 ms 59880 KB Output is correct
19 Correct 255 ms 49500 KB Output is correct
20 Correct 271 ms 50780 KB Output is correct
21 Correct 259 ms 50104 KB Output is correct
22 Correct 261 ms 50696 KB Output is correct
23 Correct 255 ms 50528 KB Output is correct
24 Correct 266 ms 49308 KB Output is correct
25 Correct 260 ms 50304 KB Output is correct
26 Correct 266 ms 49204 KB Output is correct
27 Correct 11 ms 19392 KB Output is correct
28 Correct 11 ms 19412 KB Output is correct
29 Correct 14 ms 19412 KB Output is correct
30 Correct 12 ms 19412 KB Output is correct
31 Correct 13 ms 19304 KB Output is correct
32 Correct 12 ms 19412 KB Output is correct
33 Correct 11 ms 19472 KB Output is correct
34 Correct 11 ms 19412 KB Output is correct
35 Correct 11 ms 19380 KB Output is correct
36 Correct 24 ms 23700 KB Output is correct
37 Correct 195 ms 57752 KB Output is correct
38 Correct 185 ms 55580 KB Output is correct
39 Correct 201 ms 53556 KB Output is correct
40 Correct 176 ms 52640 KB Output is correct
41 Correct 156 ms 51884 KB Output is correct
42 Correct 153 ms 49340 KB Output is correct
43 Correct 190 ms 56732 KB Output is correct
44 Correct 196 ms 57448 KB Output is correct
45 Correct 171 ms 55552 KB Output is correct
46 Correct 157 ms 53032 KB Output is correct
47 Correct 39 ms 23648 KB Output is correct
48 Correct 524 ms 60100 KB Output is correct
49 Correct 532 ms 58568 KB Output is correct
50 Correct 483 ms 57632 KB Output is correct
51 Correct 462 ms 56768 KB Output is correct
52 Correct 447 ms 54368 KB Output is correct
53 Correct 348 ms 46732 KB Output is correct
54 Correct 701 ms 60264 KB Output is correct
55 Correct 625 ms 61032 KB Output is correct
56 Correct 505 ms 60076 KB Output is correct
57 Correct 442 ms 57124 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 19040 KB Output is correct
2 Correct 13 ms 19028 KB Output is correct
3 Correct 14 ms 19040 KB Output is correct
4 Correct 10 ms 19028 KB Output is correct
5 Correct 10 ms 19156 KB Output is correct
6 Correct 11 ms 19412 KB Output is correct
7 Correct 11 ms 19412 KB Output is correct
8 Correct 11 ms 19444 KB Output is correct
9 Correct 11 ms 19412 KB Output is correct
10 Correct 180 ms 49656 KB Output is correct
11 Correct 189 ms 56880 KB Output is correct
12 Correct 212 ms 56084 KB Output is correct
13 Correct 194 ms 58432 KB Output is correct
14 Correct 162 ms 57220 KB Output is correct
15 Correct 179 ms 49296 KB Output is correct
16 Correct 258 ms 61152 KB Output is correct
17 Correct 260 ms 58736 KB Output is correct
18 Correct 253 ms 63908 KB Output is correct
19 Correct 228 ms 59880 KB Output is correct
20 Correct 255 ms 49500 KB Output is correct
21 Correct 271 ms 50780 KB Output is correct
22 Correct 259 ms 50104 KB Output is correct
23 Correct 261 ms 50696 KB Output is correct
24 Correct 255 ms 50528 KB Output is correct
25 Correct 266 ms 49308 KB Output is correct
26 Correct 260 ms 50304 KB Output is correct
27 Correct 266 ms 49204 KB Output is correct
28 Correct 11 ms 19392 KB Output is correct
29 Correct 11 ms 19412 KB Output is correct
30 Correct 14 ms 19412 KB Output is correct
31 Correct 12 ms 19412 KB Output is correct
32 Correct 13 ms 19304 KB Output is correct
33 Correct 12 ms 19412 KB Output is correct
34 Correct 11 ms 19472 KB Output is correct
35 Correct 11 ms 19412 KB Output is correct
36 Correct 11 ms 19380 KB Output is correct
37 Correct 24 ms 23700 KB Output is correct
38 Correct 195 ms 57752 KB Output is correct
39 Correct 185 ms 55580 KB Output is correct
40 Correct 201 ms 53556 KB Output is correct
41 Correct 176 ms 52640 KB Output is correct
42 Correct 156 ms 51884 KB Output is correct
43 Correct 153 ms 49340 KB Output is correct
44 Correct 190 ms 56732 KB Output is correct
45 Correct 196 ms 57448 KB Output is correct
46 Correct 171 ms 55552 KB Output is correct
47 Correct 157 ms 53032 KB Output is correct
48 Correct 39 ms 23648 KB Output is correct
49 Correct 524 ms 60100 KB Output is correct
50 Correct 532 ms 58568 KB Output is correct
51 Correct 483 ms 57632 KB Output is correct
52 Correct 462 ms 56768 KB Output is correct
53 Correct 447 ms 54368 KB Output is correct
54 Correct 348 ms 46732 KB Output is correct
55 Correct 701 ms 60264 KB Output is correct
56 Correct 625 ms 61032 KB Output is correct
57 Correct 505 ms 60076 KB Output is correct
58 Correct 442 ms 57124 KB Output is correct
59 Correct 153 ms 30284 KB Output is correct
60 Correct 481 ms 60820 KB Output is correct
61 Correct 546 ms 59128 KB Output is correct
62 Correct 559 ms 62652 KB Output is correct
63 Correct 485 ms 59988 KB Output is correct
64 Correct 12 ms 19356 KB Output is correct
65 Correct 11 ms 19376 KB Output is correct
66 Correct 11 ms 19412 KB Output is correct
67 Correct 11 ms 19284 KB Output is correct
68 Correct 11 ms 19284 KB Output is correct
69 Correct 11 ms 19504 KB Output is correct
70 Correct 11 ms 19520 KB Output is correct
71 Correct 12 ms 19432 KB Output is correct
72 Correct 11 ms 19424 KB Output is correct
73 Correct 14 ms 19412 KB Output is correct
74 Correct 148 ms 48452 KB Output is correct
75 Correct 178 ms 54696 KB Output is correct
76 Correct 158 ms 51796 KB Output is correct
77 Correct 154 ms 50848 KB Output is correct
78 Correct 76 ms 28704 KB Output is correct
79 Correct 61 ms 27672 KB Output is correct
80 Correct 116 ms 43380 KB Output is correct
81 Correct 204 ms 58104 KB Output is correct
82 Correct 193 ms 56896 KB Output is correct
83 Correct 234 ms 61912 KB Output is correct
84 Correct 158 ms 56488 KB Output is correct
85 Correct 182 ms 55480 KB Output is correct
86 Correct 99 ms 31204 KB Output is correct
87 Correct 598 ms 58100 KB Output is correct
88 Correct 630 ms 58136 KB Output is correct
89 Correct 377 ms 52880 KB Output is correct
90 Correct 229 ms 32564 KB Output is correct
91 Correct 261 ms 33760 KB Output is correct
92 Correct 346 ms 46904 KB Output is correct
93 Correct 592 ms 61676 KB Output is correct
94 Correct 402 ms 60604 KB Output is correct
95 Correct 608 ms 65728 KB Output is correct
96 Correct 521 ms 58292 KB Output is correct
97 Correct 409 ms 57416 KB Output is correct