poj_1679 The Unique MST

news/2024/7/10 3:51:54 标签: tree, path, graph, properties, each, integer

The Unique MST

Time Limit: 1000MS

Memory Limit: 10000K

Total Submissions: 14582

Accepted: 5035

题目连接:http://poj.org/problem?id=1679

Description

Given a connected undirectedgraph, 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 thefollowing 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 thespanning tree that has the smallest total cost. The total cost of T means thesum of the weights on all the edges in E'.

Input

The first line contains asingle integer t (1 <= t <= 20), the number of test cases. Each caserepresents 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 linescontains a triple (xi, yi, wi), indicating that xi and yi are connected by anedge with weight = wi. For any two nodes, there is at most one edge connectingthem.

Output

For each input, if the MST isunique, print the total cost of it, or otherwise print the string 'NotUnique!'.

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

POJMonthly--2004.06.27 srbga@POJ

题意:

         判断最小生成树是否唯一,即是否有两个一模一样的最小生成树的值

解题思路:

         这是一个求次小生成树的题目,可以依照求最小生成树的方法来求,但是求最小生成树的时候要保存该生成树的每一条边,在删除某一条边后在求最小生成树,比较是否相等

代码:

//求次小生成树

/*

       首先求出原图最小生成树,记录权值之和为MinST。枚举添加每条不在最小生成树上的边(u,v)

       加上以后一定会形成一个环。找到环上权值第二大的边(即除了(u,v)以外的权值最大的边),把

       它删掉,计算当前生成树的权值之和。取所有枚举修改的生成树权值之和的最小值,就是次小生

       成树。

*/

#include <iostream>
#include<cstdio>
#include<cstring>
#define MAX 200
#define VALUE 0xfffffff
using namespace std;

//存储最小生成树的边
struct des
{
	int w;
	int s;
	int e;
};

int g[MAX][MAX];
int visited[MAX];
des minCost[MAX];
int v,e;

//求最小生成树
int prim()
{
    int i;
    for(i=0;i<=v;i++)
	{
        minCost[i].w=VALUE;
		minCost[i].s=0;
		minCost[i].e=0;
		visited[i]=0;
	}
    minCost[1].w=0;
    int res=0;
    while(true)
    {
        int t=-1;
		int a=0;
        for(i=1;i<=v;i++)
        {
            if(visited[i]==0 && (t==-1 || minCost[i].w<minCost[t].w))
			{
                t=i;
				minCost[t].e=minCost[i].e;
				minCost[t].s=minCost[i].s;
				minCost[t].w=minCost[i].w;
			}
        }
        //每条边都遍历完
        if(t==-1)
            break;
        //访问t点
        visited[t]=1;
		if(minCost[t].w==VALUE)
			return -1;//不连通
        res+=minCost[t].w;
        //printf("%d\t",minCost[t].w);
        for(i=1;i<=v;i++)
        {
            if(minCost[i].w>g[t][i] && g[t][i]!=0)
            {
                minCost[i].w=g[t][i];
				minCost[i].s=t;
				minCost[i].e=i;
            }

        }
    }
    return res;
}


void secondShortPath()
{
    int path1=prim();//最短路径
	if(path1==-1 || path1==0)
	{
		printf("0\n");
		return ;
	}
    int i;

	//枚举最小生成树的每一条边,
	des tempCost[MAX];
	//保存原来的值
	for(i=0;i<=v;i++)
	{
		tempCost[i]=minCost[i];
	}
	for(i=1;i<=v;i++)
	{
		if(tempCost[i].s!=0 && tempCost[i].e!=0)
		{
			//假设最小生成树中某一条边没了,再求最小生成树,看是否还相等
			g[tempCost[i].s][tempCost[i].e]=0;//minCost值发生了改变
			g[tempCost[i].e][tempCost[i].s]=0;
			int path2=prim();
			if(path2==-1)//不连通
				continue;
			if(path1==path2)//最小生成树不唯一
			{
				printf("Not Unique!\n");
				return ;
			}
			//还原之前删除的边
			g[tempCost[i].s][tempCost[i].e]=tempCost[i].w;
			g[tempCost[i].e][tempCost[i].s]=tempCost[i].w;
		}
	}

    printf("%d\n",path1);
}

int main()
{
	int ts;
	scanf("%d",&ts);
	while(ts--)
	{
		memset(g,0,sizeof(g));

		scanf("%d%d",&v,&e);
		int i;
		int start,end,weight;
		for(i=0;i<e;i++)
		{
			scanf("%d%d%d",&start,&end,&weight);
			g[start][end]=weight;
			g[end][start]=weight;
		}
		secondShortPath();
	}

    return 0;
}


 

treeSkill">

http://www.niftyadmin.cn/n/862604.html

相关文章

PHP开发规范!

一、规范前言篇 标准化不是特殊的个人风格&#xff0c;它让程序员可以了解任何代码&#xff0c;弄清程序的状况&#xff1b;新人可以很快的适应环境&#xff1b;防止新接触php的人一次次的犯同样的错误&#xff1b;在一致的开发环境下&#xff0c;可以减少人们犯错的机会。本规…

poj_1789 Truck History

Truck History Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 12456 Accepted: 4713 题目链接&#xff1a;http://poj.org/problem?id1789 Description Advanced Cargo Movement, Ltd.uses trucks of different types. Some trucks are used for vegetab…

poj_2485 Highways

Highways Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16037 Accepted: 7460 题目链接&#xff1a;http://poj.org/problem?id2485 Description The island nation of Flatopiais perfectly flat. Unfortunately, Flatopia has no public highways. So…

2021.11.21【读书笔记】丨生物信息学与功能基因组学(第五章 高级数据库搜索 中 )

5.3 寻找远缘相关蛋白质&#xff1a;位置特异性迭代BLAST&#xff08;PSI-BLAST&#xff09;和DELTA-BLAST PAM250矩阵给探测远缘相关蛋白质提供了一个更好的打分系统&#xff0c;可以改变打分矩阵来检测远缘蛋白质&#xff0c;但仍然有局限性&#xff1a; BLASTP检测到匹配蛋白…

hdu_1863 畅通工程

畅通工程 题目链接&#xff1a;http://acm.hdu.edu.cn/showproblem.php?pid1863 TimeLimit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 8916 Accepted Submission(s): 3454 ProblemDescription 省政府“畅通工程”的…

2021.11.22【读书笔记】丨生物信息学与功能基因组学(第五章第四节 隐马尔可夫模型)

5.4 谱搜索&#xff1a;隐马尔可夫模型&#xff08;HMMs&#xff09; 谱隐马尔可夫模型在生成用于识别远缘序列相似度的位置特异性打分系统时&#xff0c;比PSSMs更通用&#xff0c;如语音检测&#xff0c;声纳等一系列信号检测问题&#xff1b;在生信领域&#xff0c;HMMs已经…

SqlServer2005/2008备份模式与恢复模式 1恢复模式

在SQL Server中&#xff0c;除了系统数据库外&#xff0c;你创建的每一个数据库都有三种可供选择的恢复模式: Simple(简单), full(完整), bulk-logged(批量日志)。 下面这条语句可以显示出所有在线数据库的恢复模式:SELECT name, (SELECT DATABASEPROPERTYEX(name, RECOVERY)) …

2021.11.23【bug笔记】丨picard运行报错:Exception in thread “main“ java.lang.UnsupportedClassVersionError

项目场景&#xff1a; RNA-seq对比对后bam文件绘制insert图片 问题描述&#xff1a; 执行picard命令时发生报错&#xff1a; JAVA报错代码&#xff1a; at java.lang.ClassLoader.defineClass1(Native Method)at java.lang.ClassLoader.defineClass(ClassLoader.java:800)at …