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. linux建立动态库的软链接

    复制动态库: /home/wmz/anaconda3/lib/ 删除原链接: 建立新链接: /home/wmz/anaconda3/lib/libstdc++.so. 问题的起源是,安装anacond ...

  2. angular 输入框自动绑定值最长为16位,超过16位则会报错

    最近发现angular在使用input输入框的ng-model绑定scope变量的时候,发现,输入长串的数字将会出错.代码如下: <html> <head> <meta ...

  3. 从TCL欲成JDI股东看,面板行业进入“群架”时代

    当下,屏幕早已成为第一入口.PC.智能手机.平板电脑.电视.家庭智能终端.智慧交通.智能穿戴设备.汽车中控大屏--种种设备都是以屏幕为最重要的视觉呈现方式,让人们在一个个奇幻世界中畅游.也正是因为屏幕 ...

  4. 基于selenium+Python3.7+yaml+Robot Framework的UI自动化测试框架

    前端自动化测试框架 项目说明 本框架是一套基于selenium+Python3.7+yaml+Robot Framework而设计的数据驱动UI自动化测试框架,Robot Framework 作为执行 ...

  5. idea2019.2激活至2089年!

    上图! 激活到2089年8月,绝对够用! ​ 注意:在激活之前,无需改动 host 文件. 资料自取:链接:https://pan.baidu.com/s/1MzX5ewt6lbzHYuggP5sGE ...

  6. Servlt入门

    Servlt入门 java的两种体系结构 C/S (客户端/服务器)体系结构  通讯效率高且安全,但系统占用多 B/S (浏览器/服务器)体系结构    节约开发成本 C/S (客户端/服务器)体系结 ...

  7. 简单的jquery Ajax进行登录!

    本案例包括login.html.login.php.jquery-1.12.0.min.js三个文件,只需将这三个文件放到同一文件夹下,即可运行. login.html: <!DOCTYPE h ...

  8. markdown列表

    Markdown 列表 Markdown 支持有序列表和无序列表. 无序列表使用星号(*).加号(+)或是减号(-)作为列表标记: * 第一项 * 第二项 * 第三项 + 第一项 + 第二项 + 第三 ...

  9. 【PAT甲级】1090 Highest Price in Supply Chain (25 分)

    题意: 输入一个正整数N(<=1e5),和两个小数r和f,表示树的结点总数和商品的原价以及每向下一层价格升高的幅度.下一行输入N个结点的父结点,-1表示为根节点.输出最深的叶子结点处购买商品的价 ...

  10. mui 获取本地APP本版号的两种方式

    第一种 mui.plusReady(function () { mui.getJSON("manifest.json", null, function (data) { vum = ...