# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
83814 | wzy | Pictionary (COCI18_pictionary) | C++11 | 108 ms | 43096 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int N = 1000005;
int pai[N] , peso[N] , tempo[N] , depth[N];
vector<int> adj[N];
bool vis[N];
int fd(int x){
return pai[x] == x ? x : fd(pai[x]);
}
void join(int u , int v , int tmp){
u = fd(u) , v = fd(v);
if(peso[u] < peso[v]) swap(u,v);
adj[u].push_back(v);
adj[v].push_back(u);
pai[v] = u , peso[u] += peso[v] , tempo[v] = tmp;
}
void dfs(int x , int p){
vis[x] = true;
if(x == p) depth[x] = 1;
else depth[x] = depth[p] + 1;
for(auto w : adj[x]){
if(w == p || vis[w]) continue;
dfs(w,x);
}
}
int32_t main(){
int n , m , q;
scanf("%d%d%d" , &n , &m , &q);
for(int i = 1 ; i <= n; i ++) pai[i] = i, tempo[i] = N + 100 , peso[i] = 1;
for(int i = m ; i >= 1 ; i--){
for(int j = 2*i ; j <= n ; j+=i){
if(fd(i) != fd(j))
join(i, j , m - i + 1);
}
}
for(int i = 1 ; i <= n; i ++){
if(!vis[fd(i)]) dfs(fd(i) , fd(i));
}
while(q--){
int x,y;
scanf("%d%d" , &x , &y);
if(depth[x] > depth[y]) swap(x,y);
int ans = -100;
while(depth[y] > depth[x]){
ans = max(ans , tempo[y]);
y = pai[y];
}
while(x != y){
ans = max(ans , max(tempo[x] , tempo[y]));
x = pai[x] , y = pai[y];
}
printf("%d\n" , ans);
}
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |