Submission #348073

# Submission time Handle Problem Language Result Execution time Memory
348073 2021-01-14T06:52:42 Z arnold518 Swapping Cities (APIO20_swap) C++14
100 / 100
608 ms 56808 KB
#include "swap.h"
#include <bits/stdc++.h>
using namespace std;

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

const int MAXN = 1e5;
const int MAXM = 2e5;

int N, M;

struct Edge
{
	int u, v, w;
};
Edge E[MAXM+10];

vector<int> V[MAXN+10];
int deg[MAXN+10], val[MAXN+10], cnt[MAXN+10], sz[MAXN+10];
int ans[MAXN+10];

vector<pii> adj[MAXN+10];

struct UF
{
	int par[MAXN+10];

	void init()
	{
		for(int i=1; i<=N; i++)
		{
			par[i]=i;
		}		
	}

	int Find(int x)
	{
		if(x==par[x]) return x;
		return par[x]=Find(par[x]);
	}
}uf;

int par[MAXN+10][30], len[MAXN+10][30], dep[MAXN+10];
void dfs(int now, int bef, int d)
{
	par[now][0]=bef;
	dep[now]=d;
	for(auto nxt : adj[now])
	{
		if(nxt.first==bef) continue;
		len[nxt.first][0]=nxt.second;
		dfs(nxt.first, now, d+1);
	}
}

int lca(int u, int v)
{
	if(dep[u]>dep[v]) swap(u, v);
	int ans=0;
	for(int i=20; i>=0; i--) if(dep[par[v][i]]>=dep[u])
	{
		ans=max(ans, len[v][i]);
		v=par[v][i];
	}
	if(u==v) return ans;
	for(int i=20; i>=0; i--) if(par[u][i]!=par[v][i])
	{
		ans=max(ans, len[v][i]);
		ans=max(ans, len[u][i]);
		u=par[u][i]; v=par[v][i];
	}
	ans=max(ans, len[u][0]);
	ans=max(ans, len[v][0]);
	return ans;
}

void init(int _N, int _M, vector<int> _U, vector<int> _V, vector<int> _W)
{
	N=_N; M=_M;
	for(int i=1; i<=M; i++)
	{
		int u=_U[i-1]+1, v=_V[i-1]+1, w=_W[i-1];
		E[i]={u, v, w};
	}
	sort(E+1, E+M+1, [&](const Edge &p, const Edge &q)
	{
		return p.w<q.w;
	});

	for(int i=1; i<=N; i++) ans[i]=2e9;
	uf.init();
	for(int i=1; i<=N; i++)
	{
		V[i].push_back(i);
		deg[i]=0;
		val[i]=0;
		cnt[i]=0;
		sz[i]=1;
	}
	for(int i=1; i<=M; i++)
	{
		int u=E[i].u, v=E[i].v, w=E[i].w;
		deg[u]++; deg[v]++;

		int uu=uf.Find(u), vv=uf.Find(v);

		if(uu==vv)
		{
			val[uu]=max(val[uu], deg[u]);
			val[uu]=max(val[uu], deg[v]);
			cnt[uu]++;
		}
		else
		{
			adj[u].push_back({v, w});
			adj[v].push_back({u, w});
			if(V[uu].size()<V[vv].size()) swap(V[uu], V[vv]);
			for(auto it : V[vv]) V[uu].push_back(it);
			V[vv].clear();
			uf.par[vv]=uu;
			val[uu]=max(val[uu], val[vv]);
			cnt[uu]+=cnt[vv];
			sz[uu]+=sz[vv];
			val[uu]=max(val[uu], deg[u]);
			val[uu]=max(val[uu], deg[v]);
			cnt[uu]++;
		}

		bool t=false;
		if(val[uu]>=3) t=true;
		if(val[uu]==2 && cnt[uu]==sz[uu]) t=true;
		if(t)
		{
			for(auto it : V[uu]) ans[it]=w;
			V[uu].clear();
		}
	}

	dfs(1, 0, 1);
	for(int i=1; i<=20; i++) for(int j=1; j<=N; j++)
	{
		par[j][i]=par[par[j][i-1]][i-1];
		len[j][i]=max(len[j][i-1], len[par[j][i-1]][i-1]);
	}
}
int getMinimumFuelCapacity(int X, int Y)
{
	X++; Y++;
	int aans=lca(X, Y);
	aans=max(aans, ans[X]);
	aans=max(aans, ans[Y]);
	if(aans>=2e9) aans=-1;
	return aans;
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 4 ms 5100 KB Output is correct
3 Correct 4 ms 5100 KB Output is correct
4 Correct 4 ms 5228 KB Output is correct
5 Correct 5 ms 5484 KB Output is correct
6 Correct 4 ms 5484 KB Output is correct
7 Correct 5 ms 5484 KB Output is correct
8 Correct 5 ms 5484 KB Output is correct
9 Correct 182 ms 40640 KB Output is correct
10 Correct 234 ms 48996 KB Output is correct
11 Correct 232 ms 47840 KB Output is correct
12 Correct 246 ms 50532 KB Output is correct
13 Correct 194 ms 49504 KB Output is correct
14 Correct 213 ms 40292 KB Output is correct
15 Correct 555 ms 51184 KB Output is correct
16 Correct 550 ms 48664 KB Output is correct
17 Correct 528 ms 54040 KB Output is correct
18 Correct 498 ms 50504 KB Output is correct
19 Correct 123 ms 17260 KB Output is correct
20 Correct 541 ms 52064 KB Output is correct
21 Correct 559 ms 50116 KB Output is correct
22 Correct 581 ms 53964 KB Output is correct
23 Correct 502 ms 51656 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 4 ms 5100 KB Output is correct
3 Correct 274 ms 43376 KB Output is correct
4 Correct 270 ms 45000 KB Output is correct
5 Correct 277 ms 44064 KB Output is correct
6 Correct 267 ms 44872 KB Output is correct
7 Correct 286 ms 44512 KB Output is correct
8 Correct 277 ms 43024 KB Output is correct
9 Correct 274 ms 44256 KB Output is correct
10 Correct 268 ms 42908 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 4 ms 5100 KB Output is correct
3 Correct 4 ms 5100 KB Output is correct
4 Correct 4 ms 5100 KB Output is correct
5 Correct 4 ms 5228 KB Output is correct
6 Correct 5 ms 5484 KB Output is correct
7 Correct 4 ms 5484 KB Output is correct
8 Correct 5 ms 5484 KB Output is correct
9 Correct 5 ms 5484 KB Output is correct
10 Correct 5 ms 5484 KB Output is correct
11 Correct 5 ms 5484 KB Output is correct
12 Correct 5 ms 5484 KB Output is correct
13 Correct 4 ms 5356 KB Output is correct
14 Correct 4 ms 5356 KB Output is correct
15 Correct 5 ms 5484 KB Output is correct
16 Correct 5 ms 5484 KB Output is correct
17 Correct 4 ms 5484 KB Output is correct
18 Correct 5 ms 5484 KB Output is correct
19 Correct 5 ms 5356 KB Output is correct
20 Correct 4 ms 5484 KB Output is correct
21 Correct 4 ms 5356 KB Output is correct
22 Correct 5 ms 5228 KB Output is correct
23 Correct 4 ms 5356 KB Output is correct
24 Correct 5 ms 5484 KB Output is correct
25 Correct 5 ms 5484 KB Output is correct
26 Correct 5 ms 5484 KB Output is correct
27 Correct 5 ms 5484 KB Output is correct
28 Correct 5 ms 5484 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 4 ms 5100 KB Output is correct
3 Correct 4 ms 5100 KB Output is correct
4 Correct 4 ms 5100 KB Output is correct
5 Correct 4 ms 5228 KB Output is correct
6 Correct 5 ms 5484 KB Output is correct
7 Correct 4 ms 5484 KB Output is correct
8 Correct 5 ms 5484 KB Output is correct
9 Correct 5 ms 5484 KB Output is correct
10 Correct 182 ms 40640 KB Output is correct
11 Correct 234 ms 48996 KB Output is correct
12 Correct 232 ms 47840 KB Output is correct
13 Correct 246 ms 50532 KB Output is correct
14 Correct 194 ms 49504 KB Output is correct
15 Correct 5 ms 5484 KB Output is correct
16 Correct 5 ms 5484 KB Output is correct
17 Correct 5 ms 5484 KB Output is correct
18 Correct 4 ms 5356 KB Output is correct
19 Correct 4 ms 5356 KB Output is correct
20 Correct 5 ms 5484 KB Output is correct
21 Correct 5 ms 5484 KB Output is correct
22 Correct 4 ms 5484 KB Output is correct
23 Correct 5 ms 5484 KB Output is correct
24 Correct 19 ms 10348 KB Output is correct
25 Correct 251 ms 49772 KB Output is correct
26 Correct 232 ms 47212 KB Output is correct
27 Correct 228 ms 44908 KB Output is correct
28 Correct 227 ms 43500 KB Output is correct
29 Correct 202 ms 42604 KB Output is correct
30 Correct 183 ms 39532 KB Output is correct
31 Correct 239 ms 48740 KB Output is correct
32 Correct 235 ms 49888 KB Output is correct
33 Correct 191 ms 47716 KB Output is correct
34 Correct 207 ms 43884 KB Output is correct
35 Correct 5 ms 5356 KB Output is correct
36 Correct 4 ms 5484 KB Output is correct
37 Correct 4 ms 5356 KB Output is correct
38 Correct 5 ms 5228 KB Output is correct
39 Correct 4 ms 5356 KB Output is correct
40 Correct 5 ms 5484 KB Output is correct
41 Correct 5 ms 5484 KB Output is correct
42 Correct 5 ms 5484 KB Output is correct
43 Correct 5 ms 5484 KB Output is correct
44 Correct 5 ms 5484 KB Output is correct
45 Correct 184 ms 38124 KB Output is correct
46 Correct 237 ms 46316 KB Output is correct
47 Correct 222 ms 43372 KB Output is correct
48 Correct 210 ms 42732 KB Output is correct
49 Correct 68 ms 12908 KB Output is correct
50 Correct 61 ms 13548 KB Output is correct
51 Correct 157 ms 32472 KB Output is correct
52 Correct 284 ms 48872 KB Output is correct
53 Correct 252 ms 46956 KB Output is correct
54 Correct 281 ms 52328 KB Output is correct
55 Correct 195 ms 48492 KB Output is correct
56 Correct 243 ms 46076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 4 ms 5100 KB Output is correct
3 Correct 4 ms 5100 KB Output is correct
4 Correct 4 ms 5228 KB Output is correct
5 Correct 5 ms 5484 KB Output is correct
6 Correct 4 ms 5484 KB Output is correct
7 Correct 5 ms 5484 KB Output is correct
8 Correct 5 ms 5484 KB Output is correct
9 Correct 182 ms 40640 KB Output is correct
10 Correct 234 ms 48996 KB Output is correct
11 Correct 232 ms 47840 KB Output is correct
12 Correct 246 ms 50532 KB Output is correct
13 Correct 194 ms 49504 KB Output is correct
14 Correct 213 ms 40292 KB Output is correct
15 Correct 555 ms 51184 KB Output is correct
16 Correct 550 ms 48664 KB Output is correct
17 Correct 528 ms 54040 KB Output is correct
18 Correct 498 ms 50504 KB Output is correct
19 Correct 274 ms 43376 KB Output is correct
20 Correct 270 ms 45000 KB Output is correct
21 Correct 277 ms 44064 KB Output is correct
22 Correct 267 ms 44872 KB Output is correct
23 Correct 286 ms 44512 KB Output is correct
24 Correct 277 ms 43024 KB Output is correct
25 Correct 274 ms 44256 KB Output is correct
26 Correct 268 ms 42908 KB Output is correct
27 Correct 5 ms 5484 KB Output is correct
28 Correct 5 ms 5484 KB Output is correct
29 Correct 5 ms 5484 KB Output is correct
30 Correct 4 ms 5356 KB Output is correct
31 Correct 4 ms 5356 KB Output is correct
32 Correct 5 ms 5484 KB Output is correct
33 Correct 5 ms 5484 KB Output is correct
34 Correct 4 ms 5484 KB Output is correct
35 Correct 5 ms 5484 KB Output is correct
36 Correct 19 ms 10348 KB Output is correct
37 Correct 251 ms 49772 KB Output is correct
38 Correct 232 ms 47212 KB Output is correct
39 Correct 228 ms 44908 KB Output is correct
40 Correct 227 ms 43500 KB Output is correct
41 Correct 202 ms 42604 KB Output is correct
42 Correct 183 ms 39532 KB Output is correct
43 Correct 239 ms 48740 KB Output is correct
44 Correct 235 ms 49888 KB Output is correct
45 Correct 191 ms 47716 KB Output is correct
46 Correct 207 ms 43884 KB Output is correct
47 Correct 28 ms 10348 KB Output is correct
48 Correct 585 ms 51292 KB Output is correct
49 Correct 566 ms 49232 KB Output is correct
50 Correct 524 ms 49872 KB Output is correct
51 Correct 492 ms 48988 KB Output is correct
52 Correct 426 ms 45976 KB Output is correct
53 Correct 315 ms 36596 KB Output is correct
54 Correct 582 ms 53968 KB Output is correct
55 Correct 560 ms 54472 KB Output is correct
56 Correct 492 ms 53072 KB Output is correct
57 Correct 412 ms 49488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 4 ms 5100 KB Output is correct
3 Correct 4 ms 5100 KB Output is correct
4 Correct 4 ms 5100 KB Output is correct
5 Correct 4 ms 5228 KB Output is correct
6 Correct 5 ms 5484 KB Output is correct
7 Correct 4 ms 5484 KB Output is correct
8 Correct 5 ms 5484 KB Output is correct
9 Correct 5 ms 5484 KB Output is correct
10 Correct 182 ms 40640 KB Output is correct
11 Correct 234 ms 48996 KB Output is correct
12 Correct 232 ms 47840 KB Output is correct
13 Correct 246 ms 50532 KB Output is correct
14 Correct 194 ms 49504 KB Output is correct
15 Correct 213 ms 40292 KB Output is correct
16 Correct 555 ms 51184 KB Output is correct
17 Correct 550 ms 48664 KB Output is correct
18 Correct 528 ms 54040 KB Output is correct
19 Correct 498 ms 50504 KB Output is correct
20 Correct 274 ms 43376 KB Output is correct
21 Correct 270 ms 45000 KB Output is correct
22 Correct 277 ms 44064 KB Output is correct
23 Correct 267 ms 44872 KB Output is correct
24 Correct 286 ms 44512 KB Output is correct
25 Correct 277 ms 43024 KB Output is correct
26 Correct 274 ms 44256 KB Output is correct
27 Correct 268 ms 42908 KB Output is correct
28 Correct 5 ms 5484 KB Output is correct
29 Correct 5 ms 5484 KB Output is correct
30 Correct 5 ms 5484 KB Output is correct
31 Correct 4 ms 5356 KB Output is correct
32 Correct 4 ms 5356 KB Output is correct
33 Correct 5 ms 5484 KB Output is correct
34 Correct 5 ms 5484 KB Output is correct
35 Correct 4 ms 5484 KB Output is correct
36 Correct 5 ms 5484 KB Output is correct
37 Correct 19 ms 10348 KB Output is correct
38 Correct 251 ms 49772 KB Output is correct
39 Correct 232 ms 47212 KB Output is correct
40 Correct 228 ms 44908 KB Output is correct
41 Correct 227 ms 43500 KB Output is correct
42 Correct 202 ms 42604 KB Output is correct
43 Correct 183 ms 39532 KB Output is correct
44 Correct 239 ms 48740 KB Output is correct
45 Correct 235 ms 49888 KB Output is correct
46 Correct 191 ms 47716 KB Output is correct
47 Correct 207 ms 43884 KB Output is correct
48 Correct 28 ms 10348 KB Output is correct
49 Correct 585 ms 51292 KB Output is correct
50 Correct 566 ms 49232 KB Output is correct
51 Correct 524 ms 49872 KB Output is correct
52 Correct 492 ms 48988 KB Output is correct
53 Correct 426 ms 45976 KB Output is correct
54 Correct 315 ms 36596 KB Output is correct
55 Correct 582 ms 53968 KB Output is correct
56 Correct 560 ms 54472 KB Output is correct
57 Correct 492 ms 53072 KB Output is correct
58 Correct 412 ms 49488 KB Output is correct
59 Correct 123 ms 17260 KB Output is correct
60 Correct 541 ms 52064 KB Output is correct
61 Correct 559 ms 50116 KB Output is correct
62 Correct 581 ms 53964 KB Output is correct
63 Correct 502 ms 51656 KB Output is correct
64 Correct 5 ms 5356 KB Output is correct
65 Correct 4 ms 5484 KB Output is correct
66 Correct 4 ms 5356 KB Output is correct
67 Correct 5 ms 5228 KB Output is correct
68 Correct 4 ms 5356 KB Output is correct
69 Correct 5 ms 5484 KB Output is correct
70 Correct 5 ms 5484 KB Output is correct
71 Correct 5 ms 5484 KB Output is correct
72 Correct 5 ms 5484 KB Output is correct
73 Correct 5 ms 5484 KB Output is correct
74 Correct 184 ms 38124 KB Output is correct
75 Correct 237 ms 46316 KB Output is correct
76 Correct 222 ms 43372 KB Output is correct
77 Correct 210 ms 42732 KB Output is correct
78 Correct 68 ms 12908 KB Output is correct
79 Correct 61 ms 13548 KB Output is correct
80 Correct 157 ms 32472 KB Output is correct
81 Correct 284 ms 48872 KB Output is correct
82 Correct 252 ms 46956 KB Output is correct
83 Correct 281 ms 52328 KB Output is correct
84 Correct 195 ms 48492 KB Output is correct
85 Correct 243 ms 46076 KB Output is correct
86 Correct 90 ms 19232 KB Output is correct
87 Correct 546 ms 50896 KB Output is correct
88 Correct 553 ms 50896 KB Output is correct
89 Correct 357 ms 45448 KB Output is correct
90 Correct 176 ms 18244 KB Output is correct
91 Correct 186 ms 21264 KB Output is correct
92 Correct 318 ms 38344 KB Output is correct
93 Correct 555 ms 54056 KB Output is correct
94 Correct 430 ms 51916 KB Output is correct
95 Correct 608 ms 56808 KB Output is correct
96 Correct 517 ms 51572 KB Output is correct
97 Correct 414 ms 50348 KB Output is correct