Submission #83190

#TimeUsernameProblemLanguageResultExecution timeMemory
83190luciocfPictionary (COCI18_pictionary)C++14
56 / 140
479 ms8908 KiB
#include <bits/stdc++.h> using namespace std; const int maxn = 1e3+10; int pai[maxn], sz[maxn]; int ans[maxn][maxn]; int Find(int x) { if (pai[x] == x) return x; return pai[x] = Find(pai[x]); } void Join(int x, int y) { x = Find(x), y = Find(y); if (x == y) return; if (sz[x] < sz[y]) swap(x, y); sz[x] += sz[y]; pai[y] = x; } void init(int n) { for (int i = 1; i <= n; i++) pai[i] = i, sz[i] = 1; } int main(void) { memset(ans, -1, sizeof ans); int n, m, q; cin >> n >> m >> q; init(n); vector< pair<int, int> > Q; for (int i = 1; i <= q; i++) { int a, b; cin >> a >> b; Q.push_back({a, b}); } for (int i = m; i >= 1; i--) { for (int j = i; j <= n; j += i) Join(j, i); for (auto P: Q) { int a = P.first, b = P.second; if (Find(a) == Find(b) && ans[a][b] == -1) ans[a][b] = m-i+1; } } for (auto P: Q) cout << ans[P.first][P.second] << "\n"; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...