Submission #657037

# Submission time Handle Problem Language Result Execution time Memory
657037 2022-11-08T19:16:10 Z 600Mihnea Swapping Cities (APIO20_swap) C++17
13 / 100
170 ms 13072 KB
#include "swap.h"

#include <bits/stdc++.h>

using namespace std;


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

int root(int a) {
	if (t[a] == 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] && is_endpoint[x] && is_endpoint[y]) {
		is_line[a] = 1;
		if (dim[a] > 1) {
			is_endpoint[x] = 0;
		}
		if (dim[b] > 1) {
			is_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;
}

void clr() {
	t.resize(n);
	is_line.resize(n);
	is_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;
		is_endpoint[i] = 1;
		dim[i] = 1;
		when[i] = -1;
	}
}

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

bool operator < (Edge first, Edge second) {
	return first.w < second.w;
}

vector<Edge> edges;

void init(int N, int M, vector<int> U, vector<int> V, vector<int> W) {
	n = N;
	m = M;
	assert((int) U.size() == m);
	assert((int) V.size() == m);
	assert((int) W.size() == 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 &edge : edges) {
		int a = edge.a;
		int b = edge.b;
		int w = edge.w;
		join(a, b, 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]) {
			assert(deja == 0);
			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;
	exit(0);

	return -1;
	clr();
	for (auto &edge : edges) {
		int a = edge.a;
		int b = edge.b;
		int w = edge.w;
		join(a, b, w);
		//cout << a << " " << b << " | " << w << " and I care about " << x << " " << y << "\n";
		if (root(x) == root(y) && !is_line[root(x)]) {
			return w; 
		}
	}
	return -1;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 32 ms 4800 KB Output is correct
10 Correct 39 ms 5896 KB Output is correct
11 Correct 38 ms 5820 KB Output is correct
12 Correct 39 ms 6088 KB Output is correct
13 Correct 41 ms 6148 KB Output is correct
14 Correct 41 ms 4948 KB Output is correct
15 Correct 144 ms 7724 KB Output is correct
16 Correct 170 ms 7652 KB Output is correct
17 Correct 153 ms 7952 KB Output is correct
18 Correct 117 ms 7976 KB Output is correct
19 Correct 90 ms 4776 KB Output is correct
20 Correct 152 ms 8860 KB Output is correct
21 Correct 162 ms 8992 KB Output is correct
22 Correct 149 ms 9248 KB Output is correct
23 Correct 122 ms 9252 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 107 ms 8812 KB Output is correct
4 Correct 122 ms 12836 KB Output is correct
5 Correct 110 ms 13048 KB Output is correct
6 Correct 105 ms 12692 KB Output is correct
7 Correct 109 ms 13072 KB Output is correct
8 Correct 106 ms 12792 KB Output is correct
9 Correct 107 ms 12840 KB Output is correct
10 Correct 105 ms 12684 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Runtime error 1 ms 596 KB Execution killed with signal 6
11 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 32 ms 4800 KB Output is correct
11 Correct 39 ms 5896 KB Output is correct
12 Correct 38 ms 5820 KB Output is correct
13 Correct 39 ms 6088 KB Output is correct
14 Correct 41 ms 6148 KB Output is correct
15 Runtime error 1 ms 596 KB Execution killed with signal 6
16 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 32 ms 4800 KB Output is correct
10 Correct 39 ms 5896 KB Output is correct
11 Correct 38 ms 5820 KB Output is correct
12 Correct 39 ms 6088 KB Output is correct
13 Correct 41 ms 6148 KB Output is correct
14 Correct 41 ms 4948 KB Output is correct
15 Correct 144 ms 7724 KB Output is correct
16 Correct 170 ms 7652 KB Output is correct
17 Correct 153 ms 7952 KB Output is correct
18 Correct 117 ms 7976 KB Output is correct
19 Correct 107 ms 8812 KB Output is correct
20 Correct 122 ms 12836 KB Output is correct
21 Correct 110 ms 13048 KB Output is correct
22 Correct 105 ms 12692 KB Output is correct
23 Correct 109 ms 13072 KB Output is correct
24 Correct 106 ms 12792 KB Output is correct
25 Correct 107 ms 12840 KB Output is correct
26 Correct 105 ms 12684 KB Output is correct
27 Runtime error 1 ms 596 KB Execution killed with signal 6
28 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 32 ms 4800 KB Output is correct
11 Correct 39 ms 5896 KB Output is correct
12 Correct 38 ms 5820 KB Output is correct
13 Correct 39 ms 6088 KB Output is correct
14 Correct 41 ms 6148 KB Output is correct
15 Correct 41 ms 4948 KB Output is correct
16 Correct 144 ms 7724 KB Output is correct
17 Correct 170 ms 7652 KB Output is correct
18 Correct 153 ms 7952 KB Output is correct
19 Correct 117 ms 7976 KB Output is correct
20 Correct 107 ms 8812 KB Output is correct
21 Correct 122 ms 12836 KB Output is correct
22 Correct 110 ms 13048 KB Output is correct
23 Correct 105 ms 12692 KB Output is correct
24 Correct 109 ms 13072 KB Output is correct
25 Correct 106 ms 12792 KB Output is correct
26 Correct 107 ms 12840 KB Output is correct
27 Correct 105 ms 12684 KB Output is correct
28 Runtime error 1 ms 596 KB Execution killed with signal 6
29 Halted 0 ms 0 KB -