PAT T1016 Uniqueness of MST
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的更多相关文章
- 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 ...
- 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 ...
- PAT_A1153#Decode Registration Card of PAT
Source: PAT A1153 Decode Registration Card of PAT (25 分) Description: A registration card number of ...
- 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 ...
- PAT甲级——A1153 DecodeRegistrationCardofPAT【25】
A registration card number of PAT consists of 4 parts: the 1st letter represents the test level, nam ...
- 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 ...
- 1153 Decode Registration Card of PAT
A registration card number of PAT consists of 4 parts: the 1st letter represents the test level, nam ...
- 《转载》PAT 习题
博客出处:http://blog.csdn.net/zhoufenqin/article/details/50497791 题目出处:https://www.patest.cn/contests/pa ...
- PAT Judge
原题连接:https://pta.patest.cn/pta/test/16/exam/4/question/677 题目如下: The ranklist of PAT is generated fr ...
随机推荐
- 1 dev repo organize
码云 注册 组织 创建 仓库 创建 Git版本管理工具 download from https://www.git-scm.com/download/ 克隆/下载 git clone https ...
- 集合转换为数组toArray(),数组转换为集合asList()
package seday12; import java.util.ArrayList;import java.util.Arrays;import java.util.Collection; /** ...
- JS中数组实现(倒序遍历数组,数组连接字符串)
// =================== 求最大值===================================== <script> var arr = [10,35,765 ...
- Gitee 码云 pages 搭建vue项目记录
首先要在.gitignore文件去掉/dist,这个文件默认是不上传的,但是执行打包bulid的时候会生成dist文件,而线上访问的是打包之后的dist文件: vue.config.js文件,如下图箭 ...
- 关于windows nginx不能启动问题的解决,史上最坑系列之一(原文)
我是直接在官方网址下载windows1.6稳定版的nginx,之所以下载它是因为在window下方便学习,更好的在linux安装和学习nginx. 下载到D:\nginx学习\,解压它,并进入启动它 ...
- PAT 1015 Reversible Primes (判断素数)
A reversible prime in any number system is a prime whose "reverse" in that number system i ...
- NTP服务安装及时间同步
1.安装ntp服务命令 yum install -y ntp 2.常用NTP服务器地址: ntp1.aliyun.com ntp2.aliyun.com ntp3.aliyun.com ntp4.al ...
- Vim常用按键大全
Vim完全可以用键盘进行操作.本文将常用的按键归纳总结. 第一部分:一般模式可用的按钮,如光标移动.复制粘贴.查找替换等 移动光标的方法 h, j, k, l 光标向左,下,上,右移动 Ctrl + ...
- sqli-libs(11-22关)
Less_11 点开11关出现登录页面: 1.可以随便输密码和账号,submit之后出现错误,可以看到查询username和password用and连接放在同一个SQL语句中 打开使用burp sui ...
- selenium常用的类库、对应的方法和属性
selenium常用的类库.对应的方法和属性