zoukankan      html  css  js  c++  java
  • (次小生成树) poj 1679

    The Unique MST
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 21358   Accepted: 7560

    Description

    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!
    

    Source

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<string>
    #include<cmath>
    #include<algorithm>
    using namespace std;
    #define INF 1<<30
    int n,m,a[110][110],low[110],vis[110],pre[110];
    int maxx[110][110],connect[110][110],mst;
    int prime()
    {
          int minn,pos,fa,ans=0;
          vis[1]=1,pos=1;
          for(int i=1;i<=n;i++)
                if(i!=pos)
                {
                      low[i]=a[pos][i];
                      pre[i]=pos;
                }
          for(int i=1;i<n;i++)
          {
                minn=INF;
                for(int j=1;j<=n;j++)
                {
                      if(!vis[j]&&minn>low[j])
                      {
                            pos=j;
                            minn=low[j];
                      }
                }
                fa=pre[pos];
                ans+=minn;
                vis[pos]=1;
                connect[fa][pos]=connect[pos][fa]=1;
                maxx[fa][pos]=maxx[pos][fa]=minn;
                for(int j=1;j<=n;j++)
                      maxx[pos][j]=maxx[j][pos]=max(maxx[pos][fa],maxx[fa][j]);
                for(int j=1;j<=n;j++)
                {
                      if(!vis[j]&&low[j]>a[pos][j])
                      {
                            low[j]=a[pos][j];
                            pre[j]=pos;
                      }
                }
          }
          return ans;
    }
    int main()
    {
          int tt;
          scanf("%d",&tt);
          while(tt--)
          {
                bool flag=false;
                memset(vis,0,sizeof(vis));
                memset(connect,0,sizeof(connect));
                memset(maxx,0,sizeof(maxx));
                scanf("%d%d",&n,&m);
                for(int i=1;i<=n;i++)
                      for(int j=1;j<=n;j++)
                            a[i][j]=INF;
                for(int i=1;i<=m;i++)
                {
                      int x,y,z;
                      scanf("%d%d%d",&x,&y,&z);
                      a[x][y]=z,a[y][x]=z;
                }
                mst=prime();
                for(int i=1;i<=n;i++)
                      for(int j=1;j<=n;j++)
                      {
                            if(connect[i][j]||a[i][j]==INF)
                                  continue;
                            if(a[i][j]==maxx[i][j])
                            {
                                  flag=true;
                                  break;
                            }
                      }
                if(flag)
                      printf("Not Unique!
    ");
                else
                      printf("%d
    ",mst);
          }
          return 0;
    }
    

      

  • 相关阅读:
    Hadoop面试
    Node.js面试题
    Node.js面试题
    [转载]最好的关系,是我懂你的不容易
    据说练就了一指禅神功的觅闻实时手机新闻网,正以每天2000+IP的用户量递增。有智能手机的可以当场进行体验,没有的就算了哈
    刚6瓶啤酒4两56度白酒下肚,居然20分钟做了一手机版网站 !
    Android:刚6瓶啤酒4两56度白酒下肚,居然20分钟做了一手机版网站 !
    IT人生的价值和意义 感觉真的有了
    (Android+IOS)正在做一个新闻App,做的差不多了,听听大家的建议 (图)
    (Android 即时通讯) [悬赏],无论是谁发现一个漏洞奖励人民币1000元!
  • 原文地址:https://www.cnblogs.com/a972290869/p/4228918.html
Copyright © 2011-2022 走看看