Submission #300060

# Submission time Handle Problem Language Result Execution time Memory
300060 2020-09-16T10:24:49 Z shrek12357 Pictionary (COCI18_pictionary) C++14
28 / 140
1500 ms 13916 KB
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <climits>
#include <cmath>
#include <fstream>
#include <queue>
using namespace std;

const int MAXN = 1e5+5;

struct state {
	int a;
	int b;
	int idx;
};

int sz[MAXN], par[MAXN];
vector<state> queries[MAXN];

int get(int x) {
	return x == par[x] ? x : par[x] = get(par[x]);
}

void unite(int a, int b) {
	a = get(a), b = get(b);
	if (a == b) {
		return;
	}
	if (sz[a] > sz[b]) {
		swap(a, b);
	}
	par[a] = b;
	for (auto i : queries[a]) {
		queries[b].push_back(i);
	}
	sz[b] += sz[a];
}

int main() {
	int n, m, q;
	cin >> n >> m >> q;

	int ans[MAXN];
	for (int i = 0; i < MAXN; i++) {
		sz[i] = 1;
		par[i] = i;
		ans[i] = -1;
	}
	for (int i = 0; i < q; i++) {
		int a, b;
		cin >> a >> b;
		queries[a].push_back({ a, b, i });
		queries[b].push_back({ a, b, i });
	}
	for (int i = 0; i < m; i++) {
		for (int j = m - i; j <= n; j += (m - i)) {
			unite(m - i, j);
			for (auto k : queries[get(j)]) {
				if (ans[k.idx] != -1) {
					continue;
				}
				if (get(k.a) == get(k.b)) {
					ans[k.idx] = i + 1;
				}
			}
		}
	}
	for (int i = 0; i < q; i++) {
		cout << ans[i] << endl;
	}
}
# Verdict Execution time Memory Grader output
1 Correct 65 ms 4600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 354 ms 6196 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 1535 ms 10672 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1574 ms 12744 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1585 ms 8672 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1595 ms 9180 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1582 ms 11084 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1567 ms 8176 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1583 ms 13172 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1584 ms 13916 KB Time limit exceeded
2 Halted 0 ms 0 KB -