URAL(timus) 1272 Non-Yekaterinburg Subway(最小生成树)
Non-Yekaterinburg Subway
Memory limit: 64 MB
little town started to construct a subway. The peculiarity of the town
is that it is located on small islands, some of them are connected with
tunnels or bridges. The mayor is sure that the subway is to be under the
ground, that’s why the project must use the less bridges the better.
The only request for the subway is that the townsmen could get by metro
(may be with changes) from every island to every island. Fortunately, we
know that there is enough tunnels and bridges for it. It was decided to
construct as less passages from island to island as possible to save
money.
task given a town plan to determine the minimal possible number of
bridges that is necessary to use in the subway construction.
Input
Output
Sample
input | output |
---|---|
6 3 4 |
2 |
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <time.h>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#define inf 0x3f3f3f3f
#define mod 10000
typedef long long ll;
using namespace std;
const int N=;
const int M=;
struct Edg {
int v,u;
int w;
} edg[M];
bool cmp(Edg g,Edg h) {
return g.w<h.w;
}
int n,m,maxn,cnt,k;
int parent[N];
int a[N];
void init() {
for(int i=; i<n; i++)parent[i]=i;
}
void Build() {
int u,v;
while(k--){
scanf("%d%d",&u,&v);
edg[++cnt].u=u;
edg[cnt].v=v;
edg[cnt].w=;
}
while(m--){
scanf("%d%d",&u,&v);
edg[++cnt].u=u;
edg[cnt].v=v;
edg[cnt].w=;
}
sort(edg,edg+cnt+,cmp);
}
int Find(int x) {
if(parent[x] != x) parent[x] = Find(parent[x]);
return parent[x];
}
void Union(int x,int y) {
x = Find(x);
y = Find(y);
if(x == y) return;
parent[y] = x;
}
void Kruskal() {
int sum=;
int num=;
int u,v;
for(int i=; i<=cnt; i++) {
u=edg[i].u;
v=edg[i].v;
if(Find(u)!=Find(v)) {
sum+=edg[i].w;
num++;
Union(u,v);
}
if(num>=n-) {
printf("%d\n",sum);
break;
}
}
}
int main() {
scanf("%d%d%d",&n,&k,&m);
if(m==)printf("0\n"),exit();
if(n==)printf("0\n"),exit();
cnt=-;
init();
Build();
Kruskal();
return ;
}
URAL(timus) 1272 Non-Yekaterinburg Subway(最小生成树)的更多相关文章
- URAL(timus) 1280 Topological Sorting(模拟)
Topological Sorting Time limit: 1.0 secondMemory limit: 64 MB Michael wants to win the world champio ...
- Android时区及语言代码
1. 设置默认时区 PRODUCT_PROPERTY_OVERRIDES += \ persist.sys.timezone=Asia/Shanghai\ 注:搜索“persist ...
- 我的Android进阶之旅------>Android 设置默认语言、默认时区
1. 设置默认时区 PRODUCT_PROPERTY_OVERRIDES += \ persist.sys.timezone=Asia/Shanghai\ 注:搜索“persist.sys.timez ...
- Android系统移植与调试之------->如何修改Android的默认语言、默认时区
修改device/other/TBDG1073/ system.prop文件 1.设置默认语言 找到device/other/TBDG1073/ system.prop文件,修改属性ro.produc ...
- MTK Android中设置默认时区
设置默认时区 PRODUCT_PROPERTY_OVERRIDES += \ persist.sys.timezone=Asia/Shanghai\ 注:搜索“persist.sys.timezone ...
- ural 1272. Non-Yekaterinburg Subway
1272. Non-Yekaterinburg Subway Time limit: 1.0 secondMemory limit: 64 MB A little town started to co ...
- URAL 1416 Confidential --最小生成树与次小生成树
题意:求一幅无向图的最小生成树与最小生成树,不存在输出-1 解法:用Kruskal求最小生成树,标记用过的边.求次小生成树时,依次枚举用过的边,将其去除后再求最小生成树,得出所有情况下的最小的生成树就 ...
- URAL 1160 Network(最小生成树)
Network Time limit: 1.0 secondMemory limit: 64 MB Andrew is working as system administrator and is p ...
- timus 1982 Electrification Plan(最小生成树)
Electrification Plan Time limit: 0.5 secondMemory limit: 64 MB Some country has n cities. The govern ...
随机推荐
- bzoj 2440 简单莫比乌斯反演
题目大意: 找第k个非平方数,平方数定义为一个数存在一个因子可以用某个数的平方来表示 这里首先需要考虑到二分才可以接下来做 二分去查找[1 , x]区间内非平方数的个数,后面就是简单的莫比乌斯反演了 ...
- URAL 1519 基础插头DP
题目大意: 给定一个图,一部分点'*'作为障碍物,求经过所有非障碍点的汉密尔顿回路有多少条 基础的插头DP题目,对于陈丹琦的论文来说我觉得http://blog.sina.com.cn/s/blog_ ...
- 跨域SSO的实现
翻译自CodeProject网站ASP.NET9月份最佳文章:Single Sign On (SSO) for cross-domain ASP.NET applications. 翻译不妥之处还望大 ...
- awt可视化界面上传数据到mysql,jsp通过jdbc方式查询数据库,并将结果打印在网页上
今天尝试写一个小demo实现下之前看过的代码,目的了解不同文件的数据访问,掌握如何获取前台数据,如何将数据库的数据在前端页面展示. awt可视化界面可已实现提交数据到数据库,也可查询数据在控制台打印. ...
- Linux Lab and project latest
samba : start your samba service netlogon syslog vi /usr/local/samba vi /usr/samba/etc/smb.conf smbc ...
- HTML5实战教程———开发一个简单漂亮的登录页面
最近看过几个基于HTML5开发的移动应用,比如臭名昭著的12036移动客户端就是主要使用HTML5来实现的,虽然还是有点反应迟钝,但已经比较流畅了,相信随着智能手机的配置越来越高性能越来越好,会越来越 ...
- 2013年8月份第4周51Aspx源码发布详情
迷你桌面闹钟源码 2013-8-27 [VS2010]功能介绍:实现了定时闹钟的功能,可以设置闹钟最前端显示.感兴趣的可以下载学习. BR个人博客系统(课程设计)源码 2013-8-27 [VS2 ...
- mysql批量写入
MySQL批量写入语法是: INSERT INTO table (field1,field2,field3) VALUES (“a”,”b”,”c”), (“a1”,”b1”,”c1”),(“a2”, ...
- 解决办法-错误:Access denied for user 'root'@'localhost' - java
如下更改密码即可 mysql> UPDATE user SET Password=PASSWORD('newpassword') where USER='root';mysql> FLUS ...
- eclipse GIT使用
新建工程(要和GIT上同名,同类型)->右键->team->add to index->commit->更新config文件->remote: fetch from ...