zoukankan      html  css  js  c++  java
  • K

    Given a connected undirected graph, tell if its minimum spanning tree is unique. 

    Definition 1 (Spanning Tree): Consider a connected, undirected graph G = (V, E). A spanning tree of G is a subgraph of G, say T = (V', E'), with the following properties: 
    1. V' = V. 
    2. T is connected and acyclic. 

    Definition 2 (Minimum Spanning Tree): Consider an edge-weighted, connected, undirected graph G = (V, E). The minimum spanning tree T = (V, E') of G is the spanning tree that has the smallest total cost. The total cost of T means the sum of the weights on all the edges in E'. 

    Input

    The first line contains a single integer t (1 <= t <= 20), the number of test cases. Each case represents a graph. It begins with a line containing two integers n and m (1 <= n <= 100), the number of nodes and edges. Each of the following m lines contains a triple (xi, yi, wi), indicating that xi and yi are connected by an edge with weight = wi. For any two nodes, there is at most one edge connecting them.

    Output

    For each input, if the MST is unique, print the total cost of it, or otherwise print the string 'Not Unique!'.

    Sample Input

    2
    3 3
    1 2 1
    2 3 2
    3 1 3
    4 4
    1 2 2
    2 3 2
    3 4 2
    4 1 2
    

    Sample Output

    3
    Not Unique!
    方法1:首先算出最小生成树的权值和ans,然后枚举删除最小生成树中的每一条边,若还可以达到相同的效果,就说明最小生成树不唯一,
    因为两个不同的最小生成树至少有一条边不同,所以我们才可以枚举删除每一条边.
    方法2:判断最小生成树和次小生成树的权值是否相同.
    #include<iostream>
    #include<vector>
    #include<algorithm>
    #include<stdio.h>
    using namespace std;
    typedef long long ll;
    const int maxn=100010;
    int f[maxn];
    struct node
    {
        int u,v,w;
        bool operator < (const node &r)const{
            return w<r.w;
        }
    }q[maxn];
    int Find(int x)
    {
        return f[x]==x?x:f[x]=Find(f[x]);
    }
    int Merge(int u,int v)
    {
        u=Find(u);
        v=Find(v);
        if(u!=v)return f[u]=v,1;
        return 0;
    }
    vector<int>v;
    int main()
    {
        int T;
        cin>>T;
        while(T--){
            v.clear();
            int n,m;
            cin>>n>>m;
            for(int i=1;i<=n;i++)f[i]=i;
            for(int i=1;i<=m;i++){
                cin>>q[i].u>>q[i].v>>q[i].w;
            }
            sort(q+1,q+1+m);
            int ans=0;
            for(int i=1;i<=m;i++){
                int x=Merge(q[i].u,q[i].v);
                if(x){
                    v.push_back(i);
                    ans+=q[i].w;
                }
            }
            int flag=1;
            for(int i=0;i<v.size();i++){
                int sum=0,cnt=0;
                for(int j=1;j<=n;j++)f[j]=j;
                for(int j=1;j<=m;j++){
                    if(j==v[i])continue;
                    int x=Merge(q[j].u,q[j].v);
                    if(x){
                        sum+=q[j].w;
                        cnt++;
                    }
                }
                if(cnt==n-1&&ans==sum){
                    flag=0;
                    break;
                }
            }
            if(flag)cout<<ans<<endl;
            else printf("Not Unique!
    "); 
    
        }
        return 0;
    }
    #include<iostream>
    #include<cstring>
    
    using namespace std;
    typedef long long ll;
    const int maxn=1010;
    const int INF=0x3f3f3f3f;
    int Maxlen[maxn][maxn];
    int dis[maxn],vis[maxn];
    int pre[maxn],MAP[maxn][maxn];
    int used[maxn][maxn];
    int n,m;
    
    int Prim(int x)
    {
        memset(Maxlen,0,sizeof(Maxlen));
        memset(dis,INF,sizeof(dis));
        memset(vis,0,sizeof(vis));
        memset(pre,0,sizeof(pre));
        memset(used,0,sizeof(used));
        for(int i=1;i<=n;i++){
            dis[i]=MAP[x][i];
            pre[i]=x;
        }
        dis[x]=0;
        vis[x]=1;
        pre[x]=0;
        int ans=0;
        for(int i=2;i<=n;i++){
            int u=0,minn=INF;
            for(int j=1;j<=n;j++){
                if(!vis[j]&&dis[j]<minn){
                    u=j;
                    minn=dis[j];
                }
            }
            vis[u]=1;
            ans+=minn;
            used[u][pre[u]]=used[pre[u]][u]=1;
            for(int v=1;v<=n;v++){
                if(vis[v]){
                    Maxlen[u][v]=Maxlen[v][u]=max(Maxlen[v][pre[u]],dis[u]);
                }
                else{
                    if(dis[v]>MAP[u][v]){
                        dis[v]=MAP[u][v];
                        pre[v]=u;
                    }
                }
            }
        }
        return ans;
    }
    void sst(int ans)
    {
        int sum=INF;
        for(int i=1;i<=n;i++){
            for(int j=i+1;j<=n;j++){
                if(!used[i][j]&&MAP[i][j]!=INF){
                    sum=min(sum,ans+MAP[i][j]-Maxlen[i][j]);
                }
            }
        }
        if(sum==ans)cout<<"Not Unique!"<<endl;
        else cout<<ans<<endl;
    }
    int main()
    {
        int T;
        cin>>T;
        while(T--){
            cin>>n>>m;
            for(int i=1;i<=n;i++){
                for(int j=1;j<=n;j++){
                    if(i==j)MAP[i][j]=0;
                    else MAP[i][j]=INF;
                }
            }
            for(int i=1;i<=m;i++){
                int u,v,w;
                cin>>u>>v>>w;
                MAP[u][v]=MAP[v][u]=min(MAP[u][v],w);
            }
            int ans=Prim(1);
            sst(ans);
        }
        return 0;
    }
  • 相关阅读:
    Java集合一
    集合类视图
    NIO
    IO补充
    线程图
    线程池
    Callable
    element-ui upload组件上传图片时限制图片宽高
    vue-router 使用a链接跳转至二级子页面偶尔会出现地址栏看不见路由和参数的情况
    vue中引入jquery报错问题
  • 原文地址:https://www.cnblogs.com/cherish-lin/p/11332866.html
Copyright © 2011-2022 走看看