Submission #1018625

#TimeUsernameProblemLanguageResultExecution timeMemory
1018625vjudge1Pictionary (COCI18_pictionary)C++17
140 / 140
73 ms16196 KiB
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10; int n, m, q, ans[N], par[N], tme; vector<int> query[N], S[N]; vector<pair<int, int>> vec; int root(int v){ return (par[v] == -1 ? v : root(par[v])); } void merge(int u, int v){ if ((u = root(u)) == (v = root(v))) return ; if (S[u].size() > S[v].size()) swap(u, v); par[u] = v; for (int a : S[u]){ S[v].push_back(a); for (int id : query[a]){ int b = vec[id].first + vec[id].second - a; if (root(b) == v) ans[id] = min(ans[id], tme); } } S[u].clear(); } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); memset(ans, 31, sizeof ans); cin >> n >> m >> q; for (int i = 1; i <= n; i ++){ par[i] = -1; S[i] = {i}; } for (int i = 0; i < q; i ++){ int a, b; cin >> a >> b; query[a].push_back(i); query[b].push_back(i); vec.push_back({a, b}); } for (int g = m; g > 0; g --){ tme++; for (int i = 2 * g; i <= n; i += g) merge(i, i - g); } for (int i = 0; i < q; i ++) cout << ans[i] << '\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...