Non-Yekaterinburg Subway

Time limit: 1.0 second
Memory limit: 64 MB
A
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.
Your
task given a town plan to determine the minimal possible number of
bridges that is necessary to use in the subway construction.

Input

The first line contains three integers separated with a space: N (the number of islands, 1 ≤ N ≤ 10000), K (the number of tunnels, 0 ≤ K ≤ 12000) and M (the number of bridges, 0 ≤ M ≤ 12000). Then there are K lines; each line consists of two integers — the numbers of islands, connected with the corresponding tunnel. The last M lines define bridges in the same format.

Output

the minimal number of bridges necessary for the subway construction.

Sample

input output
6 3 4
1 2
2 3
4 5
1 3
3 4
4 6
5 6
2
Problem Author: Magaz Asanov (prepared Igor Goldberg)
【分析】一个小镇由很多小岛组成,小岛之间原来有桥或者隧道。先要在某些小岛间建地铁,如果某两个小岛之间原来有隧道,就直接建。如果是桥,则不建,且要保证桥和地铁要使所有岛屿联通。可用最小生成树做,桥的话权值为1,隧道为0.一开始用二维数组存边,MLE,像这种点比较多的,就得用结构体了,这里用的Kruskal.
#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(最小生成树)的更多相关文章

  1. URAL(timus) 1280 Topological Sorting(模拟)

    Topological Sorting Time limit: 1.0 secondMemory limit: 64 MB Michael wants to win the world champio ...

  2. Android时区及语言代码

    1. 设置默认时区   PRODUCT_PROPERTY_OVERRIDES += \         persist.sys.timezone=Asia/Shanghai\ 注:搜索“persist ...

  3. 我的Android进阶之旅------>Android 设置默认语言、默认时区

    1. 设置默认时区 PRODUCT_PROPERTY_OVERRIDES += \ persist.sys.timezone=Asia/Shanghai\ 注:搜索“persist.sys.timez ...

  4. Android系统移植与调试之------->如何修改Android的默认语言、默认时区

    修改device/other/TBDG1073/ system.prop文件 1.设置默认语言 找到device/other/TBDG1073/ system.prop文件,修改属性ro.produc ...

  5. MTK Android中设置默认时区

    设置默认时区 PRODUCT_PROPERTY_OVERRIDES += \ persist.sys.timezone=Asia/Shanghai\ 注:搜索“persist.sys.timezone ...

  6. ural 1272. Non-Yekaterinburg Subway

    1272. Non-Yekaterinburg Subway Time limit: 1.0 secondMemory limit: 64 MB A little town started to co ...

  7. URAL 1416 Confidential --最小生成树与次小生成树

    题意:求一幅无向图的最小生成树与最小生成树,不存在输出-1 解法:用Kruskal求最小生成树,标记用过的边.求次小生成树时,依次枚举用过的边,将其去除后再求最小生成树,得出所有情况下的最小的生成树就 ...

  8. URAL 1160 Network(最小生成树)

    Network Time limit: 1.0 secondMemory limit: 64 MB Andrew is working as system administrator and is p ...

  9. timus 1982 Electrification Plan(最小生成树)

    Electrification Plan Time limit: 0.5 secondMemory limit: 64 MB Some country has n cities. The govern ...

随机推荐

  1. 展辰涂料如何利用K2BPM加强流程管控?

    展辰涂料集团股份有限公司是一家集环保涂料研发.生产.销售和工程施工为一体的国家级高新技术企业.展辰涂料集团股份有限公司是我国最大的民族涂料企业之一,自成立以来就高度注重产品技术研发,截至目前,公司已取 ...

  2. hdu 1035 (usage of sentinel, proper utilization of switch and goto to make code neat) 分类: hdoj 2015-06-16 12:33 28人阅读 评论(0) 收藏

    as Scott Meyers said in his book Effective STL, "My advice on choosing among the sorting algori ...

  3. 【转】Nginx+Tomcat+Memcached集群Session共享

    cookie是怎样工作的? 例 如,我们创建了一个名字为login的Cookie来包含访问者的信息,创建Cookie时,服务器端的Header如下面所示,这里假设访问者的注册名 是“Michael J ...

  4. Android 导入jar包 so模块--导入放置的目录

    Android视图下 app目录下的jniLibs 对应Project 视图app下的libs目录 把jar包或者带so文件的文件夹(一般以处理器型号命名如:arbeabi  arm64-v8a)复制 ...

  5. iOS 端的 UI 聊天组件ChatKit及代码实现

    ChatKit 是一个免费且开源的 UI 聊天组件,自带云服务器,自带推送,支持消息漫游,消息永久存储.底层聊天服务基于LeanCloud(原名 AVOS ) 的 IM 实时通信服务「LeanMess ...

  6. UIlabel设置不同的颜色

    NSString *string = @"注册过程中出现问题,致电400-650-5167联系会养车工作人员";    NSRange range = [string rangeO ...

  7. 二、XML约束

    XML约束有dtd约束和Schema约束两种 dtd约束:可以在xml内部写dtd约束也可以在xml中引用外部dtd文件 book.dtd<!ELEMENT 书架 (书+)>    < ...

  8. Cisco路由器的6种模式

    Cisco路由器的6种模式 -------------------------------------------------------------------------------------- ...

  9. (spring-第1回【IoC基础篇】)Spring容器中Bean的生命周期

    日出日落,春去秋来,花随流水,北雁南飞,世间万物皆有生死轮回.从调用XML中的Bean配置信息,到应用到具体实例中,再到销毁,Bean也有属于它的生命周期. 人类大脑对图像的认知能力永远高于文字,因此 ...

  10. 为什么要urlencode

    为什么要urlencode  1.为了正常获取值  字符 特殊字符的含义 URL编码 & 分隔不同的变量值对 %26 = 用来连接键和值 %3D ? 表示查询字符串的开始 %3F # 用来标志 ...