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

    The Unique MST
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 25387   Accepted: 9061

    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

    POJ Monthly--2004.06.27 srbga@POJ


    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    using namespace std;
    struct node
    {
    	int u,v;
    	int val,flag;
    }edge[10010];
    int n,m,minn,flag,pre[10010];
    void init()
    {
    	for(int i=0;i<=n;i++)
    	pre[i]=i;
    }
    int cmp(node s1,node s2)
    {
    	return s1.val<s2.val;
    }
    int find(int x)
    {
    	return pre[x]==x?x:find(pre[x]);
    }
    int F(int w)
    {
    	int sum=0;
    	for(int i=0;i<m;i++)
    	{
    		if(i!=w)
    		{
    			int fx=find(edge[i].u);
    			int fy=find(edge[i].v);
    			if(fx!=fy)
    			{
    				pre[fx]=fy;
    				sum+=edge[i].val;
    			}
    		}
    	}
    	int s=find(1);
    	for(int i=2;i<=n;i++)
    	if(find(i)!=s)
    	return -1;
    	return sum;
    }
    int main()
    {
    	int t;
    	scanf("%d",&t);
    	while(t--)
    	{
    		
    		scanf("%d%d",&n,&m);
    		for(int i=0;i<m;i++)
    		{
    			scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].val);
    			edge[i].flag=0;
    		}
    		sort(edge,edge+m,cmp);
    		minn=0;
    		init();
    		for(int i=0;i<m;i++)
    		{
    			int fx=find(edge[i].u);
    			int fy=find(edge[i].v);
    			if(fx!=fy)
    			{
    				pre[fx]=fy;
    				edge[i].flag=1;
    				minn+=edge[i].val;
    			}
    		}
    //		printf("%d
    ",minn);
    		flag=0;
    		for(int i=0;i<m;i++)
    		{
    			if(edge[i].flag)
    			{
    				init();
    				if(F(i)==minn)
    				{
    					flag=1;
    					break;
    				}
    			}
    		}
    		if(flag) printf("Not Unique!
    ");
    		else printf("%d
    ",minn);
    	}
    	return 0;
    }


  • 相关阅读:
    information_schema
    面包屑路径导航
    mysql5.7.26安装
    菜单权限作为父权限
    权限控制到按钮
    二级菜单
    留言板和jq轮播图
    M商城
    表单
    w3c
  • 原文地址:https://www.cnblogs.com/playboy307/p/5273527.html
Copyright © 2011-2022 走看看