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. SQL实现group by 分组后组内排序

    在一个月黑风高的夜晚,自己无聊学习的SQL的时候,练习,突发奇想的想实现一个功能查询,一张成绩表有如下字段,班级ID,英语成绩,数据成绩,语文成绩如下图 实现 查询出 每个班级英语成绩最高的前两名的记 ...

  2. 【音乐欣赏】《Wrong》 - Far Out / Emilia Ali

    曲名:Wrong 作者:Far Out / Emilia Ali [00:16.03]Ride body on mine [00:18.07]Griping your waist as I was o ...

  3. office自签名证书

    在 Office安装目录,找到 SELFCERT 文件,双击打开填写名称,生成

  4. Scrapy 命令

    Scrapy提供了两种类型的命令.一种必须在Scrapy项目中运行(针对项目(Project-specific)的命令),另外一种则不需要(全局命令).全局命令在项目中运行时的表现可能会与在非项目中运 ...

  5. win server 挂载

    新建服务器角色,选择[NFS服务器]. mount -o nolock \\x.x.x.x.x.x\! z:/*链接到*/

  6. ios 底部用定位 fixed。在软件盘出来后,页面元素被顶上去一部分,fixed定位的footer也跑到了上面去。解决方法

    ios 底部用定位 fixed.在软件盘出来后,页面元素被顶上去一部分,fixed定位的footer也跑到了上面去.解决方法 $("input").focus(function() ...

  7. python序列的拆分

    1 变量个数和序列长度相等 使用赋值语句可以将序列拆分,然后赋值给多个变量,形式如下: 变量1,变量2,...,变量n = 序列或可迭代对象 若变量个数和序列的元素个数不一致,将导致ValueErro ...

  8. Net Core解决ZipFile解压中文出现乱码

    一.在main方法中添加 Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); 二.解压添加 //sourceArchiveFi ...

  9. Django中url name

    花了好长时间才明白这个name参数的含义.便写下来了备忘 当我们在url的时候,一般情况下都是使用很明确的url地址.如在网页里面使用<a href="/login"> ...

  10. .NET中的字符串(2):你真的了解.NET中的String吗?

    概述 String在任何语言中,都有它的特殊性,在.NET中也是如此.它属于基本数据类型,也是基本数据类型中唯一的引用类型.字符串可以声明为常量,但是它却放在了堆中.希望通过本文能够使大家对.NET中 ...