# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
157784 | Lawliet | Pictionary (COCI18_pictionary) | C++17 | 0 ms | 0 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 MAX = 100010;
const int INF = 1000000010;
int n, m, q;
int pai[MAX];
int prof[MAX];
int edge[MAX];
int find(int cur)
{
if(pai[ cur ] == cur) return cur;
return find( pai[ cur ] );
}
void join(int U, int V, int W)
{
U = find( U ); V = find( V );
if( U == V ) return;
if( prof[ U ] < prof[ V ] ) swap( U , V );
pai[ V ] = U;
edge[ V ] = W;
if(prof[ U ] == prof[ V ]) prof[ U ]++;
}
int maxPath(int U, int V)
{
int ans = 0;
while( U != V )
{
if( edge[ U ] < edge[ V ] )
{
ans = max(ans , edge[ U ]);
U = pai[ U ];
}
else
{
ans = max(ans , edge[ V ]);
V = pai[ V ];
}
}
return ans;
}
int main()
{
scanf("%d %d %d",&n,&m,&q);
for(int i = 1 ; i <= n ; i++)
{
pai[ i ] = i;
edge[ i ] = INF;
}
for(int i = m ; i >= 1 ; i--)
for(int j = i ; j <= n ; j += i) join(i , j , m - i + 1);
for(int i = 1 ; i <= q ; i++)
{
int U, V;
scanf("%d %d",&U,&V);
printf("%d\n",maxPath( U , V ));
}
}#include <bits/stdc++.h>
using namespace std;
const int MAX = 100010;
const int INF = 1000000010;
int n, m, q;
int pai[MAX];
int prof[MAX];
int edge[MAX];
int find(int cur)
{
if(pai[ cur ] == cur) return cur;
return find( pai[ cur ] );
}
void join(int U, int V, int W)
{
U = find( U ); V = find( V );
if( U == V ) return;
if( prof[ U ] < prof[ V ] ) swap( U , V );
pai[ V ] = U;
edge[ V ] = W;
if(prof[ U ] == prof[ V ]) prof[ U ]++;
}
int maxPath(int U, int V)
{
int ans = 0;
while( U != V )
{
if( edge[ U ] < edge[ V ] )
{
ans = max(ans , edge[ U ]);
U = pai[ U ];
}
else
{
ans = max(ans , edge[ V ]);
V = pai[ V ];
}
}
return ans;
}
int main()
{
scanf("%d %d %d",&n,&m,&q);
for(int i = 1 ; i <= n ; i++)
{
pai[ i ] = i;
edge[ i ] = INF;
}
for(int i = m ; i >= 1 ; i--)
for(int j = i ; j <= n ; j += i) join(i , j , m - i + 1);
for(int i = 1 ; i <= q ; i++)
{
int U, V;
scanf("%d %d",&U,&V);
printf("%d\n",maxPath( U , V ));
}
}