QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#615032#4582. Uniform Makeryuto1115#RE 0ms0kbC++201.4kb2024-10-05 17:26:162024-10-05 17:26:17

Judging History

你现在查看的是最新测评结果

  • [2024-10-05 17:26:17]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:0kb
  • [2024-10-05 17:26:16]
  • 提交

answer

#include<bits/stdc++.h>

using std::cin;
using std::cout;
using std::vector;
using ll = long long;
using vi = vector<ll>;
using vii = vector<vi>;

#define rep(i,j,k) for(ll i=ll(j); i<ll(k); i++)
#define REP(i,j,k) for(ll i=ll(j); i<=ll(k); i++)
#define per(i,j,k) for(ll i=ll(j); i>=ll(k); i--)
#define pb emplace_back

int main(){
	ll N; cin >> N;
	vi A(N);
	rep(i,0,N) cin >>A[i];
	vii edge(N);
	rep(i,1,N){
		ll u,v; cin >> u >> v;
		edge[--u].pb(--v);
		edge[v].pb(u);
	}
	ll ans = 0;
	vector<std::set<ll>> v(N);
	constexpr ll max = 1e6+10;
	vi table(max);
	per(i,max-1,2){
		for(ll j=i; j<max; j+=i){
			table[j]=i;
		}
	}
	std::queue<ll> que;
	vi par(N,-1);
	par[0]=0;
	{
		que.push(0);
		while(!que.empty()){
			ll now = que.front();
			for(auto next:edge[now]){
				if(par[next]==-1){
					par[next]=now;
					que.push(next);
				}
			}
		}
	}
	rep(i,1,N){
		ll now = std::__gcd(A[i], A[par[i]]);
		while(now > 1){
			ll next=table[now];
			now/=next;
			v[next].insert(i);
		}
	}
	vi sz(N,1);
	vi par2(N);
	rep(i,0,N) par2[i]=i;
	std::function<ll(ll)> root = [&](ll now){
		if(par2[now]==now) return now;
		return par2[now]=root(par2[now]);
	};
auto merge=[&](ll u, ll v) {
		u=root(u);
		v=root(v);
		if(u==v) return;
		if(sz[u]>sz[v]) std::swap(u,v);
		sz[v]+=sz[u];
		par2[u]=v;
		ans=std::max(ans,sz[v]);
	};
	rep(i,1,max){
		for(auto el: v[i]){
			merge(el, par[el]);
		}
		for(auto el: v[i]){
			par2[i]=i;
			sz[i]=1;
		}
	}
	cout << ans << std::endl;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Runtime Error

input:

6 4
calf
palm
book
icpc
ball
room

output:


result: