Submission #292943

# Submission time Handle Problem Language Result Execution time Memory
292943 2020-09-07T14:55:47 Z arnold518 Swapping Cities (APIO20_swap) C++14
100 / 100
765 ms 59632 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;

struct Edge
{
	int u, v, w;
};

int N, M;
vector<pii> adj[MAXN+10];
Edge E[MAXM+10];

int deg[MAXN+10];
int par[MAXN+10], maxd[MAXN+10], sz[MAXN+10], ecnt[MAXN+10];
vector<pii> adj2[MAXN+10];
int T[MAXN+10];

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

vector<int> V[MAXN+10];

void Union(int x, int y)
{
	x=Find(x); y=Find(y);
	if(x==y) return;
	if(sz[x]>sz[y]) swap(x, y);
	for(auto it : V[x]) V[y].push_back(it);
	V[x].clear();
	par[x]=y;
	maxd[y]=max(maxd[y], maxd[x]);
	sz[y]+=sz[x];
	ecnt[y]+=ecnt[x];
}

int ppar[MAXN+10][30], dep[MAXN+10], spa[MAXN+10][30];

void dfs(int now, int bef, int d, int val)
{
	dep[now]=d;
	ppar[now][0]=bef;
	spa[now][0]=val;
	for(auto nxt : adj2[now])
	{
		if(nxt.first==bef) continue;
		dfs(nxt.first, now, d+1, nxt.second);
	}
}

int maxdist(int u, int v)
{
	int ans=0;
	if(dep[u]>dep[v]) swap(u, v);
	for(int i=20; i>=0; i--) if(dep[ppar[v][i]]>=dep[u])
	{
		ans=max(ans, spa[v][i]);
		v=ppar[v][i];
	}
	if(u==v) return ans;
	for(int i=20; i>=0; i--) if(ppar[u][i]!=ppar[v][i])
	{
		ans=max(ans, spa[u][i]);
		ans=max(ans, spa[v][i]);
		u=ppar[u][i]; v=ppar[v][i];
	}
	ans=max(ans, spa[u][0]);
	ans=max(ans, spa[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=0; i<M; i++)
	{
		int u=_U[i]+1, v=_V[i]+1, w=_W[i];
		adj[u].push_back({v, w});
		adj[v].push_back({u, w});
		E[i+1]={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++)
	{
		deg[i]=0;
		maxd[i]=0;
		par[i]=i;
		sz[i]=1; ecnt[i]=0;
		T[i]=1e9;
		V[i].push_back(i);
	}

	for(int i=1; i<=M; i++)
	{
		int u=E[i].u, v=E[i].v, w=E[i].w;
		deg[u]++; deg[v]++;
		if(Find(u)!=Find(v))
		{
			//printf("%d %d %d\n", u, v, w);
			adj2[u].push_back({v, w});
			adj2[v].push_back({u, w});
			Union(u, v);
		}

		ecnt[Find(u)]++;
		maxd[Find(u)]=max(maxd[Find(u)], max(deg[u], deg[v]));

		if(maxd[Find(u)]>=3 || (maxd[Find(u)]==2 && ecnt[Find(u)]==sz[Find(u)]))
		{
			for(auto it : V[Find(u)]) T[it]=E[i].w;
			V[Find(u)].clear();
		}
		//if(Find(X)==Find(Y) && maxd[Find(X)]==2 && ecnt[Find(X)]==sz[Find(X)]) { ans=E[i].w; break; }
	}
	dfs(1, 1, 1, 0);

	for(int i=1; i<=20; i++) for(int j=1; j<=N; j++)
	{
		ppar[j][i]=ppar[ppar[j][i-1]][i-1];
		spa[j][i]=max(spa[j][i-1], spa[ppar[j][i-1]][i-1]);
	}

	//for(int i=1; i<=N; i++) printf("T %d : %d\n", i, T[i]);
}

int X, Y;

int getMinimumFuelCapacity(int _X, int _Y)
{
	X=_X+1; Y=_Y+1;

	int ans=maxdist(X, Y);
	ans=max(ans, T[X]);
	ans=max(ans, T[Y]);
	if(ans>=1e9) ans=-1;
	return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7424 KB Output is correct
2 Correct 5 ms 7424 KB Output is correct
3 Correct 5 ms 7424 KB Output is correct
4 Correct 6 ms 7644 KB Output is correct
5 Correct 7 ms 7808 KB Output is correct
6 Correct 6 ms 7808 KB Output is correct
7 Correct 6 ms 7808 KB Output is correct
8 Correct 6 ms 7808 KB Output is correct
9 Correct 222 ms 42860 KB Output is correct
10 Correct 284 ms 51696 KB Output is correct
11 Correct 274 ms 50412 KB Output is correct
12 Correct 314 ms 53404 KB Output is correct
13 Correct 236 ms 51824 KB Output is correct
14 Correct 255 ms 42868 KB Output is correct
15 Correct 644 ms 53752 KB Output is correct
16 Correct 638 ms 51620 KB Output is correct
17 Correct 628 ms 56268 KB Output is correct
18 Correct 583 ms 52692 KB Output is correct
19 Correct 150 ms 18168 KB Output is correct
20 Correct 623 ms 54508 KB Output is correct
21 Correct 625 ms 52660 KB Output is correct
22 Correct 668 ms 56148 KB Output is correct
23 Correct 593 ms 53824 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7424 KB Output is correct
2 Correct 5 ms 7424 KB Output is correct
3 Correct 312 ms 48980 KB Output is correct
4 Correct 316 ms 50904 KB Output is correct
5 Correct 342 ms 49796 KB Output is correct
6 Correct 312 ms 50712 KB Output is correct
7 Correct 329 ms 50412 KB Output is correct
8 Correct 343 ms 48668 KB Output is correct
9 Correct 348 ms 50036 KB Output is correct
10 Correct 322 ms 48296 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 7424 KB Output is correct
2 Correct 5 ms 7424 KB Output is correct
3 Correct 5 ms 7424 KB Output is correct
4 Correct 5 ms 7424 KB Output is correct
5 Correct 6 ms 7644 KB Output is correct
6 Correct 7 ms 7808 KB Output is correct
7 Correct 6 ms 7808 KB Output is correct
8 Correct 6 ms 7808 KB Output is correct
9 Correct 6 ms 7808 KB Output is correct
10 Correct 6 ms 7808 KB Output is correct
11 Correct 7 ms 7808 KB Output is correct
12 Correct 8 ms 7808 KB Output is correct
13 Correct 7 ms 7680 KB Output is correct
14 Correct 7 ms 7680 KB Output is correct
15 Correct 7 ms 7808 KB Output is correct
16 Correct 6 ms 7808 KB Output is correct
17 Correct 7 ms 7808 KB Output is correct
18 Correct 7 ms 7808 KB Output is correct
19 Correct 7 ms 7680 KB Output is correct
20 Correct 7 ms 7808 KB Output is correct
21 Correct 7 ms 7808 KB Output is correct
22 Correct 7 ms 7552 KB Output is correct
23 Correct 7 ms 7680 KB Output is correct
24 Correct 8 ms 7808 KB Output is correct
25 Correct 8 ms 7808 KB Output is correct
26 Correct 8 ms 7936 KB Output is correct
27 Correct 7 ms 7808 KB Output is correct
28 Correct 7 ms 7808 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 7424 KB Output is correct
2 Correct 5 ms 7424 KB Output is correct
3 Correct 5 ms 7424 KB Output is correct
4 Correct 5 ms 7424 KB Output is correct
5 Correct 6 ms 7644 KB Output is correct
6 Correct 7 ms 7808 KB Output is correct
7 Correct 6 ms 7808 KB Output is correct
8 Correct 6 ms 7808 KB Output is correct
9 Correct 6 ms 7808 KB Output is correct
10 Correct 222 ms 42860 KB Output is correct
11 Correct 284 ms 51696 KB Output is correct
12 Correct 274 ms 50412 KB Output is correct
13 Correct 314 ms 53404 KB Output is correct
14 Correct 236 ms 51824 KB Output is correct
15 Correct 6 ms 7808 KB Output is correct
16 Correct 7 ms 7808 KB Output is correct
17 Correct 8 ms 7808 KB Output is correct
18 Correct 7 ms 7680 KB Output is correct
19 Correct 7 ms 7680 KB Output is correct
20 Correct 7 ms 7808 KB Output is correct
21 Correct 6 ms 7808 KB Output is correct
22 Correct 7 ms 7808 KB Output is correct
23 Correct 7 ms 7808 KB Output is correct
24 Correct 25 ms 12928 KB Output is correct
25 Correct 300 ms 52344 KB Output is correct
26 Correct 331 ms 50544 KB Output is correct
27 Correct 313 ms 48552 KB Output is correct
28 Correct 290 ms 47224 KB Output is correct
29 Correct 286 ms 46328 KB Output is correct
30 Correct 253 ms 43180 KB Output is correct
31 Correct 299 ms 51764 KB Output is correct
32 Correct 297 ms 52828 KB Output is correct
33 Correct 259 ms 50228 KB Output is correct
34 Correct 264 ms 47736 KB Output is correct
35 Correct 7 ms 7680 KB Output is correct
36 Correct 7 ms 7808 KB Output is correct
37 Correct 7 ms 7808 KB Output is correct
38 Correct 7 ms 7552 KB Output is correct
39 Correct 7 ms 7680 KB Output is correct
40 Correct 8 ms 7808 KB Output is correct
41 Correct 8 ms 7808 KB Output is correct
42 Correct 8 ms 7936 KB Output is correct
43 Correct 7 ms 7808 KB Output is correct
44 Correct 7 ms 7808 KB Output is correct
45 Correct 259 ms 42832 KB Output is correct
46 Correct 296 ms 49952 KB Output is correct
47 Correct 302 ms 47224 KB Output is correct
48 Correct 291 ms 46760 KB Output is correct
49 Correct 107 ms 18132 KB Output is correct
50 Correct 112 ms 17048 KB Output is correct
51 Correct 246 ms 36644 KB Output is correct
52 Correct 382 ms 53840 KB Output is correct
53 Correct 366 ms 52472 KB Output is correct
54 Correct 412 ms 57816 KB Output is correct
55 Correct 254 ms 50940 KB Output is correct
56 Correct 339 ms 51576 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7424 KB Output is correct
2 Correct 5 ms 7424 KB Output is correct
3 Correct 5 ms 7424 KB Output is correct
4 Correct 6 ms 7644 KB Output is correct
5 Correct 7 ms 7808 KB Output is correct
6 Correct 6 ms 7808 KB Output is correct
7 Correct 6 ms 7808 KB Output is correct
8 Correct 6 ms 7808 KB Output is correct
9 Correct 222 ms 42860 KB Output is correct
10 Correct 284 ms 51696 KB Output is correct
11 Correct 274 ms 50412 KB Output is correct
12 Correct 314 ms 53404 KB Output is correct
13 Correct 236 ms 51824 KB Output is correct
14 Correct 255 ms 42868 KB Output is correct
15 Correct 644 ms 53752 KB Output is correct
16 Correct 638 ms 51620 KB Output is correct
17 Correct 628 ms 56268 KB Output is correct
18 Correct 583 ms 52692 KB Output is correct
19 Correct 312 ms 48980 KB Output is correct
20 Correct 316 ms 50904 KB Output is correct
21 Correct 342 ms 49796 KB Output is correct
22 Correct 312 ms 50712 KB Output is correct
23 Correct 329 ms 50412 KB Output is correct
24 Correct 343 ms 48668 KB Output is correct
25 Correct 348 ms 50036 KB Output is correct
26 Correct 322 ms 48296 KB Output is correct
27 Correct 6 ms 7808 KB Output is correct
28 Correct 7 ms 7808 KB Output is correct
29 Correct 8 ms 7808 KB Output is correct
30 Correct 7 ms 7680 KB Output is correct
31 Correct 7 ms 7680 KB Output is correct
32 Correct 7 ms 7808 KB Output is correct
33 Correct 6 ms 7808 KB Output is correct
34 Correct 7 ms 7808 KB Output is correct
35 Correct 7 ms 7808 KB Output is correct
36 Correct 25 ms 12928 KB Output is correct
37 Correct 300 ms 52344 KB Output is correct
38 Correct 331 ms 50544 KB Output is correct
39 Correct 313 ms 48552 KB Output is correct
40 Correct 290 ms 47224 KB Output is correct
41 Correct 286 ms 46328 KB Output is correct
42 Correct 253 ms 43180 KB Output is correct
43 Correct 299 ms 51764 KB Output is correct
44 Correct 297 ms 52828 KB Output is correct
45 Correct 259 ms 50228 KB Output is correct
46 Correct 264 ms 47736 KB Output is correct
47 Correct 38 ms 12792 KB Output is correct
48 Correct 716 ms 54172 KB Output is correct
49 Correct 737 ms 52324 KB Output is correct
50 Correct 674 ms 51220 KB Output is correct
51 Correct 594 ms 50212 KB Output is correct
52 Correct 511 ms 47232 KB Output is correct
53 Correct 390 ms 37680 KB Output is correct
54 Correct 683 ms 54492 KB Output is correct
55 Correct 651 ms 55076 KB Output is correct
56 Correct 594 ms 53160 KB Output is correct
57 Correct 487 ms 50908 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 7424 KB Output is correct
2 Correct 5 ms 7424 KB Output is correct
3 Correct 5 ms 7424 KB Output is correct
4 Correct 5 ms 7424 KB Output is correct
5 Correct 6 ms 7644 KB Output is correct
6 Correct 7 ms 7808 KB Output is correct
7 Correct 6 ms 7808 KB Output is correct
8 Correct 6 ms 7808 KB Output is correct
9 Correct 6 ms 7808 KB Output is correct
10 Correct 222 ms 42860 KB Output is correct
11 Correct 284 ms 51696 KB Output is correct
12 Correct 274 ms 50412 KB Output is correct
13 Correct 314 ms 53404 KB Output is correct
14 Correct 236 ms 51824 KB Output is correct
15 Correct 255 ms 42868 KB Output is correct
16 Correct 644 ms 53752 KB Output is correct
17 Correct 638 ms 51620 KB Output is correct
18 Correct 628 ms 56268 KB Output is correct
19 Correct 583 ms 52692 KB Output is correct
20 Correct 312 ms 48980 KB Output is correct
21 Correct 316 ms 50904 KB Output is correct
22 Correct 342 ms 49796 KB Output is correct
23 Correct 312 ms 50712 KB Output is correct
24 Correct 329 ms 50412 KB Output is correct
25 Correct 343 ms 48668 KB Output is correct
26 Correct 348 ms 50036 KB Output is correct
27 Correct 322 ms 48296 KB Output is correct
28 Correct 6 ms 7808 KB Output is correct
29 Correct 7 ms 7808 KB Output is correct
30 Correct 8 ms 7808 KB Output is correct
31 Correct 7 ms 7680 KB Output is correct
32 Correct 7 ms 7680 KB Output is correct
33 Correct 7 ms 7808 KB Output is correct
34 Correct 6 ms 7808 KB Output is correct
35 Correct 7 ms 7808 KB Output is correct
36 Correct 7 ms 7808 KB Output is correct
37 Correct 25 ms 12928 KB Output is correct
38 Correct 300 ms 52344 KB Output is correct
39 Correct 331 ms 50544 KB Output is correct
40 Correct 313 ms 48552 KB Output is correct
41 Correct 290 ms 47224 KB Output is correct
42 Correct 286 ms 46328 KB Output is correct
43 Correct 253 ms 43180 KB Output is correct
44 Correct 299 ms 51764 KB Output is correct
45 Correct 297 ms 52828 KB Output is correct
46 Correct 259 ms 50228 KB Output is correct
47 Correct 264 ms 47736 KB Output is correct
48 Correct 38 ms 12792 KB Output is correct
49 Correct 716 ms 54172 KB Output is correct
50 Correct 737 ms 52324 KB Output is correct
51 Correct 674 ms 51220 KB Output is correct
52 Correct 594 ms 50212 KB Output is correct
53 Correct 511 ms 47232 KB Output is correct
54 Correct 390 ms 37680 KB Output is correct
55 Correct 683 ms 54492 KB Output is correct
56 Correct 651 ms 55076 KB Output is correct
57 Correct 594 ms 53160 KB Output is correct
58 Correct 487 ms 50908 KB Output is correct
59 Correct 150 ms 18168 KB Output is correct
60 Correct 623 ms 54508 KB Output is correct
61 Correct 625 ms 52660 KB Output is correct
62 Correct 668 ms 56148 KB Output is correct
63 Correct 593 ms 53824 KB Output is correct
64 Correct 7 ms 7680 KB Output is correct
65 Correct 7 ms 7808 KB Output is correct
66 Correct 7 ms 7808 KB Output is correct
67 Correct 7 ms 7552 KB Output is correct
68 Correct 7 ms 7680 KB Output is correct
69 Correct 8 ms 7808 KB Output is correct
70 Correct 8 ms 7808 KB Output is correct
71 Correct 8 ms 7936 KB Output is correct
72 Correct 7 ms 7808 KB Output is correct
73 Correct 7 ms 7808 KB Output is correct
74 Correct 259 ms 42832 KB Output is correct
75 Correct 296 ms 49952 KB Output is correct
76 Correct 302 ms 47224 KB Output is correct
77 Correct 291 ms 46760 KB Output is correct
78 Correct 107 ms 18132 KB Output is correct
79 Correct 112 ms 17048 KB Output is correct
80 Correct 246 ms 36644 KB Output is correct
81 Correct 382 ms 53840 KB Output is correct
82 Correct 366 ms 52472 KB Output is correct
83 Correct 412 ms 57816 KB Output is correct
84 Correct 254 ms 50940 KB Output is correct
85 Correct 339 ms 51576 KB Output is correct
86 Correct 112 ms 21548 KB Output is correct
87 Correct 626 ms 52216 KB Output is correct
88 Correct 619 ms 51932 KB Output is correct
89 Correct 426 ms 47000 KB Output is correct
90 Correct 214 ms 19792 KB Output is correct
91 Correct 238 ms 22300 KB Output is correct
92 Correct 394 ms 39384 KB Output is correct
93 Correct 691 ms 56112 KB Output is correct
94 Correct 548 ms 54872 KB Output is correct
95 Correct 765 ms 59632 KB Output is correct
96 Correct 639 ms 52060 KB Output is correct
97 Correct 512 ms 52408 KB Output is correct