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. Text Infilling解读

    多头自注意力token解码器,该解码器能够对过去和未来的信息进行condition处理,适合填充任务:自注意力机制尤其适合填充文本,因为它可以为每个空白处从左到右及从右到左双向建模,为全部语义进行有效 ...

  2. C语言最重要的知识点(电子文档)

    总体上必须清楚的: 1)程序结构是三种:  顺序结构 .选择结构(分支结构).循环结构.  2)读程序都要从main()入口, 然后从最上面顺序往下读(碰到循环做循环,碰到选择做选择),有且只有一个m ...

  3. 解决MinGW运行时提示丢失libgmp-10.dll

    如何解决刚安装的MinGW提示"丢失xxx.dll" 解决方案: 1.配置环境变量,出现这种问题的主要原因是因为没配置环境变量,网上的大多数解决方案也是基于这个的. 2.安装包损坏 ...

  4. c# pcm

    using System; using System.IO; using System.Text; using System.Windows.Forms; using System.Runtime.I ...

  5. 在elementui表单中实现对vue-quill-editor富文本编辑器内容的绑定

    1.v-model(表单标签双向绑定指令) v-model相当于:value=""和@input=""的结合 代码1: <input type=" ...

  6. vue中使用vue-i18n 一个简单的国际化操作

    1.安装:npm install vue-i18n --save-dev 2.在main.js文件中引入: import VueI18n from 'vue-i18n' Vue.use(VueI18n ...

  7. crawlSpider全站爬取 分布式

    # 如何提升scrapy爬取数据的效率? 推荐: 单线程加异步协程 增加并发: 默认scrapy开启的并发线程为32个,可以适当进行增加.在settings.py中修改 CONCURRENT_REQU ...

  8. 【转载】MyEclipse6.5 KeyGen

    输入自己的注册名,生成注册码,完成注册 package keyGenerate; import java.io.BufferedReader; import java.io.IOException; ...

  9. SpringCloud全家桶学习之分布式配置中心----Config(七)

    一.概述 (1)背景 微服务意味着将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统中出现大量的服务.由于每个服务都需要配置必要的配置信息才能运行,所以一套集中式的.动态的配置管理 ...

  10. try catch和if else

    当错误发生时,当事情出问题时,JavaScript 引擎通常会停止,并生成一个错误消息.描述这种情况的技术术语是:JavaScript 将抛出一个错误. try 语句允许我们定义在执行时进行错误测试的 ...