Submission #290559

# Submission time Handle Problem Language Result Execution time Memory
290559 2020-09-04T03:39:20 Z Lawliet Swapping Cities (APIO20_swap) C++17
100 / 100
295 ms 21496 KB
#include "swap.h"
#include <bits/stdc++.h>

using namespace std;

const int MAXN = 200010;
const int INF = 1000000010;

int n, m;

int w[MAXN];
int ord[MAXN];
int minTime[MAXN];
int anc[MAXN], tW[MAXN], degree[MAXN];

vector<int> nodes[MAXN];

bool cmp(int i, int j) { return w[i] < w[j]; }

int find(int cur)
{
	if( anc[cur] == cur ) return cur;
	return find( anc[cur] );
}

void update(int i, int curW)
{
	if( curW == INF ) return;
	if( minTime[i] != INF ) return;

	for(int k = 0 ; k < (int)nodes[i].size() ; k++)
		minTime[ nodes[i][k] ] = curW;
}

void join(int i, int j, int curW)
{
	degree[i]++; degree[j]++;
	bool flip = ( degree[i] >= 3 || degree[j] >= 3 );

	i = find(i), j = find(j);

	if( i == j ) flip = true;

	if( flip )
	{
		update( i , curW );
		update( j , curW );
	}

	if( i == j ) return;

	bool isSpecial = ( minTime[i] != INF || minTime[j] != INF );

	if( isSpecial )
	{
		update( i , curW );
		update( j , curW );
	}

	if( (int)nodes[i].size() < (int)nodes[j].size() )
		swap( i , j );

	anc[j] = i;
	tW[j] = curW;

	while( !nodes[j].empty() )
	{
		nodes[i].push_back( nodes[j].back() );
		nodes[j].pop_back();
	}
}

void init(int N, int M, vector<int> U, vector<int> V, vector<int> W) 
{
	n = N; m = M;

	for(int i = 0 ; i < n ; i++)
	{
		anc[i] = i;
		nodes[i].push_back( i );

		minTime[i] = tW[i] = INF;
	}

	for(int i = 0 ; i < m ; i++)
		w[i] = W[i];

	iota( ord , ord + m , 0 );
	sort( ord , ord + m , cmp );

	for(int i = 0 ; i < m ; i++)
		join( U[ ord[i] ] , V[ ord[i] ] , W[ ord[i] ] );
}

int getMinimumFuelCapacity(int X, int Y) 
{
	if( minTime[X] == INF ) return -1;

	int ans = minTime[X];

	while( X != Y )
	{
		if( tW[X] > tW[Y] )
			swap( X , Y );

		ans = max( ans , tW[X] );
		X = anc[X];
	}

	return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4992 KB Output is correct
2 Correct 3 ms 4992 KB Output is correct
3 Correct 4 ms 4992 KB Output is correct
4 Correct 4 ms 5120 KB Output is correct
5 Correct 4 ms 5120 KB Output is correct
6 Correct 4 ms 5120 KB Output is correct
7 Correct 5 ms 5120 KB Output is correct
8 Correct 5 ms 5120 KB Output is correct
9 Correct 84 ms 14260 KB Output is correct
10 Correct 111 ms 15988 KB Output is correct
11 Correct 107 ms 15728 KB Output is correct
12 Correct 116 ms 16500 KB Output is correct
13 Correct 86 ms 13748 KB Output is correct
14 Correct 97 ms 14500 KB Output is correct
15 Correct 184 ms 18040 KB Output is correct
16 Correct 185 ms 17700 KB Output is correct
17 Correct 194 ms 18392 KB Output is correct
18 Correct 164 ms 15476 KB Output is correct
19 Correct 91 ms 10232 KB Output is correct
20 Correct 205 ms 19024 KB Output is correct
21 Correct 200 ms 19120 KB Output is correct
22 Correct 229 ms 19928 KB Output is correct
23 Correct 172 ms 16856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4992 KB Output is correct
2 Correct 3 ms 4992 KB Output is correct
3 Correct 166 ms 16024 KB Output is correct
4 Correct 166 ms 16224 KB Output is correct
5 Correct 166 ms 16268 KB Output is correct
6 Correct 162 ms 16092 KB Output is correct
7 Correct 173 ms 16400 KB Output is correct
8 Correct 161 ms 16032 KB Output is correct
9 Correct 165 ms 16112 KB Output is correct
10 Correct 162 ms 16076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4992 KB Output is correct
2 Correct 4 ms 4992 KB Output is correct
3 Correct 3 ms 4992 KB Output is correct
4 Correct 4 ms 4992 KB Output is correct
5 Correct 4 ms 5120 KB Output is correct
6 Correct 4 ms 5120 KB Output is correct
7 Correct 4 ms 5120 KB Output is correct
8 Correct 5 ms 5120 KB Output is correct
9 Correct 5 ms 5120 KB Output is correct
10 Correct 4 ms 5120 KB Output is correct
11 Correct 4 ms 5120 KB Output is correct
12 Correct 4 ms 5120 KB Output is correct
13 Correct 4 ms 5120 KB Output is correct
14 Correct 4 ms 5120 KB Output is correct
15 Correct 4 ms 5248 KB Output is correct
16 Correct 4 ms 5120 KB Output is correct
17 Correct 4 ms 5120 KB Output is correct
18 Correct 4 ms 5120 KB Output is correct
19 Correct 4 ms 5120 KB Output is correct
20 Correct 4 ms 5120 KB Output is correct
21 Correct 4 ms 5120 KB Output is correct
22 Correct 5 ms 5120 KB Output is correct
23 Correct 4 ms 5120 KB Output is correct
24 Correct 5 ms 5120 KB Output is correct
25 Correct 5 ms 5120 KB Output is correct
26 Correct 5 ms 5120 KB Output is correct
27 Correct 4 ms 5120 KB Output is correct
28 Correct 5 ms 5248 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4992 KB Output is correct
2 Correct 4 ms 4992 KB Output is correct
3 Correct 3 ms 4992 KB Output is correct
4 Correct 4 ms 4992 KB Output is correct
5 Correct 4 ms 5120 KB Output is correct
6 Correct 4 ms 5120 KB Output is correct
7 Correct 4 ms 5120 KB Output is correct
8 Correct 5 ms 5120 KB Output is correct
9 Correct 5 ms 5120 KB Output is correct
10 Correct 84 ms 14260 KB Output is correct
11 Correct 111 ms 15988 KB Output is correct
12 Correct 107 ms 15728 KB Output is correct
13 Correct 116 ms 16500 KB Output is correct
14 Correct 86 ms 13748 KB Output is correct
15 Correct 4 ms 5120 KB Output is correct
16 Correct 4 ms 5120 KB Output is correct
17 Correct 4 ms 5120 KB Output is correct
18 Correct 4 ms 5120 KB Output is correct
19 Correct 4 ms 5120 KB Output is correct
20 Correct 4 ms 5248 KB Output is correct
21 Correct 4 ms 5120 KB Output is correct
22 Correct 4 ms 5120 KB Output is correct
23 Correct 4 ms 5120 KB Output is correct
24 Correct 14 ms 6400 KB Output is correct
25 Correct 109 ms 16584 KB Output is correct
26 Correct 107 ms 16500 KB Output is correct
27 Correct 105 ms 15992 KB Output is correct
28 Correct 104 ms 15604 KB Output is correct
29 Correct 104 ms 15328 KB Output is correct
30 Correct 94 ms 14584 KB Output is correct
31 Correct 110 ms 16496 KB Output is correct
32 Correct 108 ms 16624 KB Output is correct
33 Correct 93 ms 13692 KB Output is correct
34 Correct 115 ms 15348 KB Output is correct
35 Correct 4 ms 5120 KB Output is correct
36 Correct 4 ms 5120 KB Output is correct
37 Correct 4 ms 5120 KB Output is correct
38 Correct 5 ms 5120 KB Output is correct
39 Correct 4 ms 5120 KB Output is correct
40 Correct 5 ms 5120 KB Output is correct
41 Correct 5 ms 5120 KB Output is correct
42 Correct 5 ms 5120 KB Output is correct
43 Correct 4 ms 5120 KB Output is correct
44 Correct 5 ms 5248 KB Output is correct
45 Correct 131 ms 14324 KB Output is correct
46 Correct 111 ms 16368 KB Output is correct
47 Correct 108 ms 14836 KB Output is correct
48 Correct 109 ms 14168 KB Output is correct
49 Correct 101 ms 10104 KB Output is correct
50 Correct 79 ms 9340 KB Output is correct
51 Correct 107 ms 12664 KB Output is correct
52 Correct 162 ms 17908 KB Output is correct
53 Correct 178 ms 16888 KB Output is correct
54 Correct 182 ms 19700 KB Output is correct
55 Correct 90 ms 13684 KB Output is correct
56 Correct 161 ms 16244 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4992 KB Output is correct
2 Correct 3 ms 4992 KB Output is correct
3 Correct 4 ms 4992 KB Output is correct
4 Correct 4 ms 5120 KB Output is correct
5 Correct 4 ms 5120 KB Output is correct
6 Correct 4 ms 5120 KB Output is correct
7 Correct 5 ms 5120 KB Output is correct
8 Correct 5 ms 5120 KB Output is correct
9 Correct 84 ms 14260 KB Output is correct
10 Correct 111 ms 15988 KB Output is correct
11 Correct 107 ms 15728 KB Output is correct
12 Correct 116 ms 16500 KB Output is correct
13 Correct 86 ms 13748 KB Output is correct
14 Correct 97 ms 14500 KB Output is correct
15 Correct 184 ms 18040 KB Output is correct
16 Correct 185 ms 17700 KB Output is correct
17 Correct 194 ms 18392 KB Output is correct
18 Correct 164 ms 15476 KB Output is correct
19 Correct 166 ms 16024 KB Output is correct
20 Correct 166 ms 16224 KB Output is correct
21 Correct 166 ms 16268 KB Output is correct
22 Correct 162 ms 16092 KB Output is correct
23 Correct 173 ms 16400 KB Output is correct
24 Correct 161 ms 16032 KB Output is correct
25 Correct 165 ms 16112 KB Output is correct
26 Correct 162 ms 16076 KB Output is correct
27 Correct 4 ms 5120 KB Output is correct
28 Correct 4 ms 5120 KB Output is correct
29 Correct 4 ms 5120 KB Output is correct
30 Correct 4 ms 5120 KB Output is correct
31 Correct 4 ms 5120 KB Output is correct
32 Correct 4 ms 5248 KB Output is correct
33 Correct 4 ms 5120 KB Output is correct
34 Correct 4 ms 5120 KB Output is correct
35 Correct 4 ms 5120 KB Output is correct
36 Correct 14 ms 6400 KB Output is correct
37 Correct 109 ms 16584 KB Output is correct
38 Correct 107 ms 16500 KB Output is correct
39 Correct 105 ms 15992 KB Output is correct
40 Correct 104 ms 15604 KB Output is correct
41 Correct 104 ms 15328 KB Output is correct
42 Correct 94 ms 14584 KB Output is correct
43 Correct 110 ms 16496 KB Output is correct
44 Correct 108 ms 16624 KB Output is correct
45 Correct 93 ms 13692 KB Output is correct
46 Correct 115 ms 15348 KB Output is correct
47 Correct 23 ms 6656 KB Output is correct
48 Correct 220 ms 19032 KB Output is correct
49 Correct 214 ms 18652 KB Output is correct
50 Correct 209 ms 18724 KB Output is correct
51 Correct 211 ms 18280 KB Output is correct
52 Correct 207 ms 17616 KB Output is correct
53 Correct 164 ms 15204 KB Output is correct
54 Correct 217 ms 19704 KB Output is correct
55 Correct 218 ms 19416 KB Output is correct
56 Correct 182 ms 16320 KB Output is correct
57 Correct 213 ms 18392 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4992 KB Output is correct
2 Correct 4 ms 4992 KB Output is correct
3 Correct 3 ms 4992 KB Output is correct
4 Correct 4 ms 4992 KB Output is correct
5 Correct 4 ms 5120 KB Output is correct
6 Correct 4 ms 5120 KB Output is correct
7 Correct 4 ms 5120 KB Output is correct
8 Correct 5 ms 5120 KB Output is correct
9 Correct 5 ms 5120 KB Output is correct
10 Correct 84 ms 14260 KB Output is correct
11 Correct 111 ms 15988 KB Output is correct
12 Correct 107 ms 15728 KB Output is correct
13 Correct 116 ms 16500 KB Output is correct
14 Correct 86 ms 13748 KB Output is correct
15 Correct 97 ms 14500 KB Output is correct
16 Correct 184 ms 18040 KB Output is correct
17 Correct 185 ms 17700 KB Output is correct
18 Correct 194 ms 18392 KB Output is correct
19 Correct 164 ms 15476 KB Output is correct
20 Correct 166 ms 16024 KB Output is correct
21 Correct 166 ms 16224 KB Output is correct
22 Correct 166 ms 16268 KB Output is correct
23 Correct 162 ms 16092 KB Output is correct
24 Correct 173 ms 16400 KB Output is correct
25 Correct 161 ms 16032 KB Output is correct
26 Correct 165 ms 16112 KB Output is correct
27 Correct 162 ms 16076 KB Output is correct
28 Correct 4 ms 5120 KB Output is correct
29 Correct 4 ms 5120 KB Output is correct
30 Correct 4 ms 5120 KB Output is correct
31 Correct 4 ms 5120 KB Output is correct
32 Correct 4 ms 5120 KB Output is correct
33 Correct 4 ms 5248 KB Output is correct
34 Correct 4 ms 5120 KB Output is correct
35 Correct 4 ms 5120 KB Output is correct
36 Correct 4 ms 5120 KB Output is correct
37 Correct 14 ms 6400 KB Output is correct
38 Correct 109 ms 16584 KB Output is correct
39 Correct 107 ms 16500 KB Output is correct
40 Correct 105 ms 15992 KB Output is correct
41 Correct 104 ms 15604 KB Output is correct
42 Correct 104 ms 15328 KB Output is correct
43 Correct 94 ms 14584 KB Output is correct
44 Correct 110 ms 16496 KB Output is correct
45 Correct 108 ms 16624 KB Output is correct
46 Correct 93 ms 13692 KB Output is correct
47 Correct 115 ms 15348 KB Output is correct
48 Correct 23 ms 6656 KB Output is correct
49 Correct 220 ms 19032 KB Output is correct
50 Correct 214 ms 18652 KB Output is correct
51 Correct 209 ms 18724 KB Output is correct
52 Correct 211 ms 18280 KB Output is correct
53 Correct 207 ms 17616 KB Output is correct
54 Correct 164 ms 15204 KB Output is correct
55 Correct 217 ms 19704 KB Output is correct
56 Correct 218 ms 19416 KB Output is correct
57 Correct 182 ms 16320 KB Output is correct
58 Correct 213 ms 18392 KB Output is correct
59 Correct 91 ms 10232 KB Output is correct
60 Correct 205 ms 19024 KB Output is correct
61 Correct 200 ms 19120 KB Output is correct
62 Correct 229 ms 19928 KB Output is correct
63 Correct 172 ms 16856 KB Output is correct
64 Correct 4 ms 5120 KB Output is correct
65 Correct 4 ms 5120 KB Output is correct
66 Correct 4 ms 5120 KB Output is correct
67 Correct 5 ms 5120 KB Output is correct
68 Correct 4 ms 5120 KB Output is correct
69 Correct 5 ms 5120 KB Output is correct
70 Correct 5 ms 5120 KB Output is correct
71 Correct 5 ms 5120 KB Output is correct
72 Correct 4 ms 5120 KB Output is correct
73 Correct 5 ms 5248 KB Output is correct
74 Correct 131 ms 14324 KB Output is correct
75 Correct 111 ms 16368 KB Output is correct
76 Correct 108 ms 14836 KB Output is correct
77 Correct 109 ms 14168 KB Output is correct
78 Correct 101 ms 10104 KB Output is correct
79 Correct 79 ms 9340 KB Output is correct
80 Correct 107 ms 12664 KB Output is correct
81 Correct 162 ms 17908 KB Output is correct
82 Correct 178 ms 16888 KB Output is correct
83 Correct 182 ms 19700 KB Output is correct
84 Correct 90 ms 13684 KB Output is correct
85 Correct 161 ms 16244 KB Output is correct
86 Correct 64 ms 9004 KB Output is correct
87 Correct 211 ms 18408 KB Output is correct
88 Correct 208 ms 18520 KB Output is correct
89 Correct 191 ms 16016 KB Output is correct
90 Correct 188 ms 12496 KB Output is correct
91 Correct 185 ms 12796 KB Output is correct
92 Correct 199 ms 15444 KB Output is correct
93 Correct 259 ms 19888 KB Output is correct
94 Correct 263 ms 19028 KB Output is correct
95 Correct 295 ms 21496 KB Output is correct
96 Correct 188 ms 16344 KB Output is correct
97 Correct 226 ms 17972 KB Output is correct