#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
/*
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("-O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
*/
#define mp make_pair
#define ll long long
#define ld long double
#define pb push_back
#define fast_io ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define fs first
#define sc second
#define getfiles ifstream cin("input.txt"); ofstream cout("output.txt");
#define endl '\n'
#define pii pair<int, int>
const int INF = 2000000005;
const ll BIG_INF = 2000000000000000005;
const int mod = 1000000007;
const int P = 31;
const ld PI = 3.141592653589793238462643;
const double eps = 1e-9;
using namespace std;
using namespace __gnu_pbds;
bool valid(int x, int y, int n, int m) {
return x >= 0 && y >= 0 && x < n && y < m;
}
mt19937 rng(1999999973);
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
const int N = 100000 + 50;
int n, m, q, ans[N], p[N], sz[N], x[N], y[N];
vector<int> req[N];
int find_set(int v) {
if (p[v] == v)
return v;
p[v] = find_set(p[v]);
return p[v];
}
int cnt = 0;
inline void add_edge(int u, int v, int day) {
u = find_set(u), v = find_set(v);
if (u == v)
return;
if (sz[u] > sz[v])
swap(u, v);
p[u] = v;
sz[v] += sz[u];
for (int i = 0; i < req[v].size(); i++) {
if (find_set(x[req[v][i]]) == find_set(y[req[v][i]])) {
ans[req[v][i]] = day;
swap(req[v][i], req[v].back());
req[v].pop_back();
cnt--;
i--;
}
}
for (int i = 0; i < req[u].size(); i++) {
if (find_set(x[req[u][i]]) == find_set(y[req[u][i]])) {
ans[req[u][i]] = day;
swap(req[u][i], req[u].back());
req[u].pop_back();
cnt--;
i--;
}
}
for (int i = 0; i < req[u].size(); i++) {
req[v].pb(req[u][i]);
}
//req[u].clear();
}
signed main() {
fast_io;
cin >> n >> m >> q;
cnt = q;
for (int i = 1; i <= n; i++) {
p[i] = i;
sz[i] = 1;
}
for (int i = 0; i < q; i++) {
cin >> x[i] >> y[i];
req[x[i]].pb(i);
}
for (int i = m; i >= 1 && cnt > 0; i--) {
for (int j = i + i; j <= n; j += i) {
add_edge(i, j, m - i + 1);
}
}
for (int i = 0; i < q; i++)
cout << ans[i] << endl;
return 0;
}
Compilation message
pictionary.cpp: In function 'void add_edge(int, int, int)':
pictionary.cpp:65:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < req[v].size(); i++) {
~~^~~~~~~~~~~~~~~
pictionary.cpp:74:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < req[u].size(); i++) {
~~^~~~~~~~~~~~~~~
pictionary.cpp:83:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < req[u].size(); i++) {
~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
2944 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
3328 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
60 ms |
4904 KB |
Output is correct |
2 |
Correct |
119 ms |
4472 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
132 ms |
5624 KB |
Output is correct |
2 |
Correct |
116 ms |
5160 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
143 ms |
4600 KB |
Output is correct |
2 |
Correct |
187 ms |
4452 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
241 ms |
4860 KB |
Output is correct |
2 |
Correct |
1033 ms |
4748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
478 ms |
5752 KB |
Output is correct |
2 |
Correct |
299 ms |
4984 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1584 ms |
5636 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
973 ms |
7084 KB |
Output is correct |
2 |
Correct |
1173 ms |
7108 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1374 ms |
7764 KB |
Output is correct |
2 |
Correct |
1367 ms |
7568 KB |
Output is correct |