Submission #950146

# Submission time Handle Problem Language Result Execution time Memory
950146 2024-03-20T05:54:35 Z KiaRez Swapping Cities (APIO20_swap) C++17
100 / 100
691 ms 145088 KB
/*
    IN THE NAME OF GOD
*/
#include <bits/stdc++.h>

// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")

using namespace std;

typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef long double ld;

#define F                                      first
#define S                                      second
#define Mp                                     make_pair
#define pb                                     push_back
#define pf                                     push_front
#define size(x)                                ((ll)x.size())
#define all(x)                                 (x).begin(),(x).end()
#define kill(x)		                           cout << x << '\n', exit(0);
#define fuck(x)                                cout << "(" << #x << " , " << x << ")" << endl
#define endl                                   '\n'

const int N = 3e5+23, lg = 19;
ll Mod = 1e9+7; //998244353;

inline ll MOD(ll a, ll mod=Mod) {a%=mod; (a<0)&&(a+=mod); return a;}
inline ll poww(ll a, ll b, ll mod=Mod) {
    ll ans = 1;
    a=MOD(a, mod);
    while (b) {
        if (b & 1) ans = MOD(ans*a, mod);
        b >>= 1;
        a = MOD(a*a, mod);
    }
    return ans;
}

int n, m, h[N], ok[N], cnt, cnd[N], f[N], par[lg][N], mx[N], deg[N];
int h2[N], p2[lg][N], mx2[lg][N], mx3[lg][N];
vector<pii> alle[N], adj[N], e;
vector<int> tree[N];

int getPar(int v) {return (f[v]==0 ? v : f[v]=getPar(f[v]));}

void merge(int v, int u, int w) {
	if(getPar(v) != getPar(u)) {
		adj[v].pb({u, w});
		adj[u].pb({v, w});
	}
	deg[v]++, deg[u]++; int yey = 0;
	if(max(deg[v], deg[u]) >= 3) yey = 1;
	v=getPar(v), u=getPar(u);
	if(v == u) {
		ok[v] = 1, mx[cnd[v]] = min(mx[cnd[v]], w);
		return;
	}

	tree[cnt].pb(cnd[v]); tree[cnt].pb(cnd[u]);
	ok[v] |= ok[u];
	ok[v] |= yey;
	if(ok[v] == 1) mx[cnt] = w;
	cnd[v] = cnt;
	cnt++;
	f[u] = v;
}

void dfs_init(int v, int p=0) {
	h[v] = h[p] + 1, par[0][v] = (p==0 ? v : p);
	mx[v] = min(mx[v], mx[p]);
	for(int i=1; i<lg; i++) {
		par[i][v] = par[i-1][par[i-1][v]];
	}
	for(auto u : tree[v]) {
		//if(u == p) continue;
		dfs_init(u, v);
	}
}

int getF(int v, int dist) {
	for(int i=0; i<lg; i++) if((dist>>i)%2 == 1) {v = par[i][v];}
	return v;
}
int LCA(int v, int u) {
	if(h[v] > h[u]) swap(v, u);
	u = getF(u, h[u]-h[v]);
	if(v == u) return v;
	for(int i=lg-1; i>=0; i--) { if(par[i][v] != par[i][u]) v=par[i][v], u=par[i][u]; }
	return par[0][v];
}

int glob0, glob1;
int getF2(int v, int dist) {
	glob0 = 0, glob1 = 2e9;
	for(int i=0; i<lg; i++) if((dist>>i)%2 == 1) {
		glob0 = max(glob0, mx2[i][v]);
		glob1 = min(glob1, mx3[i][v]);
		v = p2[i][v];
	} return v;
}
int LCA2(int v, int u) {
	if(h2[v] > h2[u]) swap(v, u);
	u = getF2(u, h2[u]-h2[v]);
	if(v == u) return v;
	for(int i=lg-1; i>=0; i--) { if(p2[i][v] != p2[i][u]) v=p2[i][v], u=p2[i][u]; }
	return p2[0][v];
}

void dfs2(int v, int p=0) {
	h2[v] = h2[p] + 1, p2[0][v] = p;
	//fuck(mx2[0][v]);
	if(p==0) mx2[0][v] = 2e9;
	for(int i=1; i<lg; i++) {
		p2[i][v] = p2[i-1][p2[i-1][v]];
		mx2[i][v] = max(mx2[i-1][v], mx2[i-1][p2[i-1][v]]);
		mx3[i][v] = min(mx3[i-1][v], mx3[i-1][p2[i-1][v]]);
	}
	for(auto u : adj[v]) {
		if(u.F == p) continue;
		mx2[0][u.F] = u.S;
		dfs2(u.F, v);
	}
}

void init(int _n, int _m, vector<int> V, vector<int> U, vector<int> W) {
	n=_n, m=_m, cnt=_n+1;
	for(int i=0; i<m; i++) {
		V[i]++, U[i]++;
		e.pb({W[i], i});
		alle[V[i]].pb({W[i], U[i]});
		alle[U[i]].pb({W[i], V[i]});
	}
	fill(mx, mx+N, (int)2e9);
	for(int i=0; i<N; i++) cnd[i] = i;
	for(int i=0; i<lg; i++) mx3[i][0] = mx2[i][0] = 2e9;
	for(int i=1; i<=n; i++) {
		sort(all(alle[i]));
		mx3[0][i] = 2e9;
		if(size(alle[i]) >= 3) mx3[0][i] = alle[i][2].F;
	}
	sort(all(e));
	for(auto it : e) {
		merge(V[it.S], U[it.S], it.F);
	}
	dfs2(1);
	dfs_init(cnd[getPar(1)]);
}

int getMinimumFuelCapacity(int v, int u) {
	v++, u++;
	int lca = LCA(v, u), lca2 = LCA2(v, u);
	if(mx[lca]==2e9) return -1;
	int res0=0, res1=2e9;
	//getF2(v, h2[v]-h2[lca2]); res0 = max(res0, glob0), res1 = min(res1, glob1);
	//getF2(u, h2[u]-h2[lca2]); res0 = max(res0, glob0), res1 = min(res1, glob1);
	//res1 = min(res1, mx3[0][lca2]);
	//res0 = max(res0, res1);
	return mx[lca];
}
/*
int main() {
	init(5, 6, {0, 0, 1, 1, 1, 2}, {1, 2, 2, 3, 4, 3}, {4, 4, 1, 2, 10, 3});
	cout<<getMinimumFuelCapacity(1, 2)<<endl;
	cout<<getMinimumFuelCapacity(2, 4)<<endl;
	cout<<getMinimumFuelCapacity(0, 1)<<endl;
	init(3, 2, {0, 0}, {1, 2}, {5, 5});
	cout<<getMinimumFuelCapacity(1, 2)<<endl;
	return 0;
}
*/

Compilation message

swap.cpp: In function 'int getMinimumFuelCapacity(int, int)':
swap.cpp:155:23: warning: unused variable 'lca2' [-Wunused-variable]
  155 |  int lca = LCA(v, u), lca2 = LCA2(v, u);
      |                       ^~~~
swap.cpp:157:6: warning: unused variable 'res0' [-Wunused-variable]
  157 |  int res0=0, res1=2e9;
      |      ^~~~
swap.cpp:157:14: warning: unused variable 'res1' [-Wunused-variable]
  157 |  int res0=0, res1=2e9;
      |              ^~~~
# Verdict Execution time Memory Grader output
1 Correct 34 ms 115284 KB Output is correct
2 Correct 16 ms 115292 KB Output is correct
3 Correct 16 ms 115616 KB Output is correct
4 Correct 17 ms 115548 KB Output is correct
5 Correct 16 ms 115500 KB Output is correct
6 Correct 16 ms 115548 KB Output is correct
7 Correct 17 ms 115548 KB Output is correct
8 Correct 16 ms 115616 KB Output is correct
9 Correct 169 ms 129828 KB Output is correct
10 Correct 212 ms 133832 KB Output is correct
11 Correct 217 ms 133116 KB Output is correct
12 Correct 230 ms 134420 KB Output is correct
13 Correct 210 ms 135924 KB Output is correct
14 Correct 209 ms 129792 KB Output is correct
15 Correct 583 ms 136676 KB Output is correct
16 Correct 504 ms 135032 KB Output is correct
17 Correct 494 ms 138392 KB Output is correct
18 Correct 606 ms 137776 KB Output is correct
19 Correct 111 ms 123216 KB Output is correct
20 Correct 530 ms 137336 KB Output is correct
21 Correct 524 ms 136360 KB Output is correct
22 Correct 532 ms 138360 KB Output is correct
23 Correct 644 ms 136056 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 34 ms 115284 KB Output is correct
2 Correct 16 ms 115292 KB Output is correct
3 Correct 381 ms 136148 KB Output is correct
4 Correct 470 ms 140708 KB Output is correct
5 Correct 431 ms 140652 KB Output is correct
6 Correct 399 ms 140704 KB Output is correct
7 Correct 389 ms 140908 KB Output is correct
8 Correct 370 ms 139928 KB Output is correct
9 Correct 374 ms 140544 KB Output is correct
10 Correct 371 ms 139768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 34 ms 115284 KB Output is correct
2 Correct 16 ms 115292 KB Output is correct
3 Correct 16 ms 115616 KB Output is correct
4 Correct 17 ms 115548 KB Output is correct
5 Correct 16 ms 115500 KB Output is correct
6 Correct 16 ms 115548 KB Output is correct
7 Correct 17 ms 115548 KB Output is correct
8 Correct 16 ms 115616 KB Output is correct
9 Correct 15 ms 115292 KB Output is correct
10 Correct 17 ms 115416 KB Output is correct
11 Correct 17 ms 115804 KB Output is correct
12 Correct 17 ms 115548 KB Output is correct
13 Correct 16 ms 115544 KB Output is correct
14 Correct 16 ms 115684 KB Output is correct
15 Correct 18 ms 115544 KB Output is correct
16 Correct 17 ms 115548 KB Output is correct
17 Correct 16 ms 115684 KB Output is correct
18 Correct 16 ms 115548 KB Output is correct
19 Correct 16 ms 115548 KB Output is correct
20 Correct 16 ms 115548 KB Output is correct
21 Correct 18 ms 115672 KB Output is correct
22 Correct 17 ms 115544 KB Output is correct
23 Correct 16 ms 115548 KB Output is correct
24 Correct 17 ms 115544 KB Output is correct
25 Correct 17 ms 115548 KB Output is correct
26 Correct 17 ms 115548 KB Output is correct
27 Correct 17 ms 115548 KB Output is correct
28 Correct 17 ms 115548 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 115292 KB Output is correct
2 Correct 34 ms 115284 KB Output is correct
3 Correct 16 ms 115292 KB Output is correct
4 Correct 16 ms 115616 KB Output is correct
5 Correct 17 ms 115548 KB Output is correct
6 Correct 16 ms 115500 KB Output is correct
7 Correct 16 ms 115548 KB Output is correct
8 Correct 17 ms 115548 KB Output is correct
9 Correct 16 ms 115616 KB Output is correct
10 Correct 169 ms 129828 KB Output is correct
11 Correct 212 ms 133832 KB Output is correct
12 Correct 217 ms 133116 KB Output is correct
13 Correct 230 ms 134420 KB Output is correct
14 Correct 210 ms 135924 KB Output is correct
15 Correct 17 ms 115416 KB Output is correct
16 Correct 17 ms 115804 KB Output is correct
17 Correct 17 ms 115548 KB Output is correct
18 Correct 16 ms 115544 KB Output is correct
19 Correct 16 ms 115684 KB Output is correct
20 Correct 18 ms 115544 KB Output is correct
21 Correct 17 ms 115548 KB Output is correct
22 Correct 16 ms 115684 KB Output is correct
23 Correct 16 ms 115548 KB Output is correct
24 Correct 16 ms 115548 KB Output is correct
25 Correct 16 ms 115548 KB Output is correct
26 Correct 18 ms 115672 KB Output is correct
27 Correct 17 ms 115544 KB Output is correct
28 Correct 16 ms 115548 KB Output is correct
29 Correct 17 ms 115544 KB Output is correct
30 Correct 17 ms 115548 KB Output is correct
31 Correct 17 ms 115548 KB Output is correct
32 Correct 17 ms 115548 KB Output is correct
33 Correct 17 ms 115548 KB Output is correct
34 Correct 31 ms 117852 KB Output is correct
35 Correct 223 ms 134664 KB Output is correct
36 Correct 227 ms 132996 KB Output is correct
37 Correct 268 ms 131936 KB Output is correct
38 Correct 225 ms 131268 KB Output is correct
39 Correct 238 ms 131080 KB Output is correct
40 Correct 199 ms 130244 KB Output is correct
41 Correct 224 ms 133828 KB Output is correct
42 Correct 225 ms 134200 KB Output is correct
43 Correct 197 ms 134852 KB Output is correct
44 Correct 214 ms 132456 KB Output is correct
45 Correct 196 ms 134848 KB Output is correct
46 Correct 232 ms 132364 KB Output is correct
47 Correct 208 ms 131272 KB Output is correct
48 Correct 210 ms 133076 KB Output is correct
49 Correct 81 ms 128084 KB Output is correct
50 Correct 72 ms 125128 KB Output is correct
51 Correct 155 ms 131728 KB Output is correct
52 Correct 259 ms 137664 KB Output is correct
53 Correct 253 ms 138672 KB Output is correct
54 Correct 301 ms 141424 KB Output is correct
55 Correct 222 ms 135620 KB Output is correct
56 Correct 256 ms 138644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 34 ms 115284 KB Output is correct
2 Correct 16 ms 115292 KB Output is correct
3 Correct 16 ms 115616 KB Output is correct
4 Correct 17 ms 115548 KB Output is correct
5 Correct 16 ms 115500 KB Output is correct
6 Correct 16 ms 115548 KB Output is correct
7 Correct 17 ms 115548 KB Output is correct
8 Correct 16 ms 115616 KB Output is correct
9 Correct 169 ms 129828 KB Output is correct
10 Correct 212 ms 133832 KB Output is correct
11 Correct 217 ms 133116 KB Output is correct
12 Correct 230 ms 134420 KB Output is correct
13 Correct 210 ms 135924 KB Output is correct
14 Correct 209 ms 129792 KB Output is correct
15 Correct 583 ms 136676 KB Output is correct
16 Correct 504 ms 135032 KB Output is correct
17 Correct 494 ms 138392 KB Output is correct
18 Correct 606 ms 137776 KB Output is correct
19 Correct 381 ms 136148 KB Output is correct
20 Correct 470 ms 140708 KB Output is correct
21 Correct 431 ms 140652 KB Output is correct
22 Correct 399 ms 140704 KB Output is correct
23 Correct 389 ms 140908 KB Output is correct
24 Correct 370 ms 139928 KB Output is correct
25 Correct 374 ms 140544 KB Output is correct
26 Correct 371 ms 139768 KB Output is correct
27 Correct 17 ms 115416 KB Output is correct
28 Correct 17 ms 115804 KB Output is correct
29 Correct 17 ms 115548 KB Output is correct
30 Correct 16 ms 115544 KB Output is correct
31 Correct 16 ms 115684 KB Output is correct
32 Correct 18 ms 115544 KB Output is correct
33 Correct 17 ms 115548 KB Output is correct
34 Correct 16 ms 115684 KB Output is correct
35 Correct 16 ms 115548 KB Output is correct
36 Correct 31 ms 117852 KB Output is correct
37 Correct 223 ms 134664 KB Output is correct
38 Correct 227 ms 132996 KB Output is correct
39 Correct 268 ms 131936 KB Output is correct
40 Correct 225 ms 131268 KB Output is correct
41 Correct 238 ms 131080 KB Output is correct
42 Correct 199 ms 130244 KB Output is correct
43 Correct 224 ms 133828 KB Output is correct
44 Correct 225 ms 134200 KB Output is correct
45 Correct 197 ms 134852 KB Output is correct
46 Correct 214 ms 132456 KB Output is correct
47 Correct 42 ms 118140 KB Output is correct
48 Correct 597 ms 138404 KB Output is correct
49 Correct 589 ms 137372 KB Output is correct
50 Correct 587 ms 136756 KB Output is correct
51 Correct 640 ms 136420 KB Output is correct
52 Correct 520 ms 135384 KB Output is correct
53 Correct 408 ms 132608 KB Output is correct
54 Correct 630 ms 138648 KB Output is correct
55 Correct 562 ms 138544 KB Output is correct
56 Correct 691 ms 140188 KB Output is correct
57 Correct 521 ms 137940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 115292 KB Output is correct
2 Correct 34 ms 115284 KB Output is correct
3 Correct 16 ms 115292 KB Output is correct
4 Correct 16 ms 115616 KB Output is correct
5 Correct 17 ms 115548 KB Output is correct
6 Correct 16 ms 115500 KB Output is correct
7 Correct 16 ms 115548 KB Output is correct
8 Correct 17 ms 115548 KB Output is correct
9 Correct 16 ms 115616 KB Output is correct
10 Correct 169 ms 129828 KB Output is correct
11 Correct 212 ms 133832 KB Output is correct
12 Correct 217 ms 133116 KB Output is correct
13 Correct 230 ms 134420 KB Output is correct
14 Correct 210 ms 135924 KB Output is correct
15 Correct 209 ms 129792 KB Output is correct
16 Correct 583 ms 136676 KB Output is correct
17 Correct 504 ms 135032 KB Output is correct
18 Correct 494 ms 138392 KB Output is correct
19 Correct 606 ms 137776 KB Output is correct
20 Correct 381 ms 136148 KB Output is correct
21 Correct 470 ms 140708 KB Output is correct
22 Correct 431 ms 140652 KB Output is correct
23 Correct 399 ms 140704 KB Output is correct
24 Correct 389 ms 140908 KB Output is correct
25 Correct 370 ms 139928 KB Output is correct
26 Correct 374 ms 140544 KB Output is correct
27 Correct 371 ms 139768 KB Output is correct
28 Correct 17 ms 115416 KB Output is correct
29 Correct 17 ms 115804 KB Output is correct
30 Correct 17 ms 115548 KB Output is correct
31 Correct 16 ms 115544 KB Output is correct
32 Correct 16 ms 115684 KB Output is correct
33 Correct 18 ms 115544 KB Output is correct
34 Correct 17 ms 115548 KB Output is correct
35 Correct 16 ms 115684 KB Output is correct
36 Correct 16 ms 115548 KB Output is correct
37 Correct 31 ms 117852 KB Output is correct
38 Correct 223 ms 134664 KB Output is correct
39 Correct 227 ms 132996 KB Output is correct
40 Correct 268 ms 131936 KB Output is correct
41 Correct 225 ms 131268 KB Output is correct
42 Correct 238 ms 131080 KB Output is correct
43 Correct 199 ms 130244 KB Output is correct
44 Correct 224 ms 133828 KB Output is correct
45 Correct 225 ms 134200 KB Output is correct
46 Correct 197 ms 134852 KB Output is correct
47 Correct 214 ms 132456 KB Output is correct
48 Correct 42 ms 118140 KB Output is correct
49 Correct 597 ms 138404 KB Output is correct
50 Correct 589 ms 137372 KB Output is correct
51 Correct 587 ms 136756 KB Output is correct
52 Correct 640 ms 136420 KB Output is correct
53 Correct 520 ms 135384 KB Output is correct
54 Correct 408 ms 132608 KB Output is correct
55 Correct 630 ms 138648 KB Output is correct
56 Correct 562 ms 138544 KB Output is correct
57 Correct 691 ms 140188 KB Output is correct
58 Correct 521 ms 137940 KB Output is correct
59 Correct 111 ms 123216 KB Output is correct
60 Correct 530 ms 137336 KB Output is correct
61 Correct 524 ms 136360 KB Output is correct
62 Correct 532 ms 138360 KB Output is correct
63 Correct 644 ms 136056 KB Output is correct
64 Correct 16 ms 115548 KB Output is correct
65 Correct 16 ms 115548 KB Output is correct
66 Correct 18 ms 115672 KB Output is correct
67 Correct 17 ms 115544 KB Output is correct
68 Correct 16 ms 115548 KB Output is correct
69 Correct 17 ms 115544 KB Output is correct
70 Correct 17 ms 115548 KB Output is correct
71 Correct 17 ms 115548 KB Output is correct
72 Correct 17 ms 115548 KB Output is correct
73 Correct 17 ms 115548 KB Output is correct
74 Correct 196 ms 134848 KB Output is correct
75 Correct 232 ms 132364 KB Output is correct
76 Correct 208 ms 131272 KB Output is correct
77 Correct 210 ms 133076 KB Output is correct
78 Correct 81 ms 128084 KB Output is correct
79 Correct 72 ms 125128 KB Output is correct
80 Correct 155 ms 131728 KB Output is correct
81 Correct 259 ms 137664 KB Output is correct
82 Correct 253 ms 138672 KB Output is correct
83 Correct 301 ms 141424 KB Output is correct
84 Correct 222 ms 135620 KB Output is correct
85 Correct 256 ms 138644 KB Output is correct
86 Correct 99 ms 123520 KB Output is correct
87 Correct 582 ms 137316 KB Output is correct
88 Correct 564 ms 137468 KB Output is correct
89 Correct 428 ms 136936 KB Output is correct
90 Correct 181 ms 131692 KB Output is correct
91 Correct 188 ms 131872 KB Output is correct
92 Correct 364 ms 136484 KB Output is correct
93 Correct 587 ms 142460 KB Output is correct
94 Correct 495 ms 142956 KB Output is correct
95 Correct 601 ms 145088 KB Output is correct
96 Correct 639 ms 138940 KB Output is correct
97 Correct 495 ms 140956 KB Output is correct