Submission #849008

# Submission time Handle Problem Language Result Execution time Memory
849008 2023-09-13T21:01:35 Z tvladm2009 Swapping Cities (APIO20_swap) C++17
100 / 100
165 ms 17732 KB
#include "swap.h"

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int INF = (int)1e9 + 7;
vector<int> t;
vector<int> dim;
vector<int> is_line;
vector<int> when;
vector<int> endpoint;
vector<int> when_join;
int n, m;

void clr() {
	t.resize(n);
	is_line.resize(n);
	endpoint.resize(n);
	dim.resize(n);
	when.resize(n);
	when_join.resize(n);
	for (int i = 0; i < n; i++) {
		t[i] = i;
		is_line[i] = 1;
		endpoint[i] = 1;
		dim[i] = 1;
		when[i] = -1;
	}
}

int root(int a) {
        if (a == t[a]) {
                return t[a];
        }
        else {
                return root(t[a]);
        }
}

void join(int a, int b, int val) {
	int x = a, y = b;
	a = root(a);
	b = root(b);
	if (a == b) {
		if (is_line[a] == 1) {
			when[a] = val;
		}
		is_line[a] = 0;
		return;
	}
	if (dim[a] < dim[b]) {
		swap(a, b);
		swap(x, y);
	}
	if (is_line[a] && is_line[b] && endpoint[x] && endpoint[y]) {
		is_line[a] = 1;
		if (dim[a] > 1) {
			endpoint[x] = 0;
		}
		if (dim[b] > 1) {
			endpoint[y] = 0;
		}
	} else {
		if (is_line[a] == 1) {
			when[a] = val;
			is_line[a] = 0;
		}
	}
	when_join[b] = val; 
	dim[a] += dim[b];
	t[b] = a;
}

struct Edge {
        int a;
        int b;
        int w;
};

bool operator < (Edge a, Edge b) {
        return a.w < b.w;
}

vector<Edge> edges;

void init(int N, int M, vector<int> U, vector<int> V, vector<int> W) {
        assert((int)U.size() == M);
        assert((int)V.size() == M);
        assert((int)W.size() == M);

        n = N;
        m = M;
        edges.clear();
        for (int i = 0; i < m; i++) {
                edges.push_back({U[i], V[i], W[i]});
        }
        sort(edges.begin(), edges.end());
        clr();
        for (auto& edg : edges) {
                join(edg.a, edg.b, edg.w);
        }
}

int getMinimumFuelCapacity(int x, int y) {
	if (x == y) {
		return 0;
	}
	vector<int> path_x, path_y;
	while (1) {
		path_x.push_back(x);
		if (x == t[x]) {
			break;
		}
		x = t[x];
	}
	while (1) {
		path_y.push_back(y);
		if (y == t[y]) {
			break;
		}
		y = t[y];
	}
	reverse(path_x.begin(), path_x.end());
	reverse(path_y.begin(), path_y.end());
	bool deja = 0;
	int wjoin = 0;
	for (int i = max((int) path_x.size() - 1, (int) path_y.size() -1); i >= 0; i--) {
		if (i < (int) path_x.size() && i < (int) path_y.size() && path_x[i] == path_y[i]) {
			if (deja) {
				return wjoin;
			}
			if (when[path_x[i]] != -1) {
				return max(wjoin, when[path_x[i]]);
			}
		}
		if (i < (int) path_x.size()) {
			if (when[path_x[i]] != -1) deja = 1;
			wjoin = max(wjoin, when_join[path_x[i]]);
		}
		if (i < (int) path_y.size()) {
			if (when[path_y[i]] != -1) deja = 1;
			wjoin = max(wjoin, when_join[path_y[i]]);
		}
	}
	
	return -1;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 26 ms 6604 KB Output is correct
10 Correct 32 ms 7936 KB Output is correct
11 Correct 31 ms 7924 KB Output is correct
12 Correct 33 ms 8392 KB Output is correct
13 Correct 31 ms 8392 KB Output is correct
14 Correct 39 ms 7176 KB Output is correct
15 Correct 132 ms 12064 KB Output is correct
16 Correct 147 ms 11892 KB Output is correct
17 Correct 127 ms 12440 KB Output is correct
18 Correct 100 ms 12332 KB Output is correct
19 Correct 77 ms 6636 KB Output is correct
20 Correct 122 ms 13124 KB Output is correct
21 Correct 129 ms 13560 KB Output is correct
22 Correct 126 ms 13868 KB Output is correct
23 Correct 99 ms 13856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 89 ms 12648 KB Output is correct
4 Correct 87 ms 12836 KB Output is correct
5 Correct 89 ms 13068 KB Output is correct
6 Correct 87 ms 12844 KB Output is correct
7 Correct 89 ms 13120 KB Output is correct
8 Correct 88 ms 12812 KB Output is correct
9 Correct 86 ms 12960 KB Output is correct
10 Correct 87 ms 12672 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 448 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 26 ms 6604 KB Output is correct
11 Correct 32 ms 7936 KB Output is correct
12 Correct 31 ms 7924 KB Output is correct
13 Correct 33 ms 8392 KB Output is correct
14 Correct 31 ms 8392 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 344 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 448 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 7 ms 1560 KB Output is correct
35 Correct 31 ms 8116 KB Output is correct
36 Correct 31 ms 8116 KB Output is correct
37 Correct 32 ms 7876 KB Output is correct
38 Correct 31 ms 7980 KB Output is correct
39 Correct 33 ms 7888 KB Output is correct
40 Correct 28 ms 7108 KB Output is correct
41 Correct 33 ms 8392 KB Output is correct
42 Correct 32 ms 7960 KB Output is correct
43 Correct 30 ms 7880 KB Output is correct
44 Correct 33 ms 8400 KB Output is correct
45 Correct 49 ms 10436 KB Output is correct
46 Correct 30 ms 8144 KB Output is correct
47 Correct 31 ms 8144 KB Output is correct
48 Correct 35 ms 8648 KB Output is correct
49 Correct 52 ms 9928 KB Output is correct
50 Correct 37 ms 7116 KB Output is correct
51 Correct 43 ms 9156 KB Output is correct
52 Correct 55 ms 11968 KB Output is correct
53 Correct 59 ms 12820 KB Output is correct
54 Correct 65 ms 13760 KB Output is correct
55 Correct 30 ms 8132 KB Output is correct
56 Correct 56 ms 12740 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 26 ms 6604 KB Output is correct
10 Correct 32 ms 7936 KB Output is correct
11 Correct 31 ms 7924 KB Output is correct
12 Correct 33 ms 8392 KB Output is correct
13 Correct 31 ms 8392 KB Output is correct
14 Correct 39 ms 7176 KB Output is correct
15 Correct 132 ms 12064 KB Output is correct
16 Correct 147 ms 11892 KB Output is correct
17 Correct 127 ms 12440 KB Output is correct
18 Correct 100 ms 12332 KB Output is correct
19 Correct 89 ms 12648 KB Output is correct
20 Correct 87 ms 12836 KB Output is correct
21 Correct 89 ms 13068 KB Output is correct
22 Correct 87 ms 12844 KB Output is correct
23 Correct 89 ms 13120 KB Output is correct
24 Correct 88 ms 12812 KB Output is correct
25 Correct 86 ms 12960 KB Output is correct
26 Correct 87 ms 12672 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 344 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 1 ms 344 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 1 ms 344 KB Output is correct
34 Correct 1 ms 348 KB Output is correct
35 Correct 1 ms 348 KB Output is correct
36 Correct 7 ms 1560 KB Output is correct
37 Correct 31 ms 8116 KB Output is correct
38 Correct 31 ms 8116 KB Output is correct
39 Correct 32 ms 7876 KB Output is correct
40 Correct 31 ms 7980 KB Output is correct
41 Correct 33 ms 7888 KB Output is correct
42 Correct 28 ms 7108 KB Output is correct
43 Correct 33 ms 8392 KB Output is correct
44 Correct 32 ms 7960 KB Output is correct
45 Correct 30 ms 7880 KB Output is correct
46 Correct 33 ms 8400 KB Output is correct
47 Correct 13 ms 1900 KB Output is correct
48 Correct 124 ms 12976 KB Output is correct
49 Correct 121 ms 12896 KB Output is correct
50 Correct 122 ms 12932 KB Output is correct
51 Correct 119 ms 12856 KB Output is correct
52 Correct 118 ms 12484 KB Output is correct
53 Correct 133 ms 11464 KB Output is correct
54 Correct 129 ms 13868 KB Output is correct
55 Correct 132 ms 12904 KB Output is correct
56 Correct 96 ms 12896 KB Output is correct
57 Correct 117 ms 13884 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 26 ms 6604 KB Output is correct
11 Correct 32 ms 7936 KB Output is correct
12 Correct 31 ms 7924 KB Output is correct
13 Correct 33 ms 8392 KB Output is correct
14 Correct 31 ms 8392 KB Output is correct
15 Correct 39 ms 7176 KB Output is correct
16 Correct 132 ms 12064 KB Output is correct
17 Correct 147 ms 11892 KB Output is correct
18 Correct 127 ms 12440 KB Output is correct
19 Correct 100 ms 12332 KB Output is correct
20 Correct 89 ms 12648 KB Output is correct
21 Correct 87 ms 12836 KB Output is correct
22 Correct 89 ms 13068 KB Output is correct
23 Correct 87 ms 12844 KB Output is correct
24 Correct 89 ms 13120 KB Output is correct
25 Correct 88 ms 12812 KB Output is correct
26 Correct 86 ms 12960 KB Output is correct
27 Correct 87 ms 12672 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 1 ms 344 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 1 ms 344 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 1 ms 344 KB Output is correct
35 Correct 1 ms 348 KB Output is correct
36 Correct 1 ms 348 KB Output is correct
37 Correct 7 ms 1560 KB Output is correct
38 Correct 31 ms 8116 KB Output is correct
39 Correct 31 ms 8116 KB Output is correct
40 Correct 32 ms 7876 KB Output is correct
41 Correct 31 ms 7980 KB Output is correct
42 Correct 33 ms 7888 KB Output is correct
43 Correct 28 ms 7108 KB Output is correct
44 Correct 33 ms 8392 KB Output is correct
45 Correct 32 ms 7960 KB Output is correct
46 Correct 30 ms 7880 KB Output is correct
47 Correct 33 ms 8400 KB Output is correct
48 Correct 13 ms 1900 KB Output is correct
49 Correct 124 ms 12976 KB Output is correct
50 Correct 121 ms 12896 KB Output is correct
51 Correct 122 ms 12932 KB Output is correct
52 Correct 119 ms 12856 KB Output is correct
53 Correct 118 ms 12484 KB Output is correct
54 Correct 133 ms 11464 KB Output is correct
55 Correct 129 ms 13868 KB Output is correct
56 Correct 132 ms 12904 KB Output is correct
57 Correct 96 ms 12896 KB Output is correct
58 Correct 117 ms 13884 KB Output is correct
59 Correct 77 ms 6636 KB Output is correct
60 Correct 122 ms 13124 KB Output is correct
61 Correct 129 ms 13560 KB Output is correct
62 Correct 126 ms 13868 KB Output is correct
63 Correct 99 ms 13856 KB Output is correct
64 Correct 1 ms 348 KB Output is correct
65 Correct 1 ms 348 KB Output is correct
66 Correct 1 ms 448 KB Output is correct
67 Correct 1 ms 348 KB Output is correct
68 Correct 1 ms 348 KB Output is correct
69 Correct 1 ms 348 KB Output is correct
70 Correct 1 ms 348 KB Output is correct
71 Correct 1 ms 348 KB Output is correct
72 Correct 1 ms 348 KB Output is correct
73 Correct 1 ms 348 KB Output is correct
74 Correct 49 ms 10436 KB Output is correct
75 Correct 30 ms 8144 KB Output is correct
76 Correct 31 ms 8144 KB Output is correct
77 Correct 35 ms 8648 KB Output is correct
78 Correct 52 ms 9928 KB Output is correct
79 Correct 37 ms 7116 KB Output is correct
80 Correct 43 ms 9156 KB Output is correct
81 Correct 55 ms 11968 KB Output is correct
82 Correct 59 ms 12820 KB Output is correct
83 Correct 65 ms 13760 KB Output is correct
84 Correct 30 ms 8132 KB Output is correct
85 Correct 56 ms 12740 KB Output is correct
86 Correct 35 ms 4892 KB Output is correct
87 Correct 121 ms 12844 KB Output is correct
88 Correct 129 ms 13108 KB Output is correct
89 Correct 105 ms 13032 KB Output is correct
90 Correct 124 ms 15064 KB Output is correct
91 Correct 122 ms 14024 KB Output is correct
92 Correct 116 ms 13860 KB Output is correct
93 Correct 139 ms 17336 KB Output is correct
94 Correct 139 ms 17136 KB Output is correct
95 Correct 165 ms 17732 KB Output is correct
96 Correct 97 ms 13104 KB Output is correct
97 Correct 121 ms 15420 KB Output is correct