URAL(timus) 1272 Non-Yekaterinburg Subway(最小生成树)

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=10005;
const int M=100005;
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=0; 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=0;
    }
    while(m--){
        scanf("%d%d",&u,&v);
        edg[++cnt].u=u;
        edg[cnt].v=v;
        edg[cnt].w=1;
    }
    sort(edg,edg+cnt+1,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=0;
    int num=0;
    int u,v;
    for(int i=0; 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-1) {
            printf("%d\n",sum);
            break;
        }
    }
}
int main() {
        scanf("%d%d%d",&n,&k,&m);
        if(m==0)printf("0\n"),exit(0);
        if(n==1)printf("0\n"),exit(0);
        cnt=-1;
        init();
        Build();
        Kruskal();
    return 0;
}

时间: 2024-10-06 15:03:23

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 championship in programming and decided to study N subjects (for convenience we will number these subjects from 1 to N). Michael has worked out a study plan

平方剩余

平方剩余 POJ:1808 链接:http://poj.org/problem?id=1808 题意:给定a,n(n为质数) 问 x^2 ≡ a (mod n) 是否有解 可以用a^((n - 1)/2) ≡ ±1(mod n) 当为1是二次剩余,为-1是非二次剩余 但上述方法仅仅是判断是否有解,下面的方法能够求最小整数解 Ural(Timus) 1132 链接: http://acm.timus.ru/problem.aspx?space=1&num=1132 题意:给定a,n(n为质数) 问

android中设置默认语言 默认时区

全志平台配置文件路径: android4.2\device\softwinner\wing-common\ProductCommon.mk 系统语言默认中文配置如下 系统语言默认英语配置如下: 其它国家语言与时区详解参考: 1. 设置默认时区 PRODUCT_PROPERTY_OVERRIDES += \ persist.sys.timezone=Asia/Shanghai\ 注:搜索"persist.sys.timezone",并更改其值 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 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

Ural 1982 Electrification Plan (prim最小生成树)

很明显的最小生成树模板题 多点生成 [cpp] view plaincopy #include<bits/stdc++.h> using namespace std; int n,k,a; int dist[120],m[120][120]; bool p[120]; void prim() { for(int i=1;i<=n;i++) { if(!p[i]) { int Min=100020; for(int j=1;j<=n;j++) { if(p[j]&&m

hdu 1272 小希的迷宫(并查集 最小生成树)

http://acm.hdu.edu.cn/showproblem.php?pid=1272 这题要求任意两个房间都相通又不能有环 即通过并查集求出是否构成最小生成树 #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; #define maxn 100000 int fa[maxn+10]; int num[maxn+10

URAL - 1416 Confidential (最小生成树与次小生成树)

题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=12614 本文链接:http://www.cnblogs.com/Ash-ly/p/5495851.html 题意: 给你N个点,以及M条边,让你计算是否存在最小生成树和次小生成树,如果存在打印出权值,否则打印-1. 思路: 很直接的一道题,关于求次小生成树的方法,在我的另外一篇文章中:http://www.cnblogs.com/Ash-ly/p/5494975.

timus 1982 Electrification Plan(最小生成树)

Electrification Plan Time limit: 0.5 secondMemory limit: 64 MB Some country has n cities. The government has decided to electrify all these cities. At first, power stations in k different cities were built. The other cities should be connected with t

hust/ural Penguin-Avia 最小生成树

题型:无向图,删边加边有代价,问最小生成树同时代价最小题意:处理:DFS找连通分支(无向图,一定是强连通分支),删去环边,再将再连通分支树的根节点连接. bug ; 在go的意义上混淆过,分析如下 数据结构: const int add_e = 2; const int delete_e = 3; const int ok_e = 4; int n,d,a; long long cost; int m[110][110],go[110]; // m是输入的图(值为0/1),处理后值为234(对应