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 ...
随机推荐
- Section 1.4 The Clocks
0 0 虽然不知不觉做到了Section 1.4了,但是都没有把做题的想法和代码发到这里… 本来今天想从Section 1.2补起来然后发现之前做的题都忘了…(Name That Number那道题是 ...
- RPI学习--环境搭建_默认启动桌面/终端修改
参见:http://elinux.org/RPi_raspi-config 首次运行Raspbian会自动进入设置,往后也可以重新进入设置: $ sudo raspi-config 选项3 Enabl ...
- 手机app常见bug积累
经过一年的测试工作,以下是手机APP比较容易出现的错误.之后如果发现了还会继续添加,修改.1.翻页手机客户端,内容超过一页时,上拉加载更多内容,加载错误(容易出现数据重复,图片和文章不匹配,图片重复加 ...
- 黑客界大拿tombkeeper文章:怎么学好技术成为技术大拿(题目我自拟的)
这两天论坛上又有人开始抱怨世风日下,大家都现实了,都不开放了,不交流了.对这种“月经贴”,我基本上已经习惯了,不过因为吃了粉皮炖鸡,心情比较好,于是就说了两句. 三四年前,当时我对人性的看法还不像现在 ...
- 带弹出列表的EditText
最近做的一个Andriod里有一个这个要求,一个东西需要输入,但同时可以在列表直接选择.看到这个需求,瞬间想到了QQ的登录界面,那个账号输入的控件正式我所需要的. 这个账号输入框右边有一个按钮,点击可 ...
- JS内置对象
字符串对象 <script> //字符串对象 var str = "Hello worldlsgjlsjg"; document.write('string.lengt ...
- MongoDB error: couldn't connect to server 127.0.0.1:27017 src/mongo/shell/mongo.js(转)
rror: couldn't connect to server 127.0.0.1:27017 src/mongo/shell/mongo.js 一般这种情况就是:自己指定的数据库,所以不能.自动加 ...
- 一篇文章教你学会基础的HTML
html是学习做网页的基础,漂亮的网页与布局就是由有些html代码组成,大家看完这篇文章就可以简单的了解html了,多写多练 如果你不致力于成为美工的话,那么作为开发人员,可以读懂HTML.必 ...
- vc设置窗口透明
::SetWindowLong(GetSafeHwnd(), GWL_EXSTYLE, ::GetWindowLongPtr(GetSafeHwnd(), GWL_EXSTYLE) | WS_EX_L ...
- C++ inline weak symbol and so on
关于inline这个关键字,听到强调得最多的是,它只是一种对于编译器的建议,而非强制执行的限定. 但事实上,即使这个优化最终由于函数太过复杂的原因没有达成,加上inline关键字(还有在类定义中直接定 ...