时间限制:1 秒

内存限制:32 兆

特殊判题:否

提交:376

解决:132

题目描述:

It is vitally important to have all the cities connected by highways in a war. If a city is occupied by the enemy, all the highways from/toward that city are closed. We must know immediately if we need to repair any other highways to keep the rest of the cities
connected. Given the map of cities which have all the remaining highways marked, you are supposed to tell the number of highways need to be repaired, quickly.

For example, if we have 3 cities and 2 highways connecting city1-city2 and city1-city3.
Then if city1 is occupied by the enemy, we must have 1 highway repaired, that is the highway city2-city3.

输入:

Each input file contains one test case. Each case starts with a line containing 3 numbers N (<1000), M and K, which are the total number of cities, the number of remaining highways, and the number of cities to be checked, respectively. Then M lines follow,
each describes a highway by 2 integers, which are the numbers of the cities the highway connects. The cities are numbered from 1 to N. Finally there is a line containing K numbers, which represent the cities we concern.

输出:

For each of the K cities, output in a line the number of highways need to be repaired if that city is lost.

样例输入:
3 2 3
1 2
1 3
1 2 3
样例输出:
1
0
0

思路:

题意是求,若第i个城市被占领了,需要另外修几条路。

我的思路是对每种情况求并查集,求集合个数num,num-1就是需要修的路。

代码:

#include <stdio.h>

#define N 1000
#define M 1000000 typedef struct node {
int x;
int y;
} ROAD; int n;
int pre[N+1];
int count[N+1];
int num; void init()
{
for (int i=1; i<=n; i++)
{
pre[i] = i;
count[i] = 1;
}
num = n;
} int find(int i)
{
while (i != pre[i])
i = pre[i];
return i;
} int combine(int i, int j)
{
int a = find(i);
int b = find(j);
if (a != b)
{
if (count[a] > count[b])
{
pre[b] = a;
count[a] += count[b];
count[b] = 0;
}
else
{
pre[a] = b;
count[b] += count[a];
count[a] = 0;
}
num --;
return 1;
}
else
return 0;
} int main(void)
{
int m, k, i, j;
ROAD r[M];
int concern[N+1]; while (scanf("%d%d%d", &n, &m, &k) != EOF)
{
for(i=0; i<m; i++)
scanf("%d%d", &r[i].x, &r[i].y);
for(j=0; j<k; j++)
{
init();
scanf("%d", &concern[j]);
for(i=0; i<m; i++)
{
if (r[i].x != concern[j] && r[i].y != concern[j])
combine(r[i].x, r[i].y);
if (num == 2)
break;
}
printf("%d\n", num-2);
}
} return 0;
}
/**************************************************************
Problem: 1325
User: liangrx06
Language: C
Result: Accepted
Time:190 ms
Memory:8664 kb
****************************************************************/

九度OJ 1325:Battle Over Cities(城市间的战争) (并查集)的更多相关文章

  1. 九度OJ 1156:谁是你的潜在朋友 (并查集)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5802 解决:2593 题目描述: "臭味相投"--这是我们描述朋友时喜欢用的词汇.两个人是朋友通常意味着他们存在着许多 ...

  2. pat 1013 Battle Over Cities(25 分) (并查集)

    1013 Battle Over Cities(25 分) It is vitally important to have all the cities connected by highways i ...

  3. 【九度OJ】题目1012:畅通工程 解题报告

    [九度OJ]题目1012:畅通工程 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1012 题目描述: 某省调查城镇交通状况 ...

  4. 九度oj 题目1087:约数的个数

    题目链接:http://ac.jobdu.com/problem.php?pid=1087 题目描述: 输入n个整数,依次输出每个数的约数的个数 输入: 输入的第一行为N,即数组的个数(N<=1 ...

  5. 九度OJ 1502 最大值最小化(JAVA)

    题目1502:最大值最小化(二分答案) 九度OJ Java import java.util.Scanner; public class Main { public static int max(in ...

  6. 九度OJ,题目1089:数字反转

    题目描述: 12翻一下是21,34翻一下是43,12+34是46,46翻一下是64,现在又任意两个正整数,问他们两个数反转的和是否等于两个数的和的反转. 输入: 第一行一个正整数表示测试数据的个数n. ...

  7. 九度OJ 1500 出操队形 -- 动态规划(最长上升子序列)

    题目地址:http://ac.jobdu.com/problem.php?pid=1500 题目描述: 在读高中的时候,每天早上学校都要组织全校的师生进行跑步来锻炼身体,每当出操令吹响时,大家就开始往 ...

  8. 九度OJ 1531 货币面值(网易游戏2013年校园招聘笔试题) -- 动态规划

    题目地址:http://ac.jobdu.com/problem.php?pid=1531 题目描述: 小虎是游戏中的一个国王,在他管理的国家中发行了很多不同面额的纸币,用这些纸币进行任意的组合可以在 ...

  9. 九度OJ 1024 畅通工程 -- 并查集、贪心算法(最小生成树)

    题目地址:http://ac.jobdu.com/problem.php?pid=1024 题目描述:     省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但 ...

随机推荐

  1. Java 堆内存模型

    堆内存 Java 中的堆是 JVM 所管理的最大的一块内存空间,主要用于存放各种类的实例对象. 在 Java 中.堆被划分成两个不同的区域:新生代 ( Young ).老年代 ( Old ).新生代 ...

  2. [Other] An Overview of Arrays and Memory

    One integer takes 32bit in memory, 1 byte = 8bits, therefore one integer takes 4 bytes. Now let's as ...

  3. 容量测试之tcpcopy引流模式

    tcpcopy 给用户提供了很多命令参数来修改引流的模式和设置,详细可以查阅手册.在这里把几种常见的引流方式做个归纳小结,以tcpcopy传统架构使用命令举例. 1.分布式引流 用法:Tcpcopy可 ...

  4. 数组练习:各种数组方法的使用&&事件练习:封装兼容性添加、删除事件的函数&&星级评分系统

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  5. apache环境下禁止某文件夹内运行PHP脚本、禁止访问文件或目录执行权限的设置方法

    apache环境下禁止某文件夹内运行PHP脚本.禁止访问文件或目录执行权限的设置方法   首先我们来看两段对上传目录设置无权限的列子,配置如下: <Directory "要去掉PHP执 ...

  6. java Map根据value排序

    通用方法 public class MapUtil { public static <K, V extends Comparable<? super V>> Map<K, ...

  7. 命令行添加pod示例

    1.创建AlamFireDemo 工程,关闭工程 2.进入到工程目录 执行 pod init 命令 生成 PodFile文件 3.vi PodFile编辑该文件 启用:platform :ios, ' ...

  8. 【原创】Loadrunner使用json格式请求数据并参数化

    (2015-04-10 16:10:41) 转载▼ 标签: loadrunner json 参数化 web_custom_request 分类: 性能测试 请求自定义的http文件用函数:web_cu ...

  9. BF的真正意义

    http://www.jianglb.com/2010/11/25/whatisbf.html一个小男孩对小女孩说:“我是你的BF.”女孩扬起童真的脸,问:“什么是BF?”男孩嘻嘻笑道:“是BestF ...

  10. Android开发之httpclient文件上传实现

    文件上传可能是一个比較耗时的操作,假设为上传操作带上进度提示则能够更好的提高用户体验,最后效果例如以下图: 项目源代码:http://download.csdn.net/detail/shinay/4 ...