1713: Spanning Tree

[Title] Italy

 

Given a point n and m edges undirected graph, find the least common multiple of all spanning trees FIG right edges of the greatest common divisor.

 

[Data size]

for 20% of data, M = N-1;

for the other 20% of data, M = N;

for another 30% of the data, all the edge weights are integer powers of 2;

Data for 100% , N≤1000, M≤100000, di≤215-1, ans≤264-1 .

【answer】

Consider first the right side of the case are integer powers of two, we can each edge of the right value to the largest number of run spanning tree minimum weight edge power is the answer 2.

 

Then we can consider the same approach, decomposition of the quality factor for each edge right side on the vector of the prime factors of the number added to the prime factors, for each prime factor biggest run spanning tree, by multiplying the final answer.

 

code show as below:

 

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1005,M=100005;
int n,m,ans,zhi[70005],top,ne[70000],fa[N];
struct pigu
{
    int co,dao,quan;
}a[M];
vector <pigu> ve[70000];
bool book[70000];
inline int read()
{
    char c=getchar();
    int x=0,f=1;
    while(!isdigit(c)) {if(c=='-') f=-1;c=getchar();}
    while(isdigit(c)) {x=(x<<3)+(x<<1)+c-'0';c=getchar();}
    return x*f;
}
inline int find(int x)
{
    if(fa[x]==x) return x;
    fa[x]=find(fa[x]);
    return fa[x];
}
inline void ycl()
{
    for(int i=2;i<=67000;i++)
    {
        if(book[i]==0)
            zhi[++top]=i,ne[i]=i;
        for(int j=1;j<=top&&zhi[j]*i<=67000;j++)
        {
            book[zhi[j]*i]=1;
            ne[zhi[j]*i]=zhi[j];
            if(i%zhi[j]==0) break;
        }
    }
}
inline bool cmp(pigu x,pigu y)
{
    return x.quan>y.quan;
}
inline int zxscs(int x)
{
    int huan=ve[x].size();
    sort(ve[x].begin(),ve[x].end(),cmp);
    for(int i=1;i<=n;i++) fa[i]=i;
    int cnt=0;
    for(int i=0;i<huan;i++)
    {
        int fa1=find(ve[x][i].co),fa2=find(ve[x][i].dao);
        if(fa1!=fa2)
        {
            cnt++;
            fa[fa1]=fa2;
        }
        if(cnt==n-1) return ve[x][i].quan;
    }
    return 0;
}
signed main()
{
    n=read();m=read();
    ycl();
    for(int i=1;i<=m;i++)
    {
        a[i].co=read();a[i].dao=read();a[i].quan=read();
        pigu dahu=a[i];
        int hu=a[i].quan;
        while(hu>1 )
        {     
            int gu = 0 , V = ne [hu].
            scramble (ne [hu] == a) 
            { 
                gu ++ ; 
                hu / = ne [hu]. 
            } 
            Dahu.quan = gu; 
            ve [a] .push_back (dahu); 
        } 
        Dahu.quan = 1 ; 
        ve [ 1 ] .push_back (dahu); 
    } 
    If (zxscs ( 1 ) == 0 ) 
    { 
        cout << " 0";
        return 0;
    }ans=1;
    for(int i=2;i<=67000;i++)
    {
        int hu=ve[i].size();
        if(hu>=n-1) 
        {
            int ga=1,ha=zxscs(i);
            while(ha--) ga*=i;
            ans*=ga;
        }
    }
    cout<<ans;
}
View Code

Guess you like

Origin www.cnblogs.com/betablewaloot/p/12207877.html