dfs判断连通块的数量,prim算法建立最小生成树并判断是否唯一~

#include<bits/stdc++.h>
using namespace std;
const int maxn=;
const int inf=1e9;
int g[maxn][maxn];
int d[maxn];
int visit[maxn];
int N,M;
struct edge {
int v1,v2;
};
vector<edge> vi;
void dfs (int s) {
visit[s]=true;
for (int i=;i<=N;i++)
if (g[s][i]!=inf&&visit[i]==false) dfs(i);
}
int dfsTrave () {
int block=;
for (int i=;i<=N;i++) {
if (visit[i]==false) {
dfs(i);
block++;
}
}
return block;
}
int flag=;
int prim (int s) {
fill (d,d+maxn,inf);
fill (visit,visit+maxn,);
d[s]=;
int ans=;
for (int i=;i<=N;i++) {
int u=-,min=inf;
for (int j=;j<=N;j++)
if (visit[j]==false&&d[j]<min) {
u=j;
min=d[j];
}
if (u==-) return -;
visit[u]=;
ans+=d[u];
int num=;
for (int v=;v<=N;v++)
if (visit[v]&&g[u][v]==min) num++;
if (num>) flag++;
for (int v=;v<=N;v++) {
if (visit[v]==false&&g[u][v]!=inf&&g[u][v]<d[v])
d[v]=g[u][v];
}
}
return ans;
}
int main () {
scanf ("%d %d",&N,&M);
int u,v;
for (int i=;i<=N;i++)
for (int j=;j<=N;j++)
g[i][j]=inf;
for (int i=;i<M;i++) {
scanf ("%d %d",&u,&v);
scanf ("%d",&g[u][v]);
g[v][u]=g[u][v];
}
int block=dfsTrave ();
if (block>) {
printf ("No MST\n");
printf ("%d",block);
return ;
}
fill (visit,visit+maxn,false);
int mst=prim();
printf ("%d\n",mst);
if (flag==) printf ("Yes");
else printf ("No");
return ;
}

PAT T1016 Uniqueness of MST的更多相关文章

  1. PAT A1153 Decode Registration Card of PAT (25 分)——多种情况排序

    A registration card number of PAT consists of 4 parts: the 1st letter represents the test level, nam ...

  2. 1153 Decode Registration Card of PAT (25 分)

    A registration card number of PAT consists of 4 parts: the 1st letter represents the test level, nam ...

  3. PAT_A1153#Decode Registration Card of PAT

    Source: PAT A1153 Decode Registration Card of PAT (25 分) Description: A registration card number of ...

  4. PAT Advanced 1153 Decode Registration Card of PAT (25 分)

    A registration card number of PAT consists of 4 parts: the 1st letter represents the test level, nam ...

  5. PAT甲级——A1153 DecodeRegistrationCardofPAT【25】

    A registration card number of PAT consists of 4 parts: the 1st letter represents the test level, nam ...

  6. PAT甲级——1153.Decode Registration Card of PAT(25分)

    A registration card number of PAT consists of 4 parts: the 1st letter represents the test level, nam ...

  7. 1153 Decode Registration Card of PAT

    A registration card number of PAT consists of 4 parts: the 1st letter represents the test level, nam ...

  8. 《转载》PAT 习题

    博客出处:http://blog.csdn.net/zhoufenqin/article/details/50497791 题目出处:https://www.patest.cn/contests/pa ...

  9. PAT Judge

    原题连接:https://pta.patest.cn/pta/test/16/exam/4/question/677 题目如下: The ranklist of PAT is generated fr ...

随机推荐

  1. 吴裕雄 python 机器学习——支持向量机线性分类LinearSVC模型

    import numpy as np import matplotlib.pyplot as plt from sklearn import datasets, linear_model,svm fr ...

  2. python的logging库

    logging库 简介 logging库提供日志打印功能. 值得一提的是,不仅能打印到日志文件,还能打印到控制台. 日志级别 logging一共分为5个级别,从低到高依次为:  DEBUG<IN ...

  3. Go_ioutil包

    1. ioutil包的方法 // Discard 是一个 io.Writer 接口,调用它的 Write 方法将不做任何事情 // 并且始终成功返回. var Discard io.Writer = ...

  4. 瀑布流无限加载infinitescroll插件与masonry插件使用

    masonry官网地址http://masonry.desandro.com/,infinitescroll官网地址http://www.infinite-scroll.com/ 无限滚动原理:无限滚 ...

  5. 2019,.Net开发者的高光时刻

    随着微软发布的一系列关于Windows..net和C#的公告,.Net开发者将在2019年,迎来自己的高光时刻,毕竟“世界上只有少数几种语言是多功能的,而没有一个像C#那样干净整洁.” 一.现在学C# ...

  6. 面试问烂的 Spring AO,全文详解

    本人免费整理了Java高级资料,涵盖了Java.Redis.MongoDB.MySQL.Zookeeper.Spring Cloud.Dubbo高并发分布式等教程,一共30G,需要自己领取.传送门:h ...

  7. [JavaScript] 两个数相除有余数时结果加1

    实现代码 ; ; ?(total/item):(Math.floor(total/item)+); console.log(page)

  8. jquery--获取input radio单选框的值

    html <input type="radio" name="sex" value="man" checked> man < ...

  9. iOS-动态库创建(详解)

    https://blog.csdn.net/LisztCoder/article/details/78132147 版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原 ...

  10. Kafka .net 开发入门

    Kafka安装 首先我们需要在windows服务器上安装kafka以及zookeeper,有关zookeeper的介绍将会在后续进行讲解. 在网上可以找到相应的安装方式,我采用的是腾讯云服务器,借鉴的 ...