Submission #498182

# Submission time Handle Problem Language Result Execution time Memory
498182 2021-12-24T14:19:15 Z Karliver Swapping Cities (APIO20_swap) C++17
100 / 100
515 ms 49280 KB
	
#include <bits/stdc++.h>

#define FIXED_FLOAT(x)  std::fixed <<std::setprecision(20) << (x)
#define all(v) (v).begin(), (v).end()
using namespace  std;
#define forn(i,n) for (int i = 0; i < (n); ++i)
#define rforn(i, n) for(int i = (n) - 1;i >= 0;--i)
#define sz(x) (int)x.size()
#define ff first
#define se second
#define mp make_pair
using ll = long long;
int mod = (ll)1e9 + 7;
const int INF = 1e9 + 1;

const double eps = 1e-7;

template <class T> using V = vector<T>;  
template <class T> using VV = V<V<T>>;  
template<class T, size_t SZ> using AR = array<T, SZ>;
template<class T> using PR = pair<T, T>;
template <typename XPAX>
bool ckma(XPAX &x, XPAX y) {
    return (x < y ? x = y, 1 : 0);
}
template <typename XPAX>
bool ckmi(XPAX &x, XPAX y) {
    return (x > y ? x = y, 1 : 0);
}
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
const int mxn = 1e5+2;
int n;
VV<PR<int>> g(mxn);
const int G = 20;
int P[mxn][G], F[mxn][G];
bool isfine[mxn];
int R[mxn], S[mxn];
V<AR<int, 3>> E;
V<PR<int>> todo[mxn];
int dep[mxn];
int rt(int x) {
	return R[x]== x ? x : R[x] = rt(R[x]);
}
void add(int x, int y) {
	if(!sz(todo[x]))return;
	for(auto [a, b] : todo[x]) {
		g[a].emplace_back(b,y);
		g[b].emplace_back(a, y);
	}
	todo[x].clear();
}
	
void unite(int a, int b) {
	a = rt(a), b = rt(b);
	if(S[a] < S[b])
		swap(a, b);
	S[a] += S[b];
	isfine[a] |= isfine[b];
	if(sz(todo[a]) > sz(todo[b])) {
		for(auto x : todo[b])
			todo[a].push_back(x);
	}
	else {
		for(auto x : todo[a])
			todo[b].push_back(x);
		swap(todo[a], todo[b]);
	}
	R[b] = a;
}
void dfs(int v, int p, int wei) {
	P[v][0] = p;
	for(int i = 1;i < G;++i) P[v][i] = P[P[v][i-1]][i-1];

	F[v][0] = wei;
	for(int i = 1;i < G;++i) F[v][i] = max(F[v][i-1], F[P[v][i-1]][i-1]);

	for(auto [c, w] : g[v]) {
		if(c == p)continue;
		dep[c] = dep[v]+1;
		dfs(c, v, w);
	}
}
int lca(int a, int b) {
	if(dep[a] < dep[b])
		swap(a, b);
	int k = dep[a]-dep[b];
	
	forn(i, G)if(k >> i & 1) {
		a = P[a][i];
	}
	
	if(a == b)
		return b;
	rforn(i, G) if(P[b][i] != P[a][i]) {
		a = P[a][i];
		b = P[b][i];
	}
	return P[a][0];
}
int get(int x, int k) {
	int mx = 0;
	forn(i, G) if(k >> i & 1) {
		ckma(mx, F[x][i]);
		x = P[x][i];
	}
	return mx;
}
void init(int N, int M, V<int> A, V<int> B, V<int> W) {
	n = N;
	forn(i, M) {
		E.push_back({A[i], B[i], W[i]});
	}
	sort(all(E), [&](AR<int, 3> x, AR<int, 3> y) {return x[2] < y[2];});

	forn(i, N) {
		R[i] = i;
		S[i] = 1;
		isfine[i]= false;
		
	}
	V<int> deg(n);
	for(auto [a, b, c] : E) {
		deg[a]++;
		deg[b]++;
		int ra = rt(a);
		int rb = rt(b);
		if(deg[a] > 2) {
			isfine[ra] = 1;
			add(ra, c);
		}
		if(deg[b] > 2) {
			isfine[rb] = 1;
			add(rb, c);
		}
		if(ra == rb) {
			isfine[ra] = true;
			add(ra, c);
		}
		else {
			unite(a,b);
			int wr = rt(a);
			if(isfine[wr]) {
				add(wr, c);
				g[a].emplace_back(b, c);
				g[b].emplace_back(a, c);
			}
			else
				todo[wr].push_back({a, b});
		}
	}
	
	dfs(0, 0, 0);
}

int getMinimumFuelCapacity(int x, int y) {
	if(!isfine[rt(x)] || (rt(x) != rt(y)))
		return -1;
	int ls = lca(x, y);
	//debug(ls);
	return max(get(x, dep[x]-dep[ls]),get(y, dep[y]-dep[ls]));

}
/*

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) << '\n';

}*/
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4944 KB Output is correct
2 Correct 4 ms 4944 KB Output is correct
3 Correct 4 ms 4996 KB Output is correct
4 Correct 3 ms 5072 KB Output is correct
5 Correct 4 ms 5048 KB Output is correct
6 Correct 3 ms 5072 KB Output is correct
7 Correct 3 ms 5072 KB Output is correct
8 Correct 3 ms 5032 KB Output is correct
9 Correct 49 ms 17468 KB Output is correct
10 Correct 76 ms 19380 KB Output is correct
11 Correct 77 ms 19480 KB Output is correct
12 Correct 60 ms 19976 KB Output is correct
13 Correct 50 ms 14464 KB Output is correct
14 Correct 55 ms 17648 KB Output is correct
15 Correct 126 ms 24332 KB Output is correct
16 Correct 114 ms 23220 KB Output is correct
17 Correct 132 ms 24320 KB Output is correct
18 Correct 100 ms 17852 KB Output is correct
19 Correct 105 ms 16024 KB Output is correct
20 Correct 438 ms 45620 KB Output is correct
21 Correct 385 ms 44144 KB Output is correct
22 Correct 422 ms 47216 KB Output is correct
23 Correct 368 ms 42512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4944 KB Output is correct
2 Correct 4 ms 4944 KB Output is correct
3 Correct 223 ms 35404 KB Output is correct
4 Correct 225 ms 36372 KB Output is correct
5 Correct 208 ms 35948 KB Output is correct
6 Correct 224 ms 36224 KB Output is correct
7 Correct 239 ms 36240 KB Output is correct
8 Correct 215 ms 35152 KB Output is correct
9 Correct 232 ms 35896 KB Output is correct
10 Correct 234 ms 34916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4944 KB Output is correct
2 Correct 4 ms 4944 KB Output is correct
3 Correct 4 ms 4996 KB Output is correct
4 Correct 3 ms 5072 KB Output is correct
5 Correct 4 ms 5048 KB Output is correct
6 Correct 3 ms 5072 KB Output is correct
7 Correct 3 ms 5072 KB Output is correct
8 Correct 3 ms 5032 KB Output is correct
9 Correct 3 ms 5012 KB Output is correct
10 Correct 4 ms 5200 KB Output is correct
11 Correct 4 ms 5328 KB Output is correct
12 Correct 3 ms 5200 KB Output is correct
13 Correct 3 ms 5148 KB Output is correct
14 Correct 4 ms 5156 KB Output is correct
15 Correct 4 ms 5328 KB Output is correct
16 Correct 3 ms 5284 KB Output is correct
17 Correct 3 ms 5284 KB Output is correct
18 Correct 3 ms 5200 KB Output is correct
19 Correct 3 ms 5148 KB Output is correct
20 Correct 3 ms 5284 KB Output is correct
21 Correct 3 ms 5200 KB Output is correct
22 Correct 4 ms 5156 KB Output is correct
23 Correct 5 ms 5200 KB Output is correct
24 Correct 4 ms 5288 KB Output is correct
25 Correct 6 ms 5328 KB Output is correct
26 Correct 4 ms 5328 KB Output is correct
27 Correct 4 ms 5328 KB Output is correct
28 Correct 4 ms 5328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5012 KB Output is correct
2 Correct 3 ms 4944 KB Output is correct
3 Correct 4 ms 4944 KB Output is correct
4 Correct 4 ms 4996 KB Output is correct
5 Correct 3 ms 5072 KB Output is correct
6 Correct 4 ms 5048 KB Output is correct
7 Correct 3 ms 5072 KB Output is correct
8 Correct 3 ms 5072 KB Output is correct
9 Correct 3 ms 5032 KB Output is correct
10 Correct 49 ms 17468 KB Output is correct
11 Correct 76 ms 19380 KB Output is correct
12 Correct 77 ms 19480 KB Output is correct
13 Correct 60 ms 19976 KB Output is correct
14 Correct 50 ms 14464 KB Output is correct
15 Correct 4 ms 5200 KB Output is correct
16 Correct 4 ms 5328 KB Output is correct
17 Correct 3 ms 5200 KB Output is correct
18 Correct 3 ms 5148 KB Output is correct
19 Correct 4 ms 5156 KB Output is correct
20 Correct 4 ms 5328 KB Output is correct
21 Correct 3 ms 5284 KB Output is correct
22 Correct 3 ms 5284 KB Output is correct
23 Correct 3 ms 5200 KB Output is correct
24 Correct 3 ms 5148 KB Output is correct
25 Correct 3 ms 5284 KB Output is correct
26 Correct 3 ms 5200 KB Output is correct
27 Correct 4 ms 5156 KB Output is correct
28 Correct 5 ms 5200 KB Output is correct
29 Correct 4 ms 5288 KB Output is correct
30 Correct 6 ms 5328 KB Output is correct
31 Correct 4 ms 5328 KB Output is correct
32 Correct 4 ms 5328 KB Output is correct
33 Correct 4 ms 5328 KB Output is correct
34 Correct 13 ms 8716 KB Output is correct
35 Correct 175 ms 40452 KB Output is correct
36 Correct 161 ms 37628 KB Output is correct
37 Correct 125 ms 35092 KB Output is correct
38 Correct 146 ms 33524 KB Output is correct
39 Correct 137 ms 32140 KB Output is correct
40 Correct 110 ms 29596 KB Output is correct
41 Correct 160 ms 40048 KB Output is correct
42 Correct 161 ms 40628 KB Output is correct
43 Correct 135 ms 35636 KB Output is correct
44 Correct 159 ms 32696 KB Output is correct
45 Correct 118 ms 30500 KB Output is correct
46 Correct 153 ms 37116 KB Output is correct
47 Correct 116 ms 33276 KB Output is correct
48 Correct 143 ms 32140 KB Output is correct
49 Correct 62 ms 14652 KB Output is correct
50 Correct 51 ms 13116 KB Output is correct
51 Correct 102 ms 26224 KB Output is correct
52 Correct 214 ms 40820 KB Output is correct
53 Correct 169 ms 37656 KB Output is correct
54 Correct 213 ms 45332 KB Output is correct
55 Correct 139 ms 36636 KB Output is correct
56 Correct 183 ms 36684 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4944 KB Output is correct
2 Correct 4 ms 4944 KB Output is correct
3 Correct 4 ms 4996 KB Output is correct
4 Correct 3 ms 5072 KB Output is correct
5 Correct 4 ms 5048 KB Output is correct
6 Correct 3 ms 5072 KB Output is correct
7 Correct 3 ms 5072 KB Output is correct
8 Correct 3 ms 5032 KB Output is correct
9 Correct 49 ms 17468 KB Output is correct
10 Correct 76 ms 19380 KB Output is correct
11 Correct 77 ms 19480 KB Output is correct
12 Correct 60 ms 19976 KB Output is correct
13 Correct 50 ms 14464 KB Output is correct
14 Correct 55 ms 17648 KB Output is correct
15 Correct 126 ms 24332 KB Output is correct
16 Correct 114 ms 23220 KB Output is correct
17 Correct 132 ms 24320 KB Output is correct
18 Correct 100 ms 17852 KB Output is correct
19 Correct 223 ms 35404 KB Output is correct
20 Correct 225 ms 36372 KB Output is correct
21 Correct 208 ms 35948 KB Output is correct
22 Correct 224 ms 36224 KB Output is correct
23 Correct 239 ms 36240 KB Output is correct
24 Correct 215 ms 35152 KB Output is correct
25 Correct 232 ms 35896 KB Output is correct
26 Correct 234 ms 34916 KB Output is correct
27 Correct 4 ms 5200 KB Output is correct
28 Correct 4 ms 5328 KB Output is correct
29 Correct 3 ms 5200 KB Output is correct
30 Correct 3 ms 5148 KB Output is correct
31 Correct 4 ms 5156 KB Output is correct
32 Correct 4 ms 5328 KB Output is correct
33 Correct 3 ms 5284 KB Output is correct
34 Correct 3 ms 5284 KB Output is correct
35 Correct 3 ms 5200 KB Output is correct
36 Correct 13 ms 8716 KB Output is correct
37 Correct 175 ms 40452 KB Output is correct
38 Correct 161 ms 37628 KB Output is correct
39 Correct 125 ms 35092 KB Output is correct
40 Correct 146 ms 33524 KB Output is correct
41 Correct 137 ms 32140 KB Output is correct
42 Correct 110 ms 29596 KB Output is correct
43 Correct 160 ms 40048 KB Output is correct
44 Correct 161 ms 40628 KB Output is correct
45 Correct 135 ms 35636 KB Output is correct
46 Correct 159 ms 32696 KB Output is correct
47 Correct 23 ms 8920 KB Output is correct
48 Correct 397 ms 43892 KB Output is correct
49 Correct 422 ms 41844 KB Output is correct
50 Correct 417 ms 40008 KB Output is correct
51 Correct 405 ms 38540 KB Output is correct
52 Correct 317 ms 35808 KB Output is correct
53 Correct 256 ms 29068 KB Output is correct
54 Correct 441 ms 44548 KB Output is correct
55 Correct 432 ms 45204 KB Output is correct
56 Correct 418 ms 40948 KB Output is correct
57 Correct 348 ms 38084 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5012 KB Output is correct
2 Correct 3 ms 4944 KB Output is correct
3 Correct 4 ms 4944 KB Output is correct
4 Correct 4 ms 4996 KB Output is correct
5 Correct 3 ms 5072 KB Output is correct
6 Correct 4 ms 5048 KB Output is correct
7 Correct 3 ms 5072 KB Output is correct
8 Correct 3 ms 5072 KB Output is correct
9 Correct 3 ms 5032 KB Output is correct
10 Correct 49 ms 17468 KB Output is correct
11 Correct 76 ms 19380 KB Output is correct
12 Correct 77 ms 19480 KB Output is correct
13 Correct 60 ms 19976 KB Output is correct
14 Correct 50 ms 14464 KB Output is correct
15 Correct 55 ms 17648 KB Output is correct
16 Correct 126 ms 24332 KB Output is correct
17 Correct 114 ms 23220 KB Output is correct
18 Correct 132 ms 24320 KB Output is correct
19 Correct 100 ms 17852 KB Output is correct
20 Correct 223 ms 35404 KB Output is correct
21 Correct 225 ms 36372 KB Output is correct
22 Correct 208 ms 35948 KB Output is correct
23 Correct 224 ms 36224 KB Output is correct
24 Correct 239 ms 36240 KB Output is correct
25 Correct 215 ms 35152 KB Output is correct
26 Correct 232 ms 35896 KB Output is correct
27 Correct 234 ms 34916 KB Output is correct
28 Correct 4 ms 5200 KB Output is correct
29 Correct 4 ms 5328 KB Output is correct
30 Correct 3 ms 5200 KB Output is correct
31 Correct 3 ms 5148 KB Output is correct
32 Correct 4 ms 5156 KB Output is correct
33 Correct 4 ms 5328 KB Output is correct
34 Correct 3 ms 5284 KB Output is correct
35 Correct 3 ms 5284 KB Output is correct
36 Correct 3 ms 5200 KB Output is correct
37 Correct 13 ms 8716 KB Output is correct
38 Correct 175 ms 40452 KB Output is correct
39 Correct 161 ms 37628 KB Output is correct
40 Correct 125 ms 35092 KB Output is correct
41 Correct 146 ms 33524 KB Output is correct
42 Correct 137 ms 32140 KB Output is correct
43 Correct 110 ms 29596 KB Output is correct
44 Correct 160 ms 40048 KB Output is correct
45 Correct 161 ms 40628 KB Output is correct
46 Correct 135 ms 35636 KB Output is correct
47 Correct 159 ms 32696 KB Output is correct
48 Correct 23 ms 8920 KB Output is correct
49 Correct 397 ms 43892 KB Output is correct
50 Correct 422 ms 41844 KB Output is correct
51 Correct 417 ms 40008 KB Output is correct
52 Correct 405 ms 38540 KB Output is correct
53 Correct 317 ms 35808 KB Output is correct
54 Correct 256 ms 29068 KB Output is correct
55 Correct 441 ms 44548 KB Output is correct
56 Correct 432 ms 45204 KB Output is correct
57 Correct 418 ms 40948 KB Output is correct
58 Correct 348 ms 38084 KB Output is correct
59 Correct 105 ms 16024 KB Output is correct
60 Correct 438 ms 45620 KB Output is correct
61 Correct 385 ms 44144 KB Output is correct
62 Correct 422 ms 47216 KB Output is correct
63 Correct 368 ms 42512 KB Output is correct
64 Correct 3 ms 5148 KB Output is correct
65 Correct 3 ms 5284 KB Output is correct
66 Correct 3 ms 5200 KB Output is correct
67 Correct 4 ms 5156 KB Output is correct
68 Correct 5 ms 5200 KB Output is correct
69 Correct 4 ms 5288 KB Output is correct
70 Correct 6 ms 5328 KB Output is correct
71 Correct 4 ms 5328 KB Output is correct
72 Correct 4 ms 5328 KB Output is correct
73 Correct 4 ms 5328 KB Output is correct
74 Correct 118 ms 30500 KB Output is correct
75 Correct 153 ms 37116 KB Output is correct
76 Correct 116 ms 33276 KB Output is correct
77 Correct 143 ms 32140 KB Output is correct
78 Correct 62 ms 14652 KB Output is correct
79 Correct 51 ms 13116 KB Output is correct
80 Correct 102 ms 26224 KB Output is correct
81 Correct 214 ms 40820 KB Output is correct
82 Correct 169 ms 37656 KB Output is correct
83 Correct 213 ms 45332 KB Output is correct
84 Correct 139 ms 36636 KB Output is correct
85 Correct 183 ms 36684 KB Output is correct
86 Correct 71 ms 15604 KB Output is correct
87 Correct 425 ms 41204 KB Output is correct
88 Correct 489 ms 40964 KB Output is correct
89 Correct 290 ms 35304 KB Output is correct
90 Correct 158 ms 18452 KB Output is correct
91 Correct 223 ms 19932 KB Output is correct
92 Correct 297 ms 31088 KB Output is correct
93 Correct 497 ms 45356 KB Output is correct
94 Correct 462 ms 42380 KB Output is correct
95 Correct 515 ms 49280 KB Output is correct
96 Correct 486 ms 39488 KB Output is correct
97 Correct 383 ms 39580 KB Output is correct