Submission #94212

# Submission time Handle Problem Language Result Execution time Memory
94212 2019-01-16T16:37:33 Z teomrn Pictionary (COCI18_pictionary) C++14
0 / 140
320 ms 24484 KB
#include <bits/stdc++.h>
using namespace std;

const int NMAX = 100010;
const int LGMAX = 20;

namespace UF {
	int tata[NMAX + 10], g[NMAX + 10];

	void init() {
		iota(tata, tata + NMAX, 0);
		fill(g, g + NMAX, 1);
	}

	int find(int n) {
		if (tata[tata[n]] != tata[n])
			tata[n] = find(tata[n]);
		return tata[n];
	}
	bool unite(int a, int b) {
		a = find(a);
		b = find(b);
		if (a == b)
			return 0;
		if (g[a] < g[b])
			swap(a, b);
		tata[b] = a, g[a] += g[b];
		return 1;
	}
}

namespace Arb {
	int h[NMAX + 10];
	int val[LGMAX + 1][NMAX + 10];
	int str[LGMAX + 1][NMAX + 10];
	vector <pair <int, int>> adia[NMAX + 10];

	void dfs(int nod, int t, int vnod) {
		h[nod] = 1 + h[t];
		val[0][nod] = vnod;
		str[0][nod] = t;
		for (int i = 1; i <= LGMAX; i++) {
			val[i][nod] = min(val[i - 1][nod], val[i - 1][str[i - 1][nod]]);
			str[i][nod] = str[i - 1][str[i - 1][nod]];
		}
		for (auto i : adia[nod])
			if (i.first != t)
				dfs(i.first, nod, i.second);
	}

	int lca(int a, int b) {
		if (h[a] < h[b])
			swap(a, b);
		int ans = 1e9;
		for (int i = LGMAX; i >= 0; i--)
			if (h[a] - (1 << i) >= h[b])
				ans = min(ans, val[i][a]), a = str[i][a];
		for (int i = LGMAX; i >= 0; i--)
			if (str[i][a] != str[i][b])
				ans = min(ans, min(val[i][a], val[i][b])), a = str[i][a], b = str[i][b];
		return ans;
	}
}

int main()
{
	ios_base::sync_with_stdio(0);

	int n, m, q;
	cin >> n >> m >> q;

	UF::init();

	for (int i = m; i >= 1; i--) {
		for (int j = 2 * i; j <= n; j += i) {
			if (UF::unite(i, j)) {
				Arb::adia[j].push_back({ i, i });
				Arb::adia[i].push_back({ j, i });
			}
		}
	}

	assert(UF::g[UF::find(1)] == n);
	Arb::dfs(1, 0, 0);

	while (q--) {
		int a, b;
		cin >> a >> b;
		cout << m + 1 - Arb::lca(a, b) << '\n';
	}
		
	return 0;
}



# Verdict Execution time Memory Grader output
1 Incorrect 14 ms 3704 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Incorrect 35 ms 3832 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Incorrect 116 ms 4088 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 169 ms 4216 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 92 ms 8568 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 109 ms 10396 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 162 ms 13352 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 299 ms 17048 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 240 ms 20204 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 320 ms 24484 KB Output isn't correct
2 Halted 0 ms 0 KB -