Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- const int N=100001;
- #define mod 1000000007
- #define ll long long
- ll r[N],parent[N],n,prime[N]={0},p,m1[N]={0},m2[N]={0};
- void calculate(ll a,ll b, ll flag){
- ll curr=prime[a];
- ll cnt=1;
- while (a>1) {
- a/=prime[a];
- if (curr==prime[a]){
- cnt++;
- continue;
- }
- if(flag==1)
- m1[curr]+=b*cnt;
- else m2[curr]+=b*cnt;
- curr=prime[a];
- cnt=1;
- }
- }
- ll power(ll a,ll b){
- ll res=1;
- while(b){
- if(b%2) res=(res*a)%mod;
- a=(a*a)%mod;
- b/=2;
- }
- return res;
- }
- void makeset(){
- for(int i=1;i<=n;i++){
- parent[i]=i;
- r[i]=1;
- }
- }
- ll find(ll x){
- if(parent[x]!=x){
- parent[x]=find(parent[x]);
- }
- return parent[x];
- }
- void unionset(ll x,ll y){
- ll xp=find(x);
- ll yp=find(y);
- if(xp==yp) return;
- if(r[xp]>r[yp]){
- parent[yp]=xp;
- r[xp]+=r[yp];
- }
- else{
- parent[xp]=yp;
- r[yp]+=r[xp];
- }
- }
- int main(){
- ios::sync_with_stdio(false);
- cin.tie(0);
- // #ifndef ONLINE_JUDGE
- // freopen("in01.txt","r",stdin);
- // freopen("out01.txt","w",stdout);
- // #endif
- prime[1]=1;
- for(int i=2;i<=N;i++) prime[i]=i;
- for(int i=2;i*i<=N;i++){
- if(prime[i]==i){
- for(int j=i*i;j<=N;j+=i)
- prime[j]=i;
- }
- }
- cin>>n;
- makeset();
- vector<pair<int,pair<int,int > > > v;
- ll x,y,k,ans=1,m=0;
- int c=0;
- for(int i=0;i<n-1;i++){
- cin>>x>>y>>k;
- v.push_back(make_pair(k,make_pair(x,y)));
- if(k==1) c++;
- }
- sort(v.begin(),v.end());
- vector<pair<int,pair<int,int > > > :: iterator it,itr,itr2;
- it=v.begin();
- for( it;it!=v.end();it++){
- p=it->first;
- ll q=r[find(it->second.first)]*r[find(it->second.second)];
- ans=(ans*power(p,q))%mod;
- calculate(p,q,1);
- unionset(it->second.first,it->second.second);
- }
- cout<<ans<<"\n";
- makeset();
- ans=1;
- itr=v.end();
- itr--;
- itr2=v.begin();
- itr2--;
- for( it=itr;it!=itr2;it--){
- ll p=it->first;
- ll q=r[find(it->second.first)]*r[find(it->second.second)];
- ans=(ans*power(p,q))%mod;
- calculate(p,q,2);
- unionset(it->second.first,it->second.second);
- }
- cout<<ans<<"\n";
- ll res=INT_MAX;
- for(int i=1;i<N;i++){
- if(prime[i]!=i) continue;
- ll x=m1[i];
- ll y=m2[i];
- if(y==0) continue;
- ll z=x/y;
- res=min(res,z);
- if(y>x) {res=0;break;}
- }
- if(res==INT_MAX && c!=n-1) cout<<"0\n";
- else if(c==n-1) cout<<"1\n";
- else cout<<res;
- }
Add Comment
Please, Sign In to add comment