Submission #547112

# Submission time Handle Problem Language Result Execution time Memory
547112 2022-04-09T14:51:55 Z cig32 Swapping Cities (APIO20_swap) C++17
100 / 100
338 ms 121124 KB
#include "bits/stdc++.h"
using namespace std;
//#define int long long
const int MAXN = 2e5 + 10;
const int MOD = 1e9 + 7;
#define ll __int128
mt19937_64 rng((int)std::chrono::steady_clock::now().time_since_epoch().count());
int rnd(int x, int y) {
  int u = uniform_int_distribution<int>(x, y)(rng); return u;
}
ll read() { // read int128
  int x; cin >> x; return (ll)x;
}
long long bm(long long b, long long p) {
  if(p==0) return 1 % MOD;
  long long r = bm(b, p >> 1);
  if(p&1) return (((r*r) % MOD) * b) % MOD;
  return (r*r) % MOD;
}
long long inv(long long b) { 
  return bm(b, MOD-2);
}
long long f[MAXN];
long long nCr(int n, int r) { 
  long long ans = f[n]; ans *= inv(f[r]); ans %= MOD;
  ans *= inv(f[n-r]); ans %= MOD; return ans;
}
void precomp() { 
  for(int i=0; i<MAXN; i++) f[i] = (i == 0 ? 1 % MOD : (f[i-1] * i) % MOD);
}
int fastlog(int x) {
  if(x == 0) return -1;
  return 32 - __builtin_clz(x) - 1;
}
void gay(int i) {
  cout << "Case #" << i << ": ";
}
#include "swap.h"
void init(int N, int M, std::vector<int> U, std::vector<int> V, std::vector<int> W);

int getMinimumFuelCapacity(int X, int Y);

int n, m;
vector<pair<int,int> > adj[500000];
struct edge {
  int f, t, w;
};
class cmp {
  public:
  bool operator() (edge x, edge y) {
    return x.w>y.w;
  }
};
vector<edge> mst;
edge e[500000];
int dsu[500000];
int set_of(int u) {
  if(dsu[u]==u) return u;
  return dsu[u]= set_of(dsu[u]);
}
void union_(int u, int v) {
  dsu[set_of(u)] = set_of(v);
}
struct node {
  vector<int> a; // children
  int par; // parenty
  int blackw, greenw; // minimum weight limit s.t. subtree is pairwise reachable
  int madeg = 0; // max degree
} krt[500000];
int rep[500000];
int root;
vector<int> euler;
int dep[500000];
int fuck[500000];
void dfs(int node, int prv) {
  euler.push_back(node);
  dep[node] = (prv == -1 ? 0 : dep[prv] + 1);
  fuck[node] = min(krt[node].greenw, (prv == -1 ? (int)2e9 : fuck[prv]));
  for(int x: krt[node].a) {
    if(x != prv) {
      dfs(x, node);
      euler.push_back(node);
    }
  }
}
int l[500000], r[500000];
pair<int,int> st[19][500000];
int lca_idx(int x, int y) {
  int m1 = min(l[x], l[y]);
  int m2 = max(r[x], r[y]);
  int k = 32 - __builtin_clz(m2-m1 + 1) - 1;
  return min(st[k][m1], st[k][m2 - (1<<k) + 1]).second;
}
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=0; i<m; i++) {
    adj[U[i]].push_back({V[i], W[i]});
    adj[V[i]].push_back({U[i], W[i]});
    e[i] = {U[i], V[i], W[i]};
  }
  for(int i=0; i<n; i++) dsu[i] = i;
  priority_queue<edge, vector<edge>, cmp> pq;
  for(int i=0; i<m; i++) pq.push(e[i]);
  int newnode = n;
  for(int i=0; i<n; i++) rep[i] = i;
  for(int i=0; i<500000; i++) krt[i].blackw = 0, krt[i].greenw = 2e9, krt[i].par = -1;
  int deg[n];
  for(int i=0; i<n; i++) deg[i] = 0;
  // Conditions: either cycle or max degree ≥ 3 (i.e. NOT chain)
  while(pq.size()) {
    deg[pq.top().f]++;
    deg[pq.top().t]++;
    if(set_of(pq.top().f) != set_of(pq.top().t)) {
      int st1 = rep[set_of(pq.top().f)];
      int st2 = rep[set_of(pq.top().t)];
      krt[newnode].a.push_back(st1);
      krt[newnode].a.push_back(st2);
      krt[st1].par = krt[st2].par = newnode;
      union_(pq.top().f, pq.top().t);
      rep[set_of(pq.top().f)] = newnode;
      krt[newnode].madeg = max({krt[st1].madeg, krt[st2].madeg, deg[pq.top().f], deg[pq.top().t]});
      krt[newnode].blackw = pq.top().w;
      if(krt[newnode].madeg >= 3 || krt[st1].greenw != 2e9 || krt[st2].greenw != 2e9) {
        krt[newnode].greenw = pq.top().w;
      }
      else {
        krt[newnode].greenw = 2e9;
      }
      newnode++;
    }
    else { // update all the way to the root, update max degree array and green weights
      int rt = rep[set_of(pq.top().f)];
      //cout << "root = " << rt << "\n";
      if(krt[rt].greenw == 2e9) { // O(component size) is ok
        int now = pq.top().f;
        unordered_map<int, int> vis;
        while(now != rt) {
          now = krt[now].par;
          vis[now] = 1;
        }
        now = pq.top().t;
        while(now != rt) {
          now = krt[now].par;
          if(vis[now]) {
            // common ancestor
            krt[now].greenw = pq.top().w;
          }
        }
      }

    }
    pq.pop();
  }
  root = newnode - 1;
  
  dfs(root, -1);
  for(int i=0; i<euler.size(); i++) r[euler[i]] = i;
  for(int i=euler.size()-1; i>=0; i--) l[euler[i]] = i;
  for(int i=0; i<euler.size(); i++) st[0][i] = {dep[euler[i]], euler[i]};
  for(int i=1; i<19; i++) {
    for(int j=0; j<euler.size(); j++) {
      if(j+(1<<i)-1 < euler.size()) st[i][j] = min(st[i-1][j], st[i-1][j+(1<<(i-1))]);
    }
  }
  /*
  
  cout << "Root = " << root << "\n";
  cout << "Euler tour = ";
  for(int x: euler) cout << x << " ";
  cout << "\n";
  for(int i=newnode - 1; i >= 0; i--) {
    cout << "node " << i << ": black = " << krt[i].blackw << ", green = " << krt[i].greenw << ", max degree = " << krt[i].madeg << "\n";
    for(int x: krt[i].a) cout << i << " " << x << "\n";

  }
  
  
  cout << "test " << min(l[2], l[4]) << " " << max(r[2], r[4]) << "\n";
  */
}
int getMinimumFuelCapacity(int X, int Y) {
  int q = lca_idx(X, Y);
  //cout << "lca = " << q << "\n";
  return (fuck[q] == 2e9 ? -1 : fuck[q]);
}

Compilation message

swap.cpp: In function 'void init(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
swap.cpp:157:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  157 |   for(int i=0; i<euler.size(); i++) r[euler[i]] = i;
      |                ~^~~~~~~~~~~~~
swap.cpp:159:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  159 |   for(int i=0; i<euler.size(); i++) st[0][i] = {dep[euler[i]], euler[i]};
      |                ~^~~~~~~~~~~~~
swap.cpp:161:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  161 |     for(int j=0; j<euler.size(); j++) {
      |                  ~^~~~~~~~~~~~~
swap.cpp:162:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  162 |       if(j+(1<<i)-1 < euler.size()) st[i][j] = min(st[i-1][j], st[i-1][j+(1<<(i-1))]);
      |          ~~~~~~~~~~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 18 ms 31700 KB Output is correct
2 Correct 19 ms 31592 KB Output is correct
3 Correct 18 ms 31656 KB Output is correct
4 Correct 19 ms 31844 KB Output is correct
5 Correct 22 ms 32264 KB Output is correct
6 Correct 18 ms 32256 KB Output is correct
7 Correct 18 ms 32280 KB Output is correct
8 Correct 18 ms 32212 KB Output is correct
9 Correct 126 ms 88560 KB Output is correct
10 Correct 150 ms 101896 KB Output is correct
11 Correct 162 ms 100824 KB Output is correct
12 Correct 168 ms 105008 KB Output is correct
13 Correct 172 ms 108072 KB Output is correct
14 Correct 128 ms 88568 KB Output is correct
15 Correct 235 ms 104056 KB Output is correct
16 Correct 225 ms 101984 KB Output is correct
17 Correct 218 ms 106564 KB Output is correct
18 Correct 205 ms 109612 KB Output is correct
19 Correct 88 ms 45272 KB Output is correct
20 Correct 220 ms 104388 KB Output is correct
21 Correct 210 ms 101324 KB Output is correct
22 Correct 246 ms 106464 KB Output is correct
23 Correct 220 ms 110972 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 31700 KB Output is correct
2 Correct 19 ms 31592 KB Output is correct
3 Correct 214 ms 108964 KB Output is correct
4 Correct 189 ms 113340 KB Output is correct
5 Correct 188 ms 110352 KB Output is correct
6 Correct 192 ms 112964 KB Output is correct
7 Correct 187 ms 111612 KB Output is correct
8 Correct 191 ms 108216 KB Output is correct
9 Correct 187 ms 111236 KB Output is correct
10 Correct 195 ms 107368 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 31700 KB Output is correct
2 Correct 19 ms 31592 KB Output is correct
3 Correct 18 ms 31656 KB Output is correct
4 Correct 19 ms 31844 KB Output is correct
5 Correct 22 ms 32264 KB Output is correct
6 Correct 18 ms 32256 KB Output is correct
7 Correct 18 ms 32280 KB Output is correct
8 Correct 18 ms 32212 KB Output is correct
9 Correct 17 ms 31700 KB Output is correct
10 Correct 20 ms 32276 KB Output is correct
11 Correct 18 ms 32268 KB Output is correct
12 Correct 18 ms 32248 KB Output is correct
13 Correct 18 ms 32096 KB Output is correct
14 Correct 18 ms 32132 KB Output is correct
15 Correct 20 ms 32300 KB Output is correct
16 Correct 19 ms 32212 KB Output is correct
17 Correct 19 ms 32212 KB Output is correct
18 Correct 22 ms 32284 KB Output is correct
19 Correct 23 ms 32288 KB Output is correct
20 Correct 17 ms 32296 KB Output is correct
21 Correct 18 ms 32220 KB Output is correct
22 Correct 19 ms 32000 KB Output is correct
23 Correct 19 ms 32188 KB Output is correct
24 Correct 18 ms 32340 KB Output is correct
25 Correct 20 ms 32376 KB Output is correct
26 Correct 19 ms 32340 KB Output is correct
27 Correct 18 ms 32320 KB Output is correct
28 Correct 20 ms 32340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 31700 KB Output is correct
2 Correct 18 ms 31700 KB Output is correct
3 Correct 19 ms 31592 KB Output is correct
4 Correct 18 ms 31656 KB Output is correct
5 Correct 19 ms 31844 KB Output is correct
6 Correct 22 ms 32264 KB Output is correct
7 Correct 18 ms 32256 KB Output is correct
8 Correct 18 ms 32280 KB Output is correct
9 Correct 18 ms 32212 KB Output is correct
10 Correct 126 ms 88560 KB Output is correct
11 Correct 150 ms 101896 KB Output is correct
12 Correct 162 ms 100824 KB Output is correct
13 Correct 168 ms 105008 KB Output is correct
14 Correct 172 ms 108072 KB Output is correct
15 Correct 20 ms 32276 KB Output is correct
16 Correct 18 ms 32268 KB Output is correct
17 Correct 18 ms 32248 KB Output is correct
18 Correct 18 ms 32096 KB Output is correct
19 Correct 18 ms 32132 KB Output is correct
20 Correct 20 ms 32300 KB Output is correct
21 Correct 19 ms 32212 KB Output is correct
22 Correct 19 ms 32212 KB Output is correct
23 Correct 22 ms 32284 KB Output is correct
24 Correct 23 ms 32288 KB Output is correct
25 Correct 17 ms 32296 KB Output is correct
26 Correct 18 ms 32220 KB Output is correct
27 Correct 19 ms 32000 KB Output is correct
28 Correct 19 ms 32188 KB Output is correct
29 Correct 18 ms 32340 KB Output is correct
30 Correct 20 ms 32376 KB Output is correct
31 Correct 19 ms 32340 KB Output is correct
32 Correct 18 ms 32320 KB Output is correct
33 Correct 20 ms 32340 KB Output is correct
34 Correct 34 ms 40460 KB Output is correct
35 Correct 168 ms 105080 KB Output is correct
36 Correct 156 ms 104892 KB Output is correct
37 Correct 178 ms 104868 KB Output is correct
38 Correct 161 ms 103824 KB Output is correct
39 Correct 163 ms 103548 KB Output is correct
40 Correct 156 ms 97528 KB Output is correct
41 Correct 175 ms 105000 KB Output is correct
42 Correct 155 ms 105008 KB Output is correct
43 Correct 152 ms 109040 KB Output is correct
44 Correct 175 ms 105504 KB Output is correct
45 Correct 181 ms 96252 KB Output is correct
46 Correct 158 ms 106816 KB Output is correct
47 Correct 178 ms 106560 KB Output is correct
48 Correct 174 ms 107552 KB Output is correct
49 Correct 99 ms 48080 KB Output is correct
50 Correct 90 ms 47132 KB Output is correct
51 Correct 158 ms 86100 KB Output is correct
52 Correct 219 ms 114044 KB Output is correct
53 Correct 240 ms 116264 KB Output is correct
54 Correct 265 ms 117180 KB Output is correct
55 Correct 138 ms 110588 KB Output is correct
56 Correct 245 ms 116848 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 31700 KB Output is correct
2 Correct 19 ms 31592 KB Output is correct
3 Correct 18 ms 31656 KB Output is correct
4 Correct 19 ms 31844 KB Output is correct
5 Correct 22 ms 32264 KB Output is correct
6 Correct 18 ms 32256 KB Output is correct
7 Correct 18 ms 32280 KB Output is correct
8 Correct 18 ms 32212 KB Output is correct
9 Correct 126 ms 88560 KB Output is correct
10 Correct 150 ms 101896 KB Output is correct
11 Correct 162 ms 100824 KB Output is correct
12 Correct 168 ms 105008 KB Output is correct
13 Correct 172 ms 108072 KB Output is correct
14 Correct 128 ms 88568 KB Output is correct
15 Correct 235 ms 104056 KB Output is correct
16 Correct 225 ms 101984 KB Output is correct
17 Correct 218 ms 106564 KB Output is correct
18 Correct 205 ms 109612 KB Output is correct
19 Correct 214 ms 108964 KB Output is correct
20 Correct 189 ms 113340 KB Output is correct
21 Correct 188 ms 110352 KB Output is correct
22 Correct 192 ms 112964 KB Output is correct
23 Correct 187 ms 111612 KB Output is correct
24 Correct 191 ms 108216 KB Output is correct
25 Correct 187 ms 111236 KB Output is correct
26 Correct 195 ms 107368 KB Output is correct
27 Correct 20 ms 32276 KB Output is correct
28 Correct 18 ms 32268 KB Output is correct
29 Correct 18 ms 32248 KB Output is correct
30 Correct 18 ms 32096 KB Output is correct
31 Correct 18 ms 32132 KB Output is correct
32 Correct 20 ms 32300 KB Output is correct
33 Correct 19 ms 32212 KB Output is correct
34 Correct 19 ms 32212 KB Output is correct
35 Correct 22 ms 32284 KB Output is correct
36 Correct 34 ms 40460 KB Output is correct
37 Correct 168 ms 105080 KB Output is correct
38 Correct 156 ms 104892 KB Output is correct
39 Correct 178 ms 104868 KB Output is correct
40 Correct 161 ms 103824 KB Output is correct
41 Correct 163 ms 103548 KB Output is correct
42 Correct 156 ms 97528 KB Output is correct
43 Correct 175 ms 105000 KB Output is correct
44 Correct 155 ms 105008 KB Output is correct
45 Correct 152 ms 109040 KB Output is correct
46 Correct 175 ms 105504 KB Output is correct
47 Correct 50 ms 39912 KB Output is correct
48 Correct 262 ms 106612 KB Output is correct
49 Correct 239 ms 106556 KB Output is correct
50 Correct 221 ms 106312 KB Output is correct
51 Correct 233 ms 106008 KB Output is correct
52 Correct 212 ms 101480 KB Output is correct
53 Correct 174 ms 81476 KB Output is correct
54 Correct 266 ms 106520 KB Output is correct
55 Correct 223 ms 106484 KB Output is correct
56 Correct 198 ms 109800 KB Output is correct
57 Correct 236 ms 107044 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 31700 KB Output is correct
2 Correct 18 ms 31700 KB Output is correct
3 Correct 19 ms 31592 KB Output is correct
4 Correct 18 ms 31656 KB Output is correct
5 Correct 19 ms 31844 KB Output is correct
6 Correct 22 ms 32264 KB Output is correct
7 Correct 18 ms 32256 KB Output is correct
8 Correct 18 ms 32280 KB Output is correct
9 Correct 18 ms 32212 KB Output is correct
10 Correct 126 ms 88560 KB Output is correct
11 Correct 150 ms 101896 KB Output is correct
12 Correct 162 ms 100824 KB Output is correct
13 Correct 168 ms 105008 KB Output is correct
14 Correct 172 ms 108072 KB Output is correct
15 Correct 128 ms 88568 KB Output is correct
16 Correct 235 ms 104056 KB Output is correct
17 Correct 225 ms 101984 KB Output is correct
18 Correct 218 ms 106564 KB Output is correct
19 Correct 205 ms 109612 KB Output is correct
20 Correct 214 ms 108964 KB Output is correct
21 Correct 189 ms 113340 KB Output is correct
22 Correct 188 ms 110352 KB Output is correct
23 Correct 192 ms 112964 KB Output is correct
24 Correct 187 ms 111612 KB Output is correct
25 Correct 191 ms 108216 KB Output is correct
26 Correct 187 ms 111236 KB Output is correct
27 Correct 195 ms 107368 KB Output is correct
28 Correct 20 ms 32276 KB Output is correct
29 Correct 18 ms 32268 KB Output is correct
30 Correct 18 ms 32248 KB Output is correct
31 Correct 18 ms 32096 KB Output is correct
32 Correct 18 ms 32132 KB Output is correct
33 Correct 20 ms 32300 KB Output is correct
34 Correct 19 ms 32212 KB Output is correct
35 Correct 19 ms 32212 KB Output is correct
36 Correct 22 ms 32284 KB Output is correct
37 Correct 34 ms 40460 KB Output is correct
38 Correct 168 ms 105080 KB Output is correct
39 Correct 156 ms 104892 KB Output is correct
40 Correct 178 ms 104868 KB Output is correct
41 Correct 161 ms 103824 KB Output is correct
42 Correct 163 ms 103548 KB Output is correct
43 Correct 156 ms 97528 KB Output is correct
44 Correct 175 ms 105000 KB Output is correct
45 Correct 155 ms 105008 KB Output is correct
46 Correct 152 ms 109040 KB Output is correct
47 Correct 175 ms 105504 KB Output is correct
48 Correct 50 ms 39912 KB Output is correct
49 Correct 262 ms 106612 KB Output is correct
50 Correct 239 ms 106556 KB Output is correct
51 Correct 221 ms 106312 KB Output is correct
52 Correct 233 ms 106008 KB Output is correct
53 Correct 212 ms 101480 KB Output is correct
54 Correct 174 ms 81476 KB Output is correct
55 Correct 266 ms 106520 KB Output is correct
56 Correct 223 ms 106484 KB Output is correct
57 Correct 198 ms 109800 KB Output is correct
58 Correct 236 ms 107044 KB Output is correct
59 Correct 88 ms 45272 KB Output is correct
60 Correct 220 ms 104388 KB Output is correct
61 Correct 210 ms 101324 KB Output is correct
62 Correct 246 ms 106464 KB Output is correct
63 Correct 220 ms 110972 KB Output is correct
64 Correct 23 ms 32288 KB Output is correct
65 Correct 17 ms 32296 KB Output is correct
66 Correct 18 ms 32220 KB Output is correct
67 Correct 19 ms 32000 KB Output is correct
68 Correct 19 ms 32188 KB Output is correct
69 Correct 18 ms 32340 KB Output is correct
70 Correct 20 ms 32376 KB Output is correct
71 Correct 19 ms 32340 KB Output is correct
72 Correct 18 ms 32320 KB Output is correct
73 Correct 20 ms 32340 KB Output is correct
74 Correct 181 ms 96252 KB Output is correct
75 Correct 158 ms 106816 KB Output is correct
76 Correct 178 ms 106560 KB Output is correct
77 Correct 174 ms 107552 KB Output is correct
78 Correct 99 ms 48080 KB Output is correct
79 Correct 90 ms 47132 KB Output is correct
80 Correct 158 ms 86100 KB Output is correct
81 Correct 219 ms 114044 KB Output is correct
82 Correct 240 ms 116264 KB Output is correct
83 Correct 265 ms 117180 KB Output is correct
84 Correct 138 ms 110588 KB Output is correct
85 Correct 245 ms 116848 KB Output is correct
86 Correct 86 ms 57044 KB Output is correct
87 Correct 227 ms 110608 KB Output is correct
88 Correct 253 ms 110684 KB Output is correct
89 Correct 217 ms 106648 KB Output is correct
90 Correct 184 ms 51012 KB Output is correct
91 Correct 181 ms 55264 KB Output is correct
92 Correct 259 ms 89620 KB Output is correct
93 Correct 274 ms 117620 KB Output is correct
94 Correct 316 ms 119888 KB Output is correct
95 Correct 338 ms 121124 KB Output is correct
96 Correct 228 ms 114096 KB Output is correct
97 Correct 292 ms 117340 KB Output is correct