Submission #754111

# Submission time Handle Problem Language Result Execution time Memory
754111 2023-06-06T17:44:45 Z Lalic Pictionary (COCI18_pictionary) C++17
140 / 140
204 ms 13576 KB
#include <bits/stdc++.h>
using namespace std;
 
#define fi first
#define se second
#define pb push_back
 
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
 
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
const int MAXN = 1e5+10;

int pai[MAXN], peso[MAXN], val[MAXN];

int find(int x){ return pai[x]==x ? x : find(pai[x]); }

void join(int a, int b, int t){
	a=find(a); b=find(b);

	if(a==b) return;

	if(peso[a]>peso[b]){
		pai[b]=a;
		val[b]=t;
	}
	else if(peso[a]<peso[b]){
		pai[a]=b;
		val[a]=t;
	}
	else{
		pai[a]=b;
		val[a]=t;
		peso[b]++;
	}
}

int main(){
  	ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
 
    // freopen("wormhole.in","r",stdin);
    // freopen("wormhole.out","w",stdout);

	int n, m, q; cin >> n >> m >> q;

	memset(val, INF, sizeof val);
	for(int i=1;i<=n;i++) pai[i]=i;

	vector<vector<int>> divs(m+1);

	for(int i=1;i<=n;i++){
		for(int j=1;j*j<=i && j<=m;j++){
			if(i%j!=0) continue;

			divs[j].pb(i);
			if(j*j!=i && i/j<=m) divs[i/j].pb(i);
		}
	}

	int cnt=1;
	for(int i=m;i>=1;i--){
		for(int j=1;j<(int)divs[i].size();j++) join(divs[i][0], divs[i][j], cnt);
		cnt++;
	}

	while(q--){
		int a, b; cin >> a >> b;

		int ans=0;
		while(a!=b){
			if(val[a]<=val[b]) ans=max(ans, val[a]), a=pai[a];
			else ans=max(ans, val[b]), b=pai[b];
		}

		cout << ans << "\n";
	}
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 1608 KB Output is correct
2 Correct 16 ms 1500 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 1916 KB Output is correct
2 Correct 24 ms 1836 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 39 ms 3804 KB Output is correct
2 Correct 34 ms 3076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 48 ms 4248 KB Output is correct
2 Correct 51 ms 3144 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 83 ms 6808 KB Output is correct
2 Correct 85 ms 5220 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 79 ms 3892 KB Output is correct
2 Correct 137 ms 7828 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 146 ms 9792 KB Output is correct
2 Correct 204 ms 8528 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 200 ms 13576 KB Output is correct
2 Correct 200 ms 9720 KB Output is correct