http://poj.org/problem?id=1679

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 30120   Accepted: 10778

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 <algorithm>
#include <cstdio> using namespace std; const int N();
int num,n,m;
int fa[N],cnt;
int Fir,MST;
int u,v,w,used[N];
struct Edge
{
int u,v,w;
} edge[N<<]; bool cmp(Edge a,Edge b)
{
return a.w<b.w;
} int find(int x)
{
return fa[x]==x?x:fa[x]=find(fa[x]);
} int Kruskal()
{
int ans=; cnt=;
sort(edge+,edge+m+,cmp);
for(int i=; i<=n; i++) fa[i]=i;
for(int i=; i<=m; i++)
{
int fx=find(edge[i].u),fy=find(edge[i].v);
if(fx!=fy)
{
fa[fx]=fy;
used[++cnt]=i;
ans+=edge[i].w;
}
if(cnt==n-) return ans;
}
return ans;
} int SecKru(int cant)
{
int ans=; cnt=;
for(int i=;i<=n;i++) fa[i]=i;
for(int i=;i<=m;i++)
{
if(cant==i) continue;
int fx=find(edge[i].u),fy=find(edge[i].v);
if(fx!=fy)
{
cnt++;
fa[fx]=fy;
ans+=edge[i].w;
}
if(cnt==n-) return ans;
}
return 0x7fffffff;
} int main()
{
scanf("%d",&num);
for(;num--;)
{
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++)
scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].w);
Fir=Kruskal(); MST=0x7fffffff;
for(int i=;i<n;i++)
{
MST=min(SecKru(used[i]),MST);
}
if(Fir==MST)
printf("Not Unique!\n");
else printf("%d\n",Fir);
}
return ;
}

POJ——T1679 The Unique MST的更多相关文章

  1. poj 1679 The Unique MST 【次小生成树】【模板】

    题目:poj 1679 The Unique MST 题意:给你一颗树,让你求最小生成树和次小生成树值是否相等. 分析:这个题目关键在于求解次小生成树. 方法是,依次枚举不在最小生成树上的边,然后加入 ...

  2. poj 1679 The Unique MST

    题目连接 http://poj.org/problem?id=1679 The Unique MST Description Given a connected undirected graph, t ...

  3. POJ 1679 The Unique MST (最小生成树)

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22668   Accepted: 8038 D ...

  4. poj 1679 The Unique MST(唯一的最小生成树)

    http://poj.org/problem?id=1679 The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submis ...

  5. poj 1679 The Unique MST (判定最小生成树是否唯一)

    题目链接:http://poj.org/problem?id=1679 The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total S ...

  6. POJ 1679 The Unique MST (最小生成树)

    The Unique MST 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/J Description Given a conn ...

  7. poj 1679 The Unique MST【次小生成树】

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24034   Accepted: 8535 D ...

  8. POJ 1679 The Unique MST (次小生成树kruskal算法)

    The Unique MST 时间限制: 10 Sec  内存限制: 128 MB提交: 25  解决: 10[提交][状态][讨论版] 题目描述 Given a connected undirect ...

  9. POJ 1679 The Unique MST 【最小生成树/次小生成树模板】

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22668   Accepted: 8038 D ...

随机推荐

  1. BA--空调静压箱的作用

    空调静压箱的主要作用有两个,一个是降低噪音:一个是提高送风距离.工作原理如下:空调出风从空调风机里面出来的时候,具有很大的风速,同时由于空调风机自身的结构原因,空调的出风并不均衡,空气在风管中相互摩擦 ...

  2. MMORPG大型游戏设计与开发(server 游戏场景 事件)

    游戏截图 场景事件 一个完整的对象一般都拥有事件,至于什么是事件在这里就不多解释了. 在场景中的事件在天龙/武侠世界中的事件包含场景初始化.场景定时器.场景退出.玩家进入场景.角色升级.角色死亡.角色 ...

  3. 初学ToggleButton 点击button,更换button背景图片;再次点击,恢复之前背景图

    上方的图标,R.drawable.register_checked  是选中图片 下方的图标,   R.drawable.register_unchecked 是未选中图片 默认是上方的选中效果.点击 ...

  4. lightoj--1410--Consistent Verdicts(技巧)

    Consistent Verdicts Time Limit: 5000MS   Memory Limit: 32768KB   64bit IO Format: %lld & %llu Su ...

  5. layer最大话.最小化.还原回调方法使用

    <head> <meta charset="UTF-8"> <title>layer最大话.最小化.还原回调方法使用</title> ...

  6. layer-list

    <?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android=" ...

  7. c#可自定义码表的base64加密解密算法类

    000 using System; using System.Collections.Generic; using System.Linq; using System.Text; using Syst ...

  8. RabbitMQ学习笔记(3)----RabbitMQ Worker的使用

    1. Woker队列结构图 这里表示一个生产者生产了消息发送到队列中,但是确有两个消费者在消费同一个队列中的消息. 2. 创建一个生产者 Producer如下: package com.wangx.r ...

  9. ZBrush创建人体模型-ZBrush中ZSphere的基本使用

    本教程我们将学习ZSphere(Z球)在ZBrush®中的基本使用情况,了解它在个人创作过程中发挥着怎样的作用.作为ZBrush中的独特功能之一,ZSphere能够让用户通过清晰的拓扑结构创建基础模型 ...

  10. requests模块的高级用法

    SSL Cert Verification #证书验证(大部分网站都是https) import requests respone=requests.get('https://www.12306.cn ...