The Unique MST
The Unique MST
http://poj.org/problem?id=1679
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 36744 | Accepted: 13395 |
Description
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
Output
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<cmath>
#include<algorithm>
#include<string>
#include<cstring>
#include<vector>
#include<queue>
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define N 250500
#define MOD 1e9+7
#define INF 0x3f3f3f3f
typedef long long ll;
using namespace std;
struct sair{
int x,y,v;
}a[];
int fa[];
int n,m;
bool cmp(sair a,sair b){
return a.v<b.v;
} int Find(int x){
int r=x,y;
while(x!=fa[x]){
x=fa[x];
}
while(x!=r){
y=fa[r];
fa[r]=x;
r=y;
}
return x;
} int join(int x,int y){
int xx=Find(x);
int yy=Find(y);
if(xx==yy){
return ;
}
fa[xx]=yy;
return ;
} vector<int>v; int check(int xxx){
int ans=;
int xxxx=;
for(int i=;i<=n;i++){
fa[i]=i;
}
for(int i=;i<=m;i++){
if(i!=xxx){
if(join(a[i].x,a[i].y)){
ans+=a[i].v;
xxxx++;
}
}
}
if(xxxx==n)
return ans;
return -;
} int main(){
std::ios::sync_with_stdio(false);
int t;
cin>>t;
while(t--){
cin>>n>>m;
for(int i=;i<=n;i++){
fa[i]=i;
}
for(int i=;i<=m;i++){
cin>>a[i].x>>a[i].y>>a[i].v; }
int ans1=;
v.clear();
sort(a+,a+m+,cmp);
for(int i=;i<=m;i++){
if(join(a[i].x,a[i].y)){
ans1+=a[i].v;
v.push_back(i);
}
}
int flag=;
for(int i=;i<v.size();i++){
if(check(v[i])==ans1){
flag=;
break;
}
}
if(flag){
cout<<ans1<<endl;
}
else{
cout<<"Not Unique!"<<endl;
}
}
}
The Unique MST的更多相关文章
- POJ1679 The Unique MST[次小生成树]
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 28673 Accepted: 10239 ...
- [poj1679]The Unique MST(最小生成树)
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 28207 Accepted: 10073 ...
- POJ1679The Unique MST(次小生成树)
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 25203 Accepted: 8995 D ...
- poj 1679 The Unique MST
题目连接 http://poj.org/problem?id=1679 The Unique MST Description Given a connected undirected graph, t ...
- POJ 1679 The Unique MST (最小生成树)
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22668 Accepted: 8038 D ...
- POJ 1679 The Unique MST (最小生成树)
The Unique MST 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/J Description Given a conn ...
- poj 1679 The Unique MST(唯一的最小生成树)
http://poj.org/problem?id=1679 The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submis ...
- poj 1679 The Unique MST【次小生成树】
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24034 Accepted: 8535 D ...
- The Unique MST (判断是否存在多个最小生成树)
The Unique MST Time Limit: 10 ...
- POJ 1679:The Unique MST(次小生成树&&Kruskal)
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 19941 Accepted: 6999 D ...
随机推荐
- [转] Maven.pom.xml 配置示例
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mave ...
- 1016 Phone Bills (25 分)
1016 Phone Bills (25 分) A long-distance telephone company charges its customers by the following rul ...
- 共享存储(ISCSI网络存储)
server端:(192.168.100.11) 分区sdb 分别为10G和5G #fdisk /dev/dab …… 同步分区表 #partprobe /dev/sdb # yum install ...
- [UE4]计算2点坐标附近的坐标:线性插值法
float distance = FVector::Distance(SelfLocation, TargetLocation); .f / distance; DrawDebugPoint(GetW ...
- servlet的讲解
https://www.ibm.com/developerworks/cn/java/j-lo-servlet/
- Solr Web增加Basic安全性验证
Solr在5.0版本后,不再提供war包部署的方式,取而代之的是内置了jetty服务, 但是我们发现其中并没有内置任何安全性相关检查,任何人如果知道了我们的外网地址就能直接访问并修改其中的索引 ...
- ASP.NET基于Redis的Provider库
因为session基于本地cache,以前我们自己写分布式缓存,或者数据库存储,或者cookie加密存储,来保存用户状态信息,但较少的直接通过创建一个继承 SessionStateStoreProvi ...
- 2018-2019-2 《网络对抗技术》Exp2 后门原理与实践 Week4 20165233
Exp2 后门原理与实践 实验内容 一.基础问题回答 1.例举你能想到的一个后门进入到你系统中的可能方式? 答:通过访问钓鱼网站,无意下载了一些图片或是文件.而这个图片或文件中携带后门. 2.例举你知 ...
- uva-10344
题意: 枚举23点,注意,数字也是可以枚举的,wa了一次 #include<stdio.h> #include<iostream> #include<sstream> ...
- VisualSVN: 只能修改自己提交日志
上回讲过怎么修改日志信息,这次想提交怎么只能修改自己提交的. 现在演示用户111来修改libra的日志信息 这个公正的SVN出现了 用户111说小样,不让我改,那我修改自己提交的日志总行了吧!! 我改 ...