Submission #451956

#TimeUsernameProblemLanguageResultExecution timeMemory
451956zxcvbnmPictionary (COCI18_pictionary)C++14
140 / 140
115 ms10188 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; const int nax = 100'005; struct Edge { int to, w; }; int parent[nax]; int sz[nax]; int find(int v) { if (parent[v] == v) { return v; } return parent[v] = find(parent[v]); } bool unite(int a, int b) { a = find(a), b = find(b); if (a != b) { if (sz[a] < sz[b]) swap(a, b); parent[b] = a; sz[a] += sz[b]; return true; } return false; } vector<Edge> adj[nax]; int par[nax], depth[nax], cost[nax]; void dfs(int v) { for(Edge u : adj[v]) { if (u.to == par[v]) continue; par[u.to] = v; depth[u.to] = depth[v] + 1; cost[u.to] = u.w; dfs(u.to); } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n, m, q; cin >> n >> m >> q; for(int i = 1; i <= n; i++) { parent[i] = i; sz[i] = 1; } for(int i = m; i >= 1; i--) { for(int j = i+i; j <= n; j+=i) { if (unite(i, j)) { adj[i].push_back({j, m-i+1}); adj[j].push_back({i, m-i+1}); // cout << i << "->" << j << " " << m-i+1 << "\n"; } } } par[1] = -1; depth[1] = 0; cost[1] = 0; dfs(1); while(q--) { int x, y; cin >> x >> y; int dx = depth[x], dy = depth[y]; int mx = 0; while(dx > dy) { mx = max(mx, cost[x]); x = par[x]; dx--; } while(dy > dx) { mx = max(mx, cost[y]); y = par[y]; dy--; } while(x != y) { mx = max({mx, cost[x], cost[y]}); x = par[x]; y = par[y]; } cout << mx << "\n"; } return 0; }
#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...