九度OJ 1325:Battle Over Cities(城市间的战争) (并查集)
时间限制: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(城市间的战争) (并查集)的更多相关文章
- 九度OJ 1156:谁是你的潜在朋友 (并查集)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5802 解决:2593 题目描述: "臭味相投"--这是我们描述朋友时喜欢用的词汇.两个人是朋友通常意味着他们存在着许多 ...
- pat 1013 Battle Over Cities(25 分) (并查集)
1013 Battle Over Cities(25 分) It is vitally important to have all the cities connected by highways i ...
- 【九度OJ】题目1012:畅通工程 解题报告
[九度OJ]题目1012:畅通工程 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1012 题目描述: 某省调查城镇交通状况 ...
- 九度oj 题目1087:约数的个数
题目链接:http://ac.jobdu.com/problem.php?pid=1087 题目描述: 输入n个整数,依次输出每个数的约数的个数 输入: 输入的第一行为N,即数组的个数(N<=1 ...
- 九度OJ 1502 最大值最小化(JAVA)
题目1502:最大值最小化(二分答案) 九度OJ Java import java.util.Scanner; public class Main { public static int max(in ...
- 九度OJ,题目1089:数字反转
题目描述: 12翻一下是21,34翻一下是43,12+34是46,46翻一下是64,现在又任意两个正整数,问他们两个数反转的和是否等于两个数的和的反转. 输入: 第一行一个正整数表示测试数据的个数n. ...
- 九度OJ 1500 出操队形 -- 动态规划(最长上升子序列)
题目地址:http://ac.jobdu.com/problem.php?pid=1500 题目描述: 在读高中的时候,每天早上学校都要组织全校的师生进行跑步来锻炼身体,每当出操令吹响时,大家就开始往 ...
- 九度OJ 1531 货币面值(网易游戏2013年校园招聘笔试题) -- 动态规划
题目地址:http://ac.jobdu.com/problem.php?pid=1531 题目描述: 小虎是游戏中的一个国王,在他管理的国家中发行了很多不同面额的纸币,用这些纸币进行任意的组合可以在 ...
- 九度OJ 1024 畅通工程 -- 并查集、贪心算法(最小生成树)
题目地址:http://ac.jobdu.com/problem.php?pid=1024 题目描述: 省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但 ...
随机推荐
- Solidworks 好的测试题模拟题
题目:按照下图构建草图,注意设置必要的几何约束. 问题: 1.参照下图所示参数时请问其中绿色区域的面积为多少平方毫米? 题目:参照下图绘制草图轮廓,注意图中各线条之间均为相切过渡. 问题: ...
- AFN检測网络情况
问: I'm a bit lost on AFNetorking's Reachability and haven't found a lot of good information out ther ...
- S1:适配器 Adapter
将一个类的接口转换为用户期望的另外一个接口.适配器使得原本由于接口不兼容而不能一起工作的类可以一起工作 UML: 一.类适配器: class A { public function methodA ...
- PHP结巴程序实现
<?php $str="我...我要要要...学学学..编编程"; $str=preg_replace('/\./','',$str);//我我要要要学学学编编程 $str= ...
- apue学习笔记(第一章UNIX基础知识)
总所周知,UNIX环境高级编程是一本很经典的书,之前我粗略的看了一遍,感觉理解得不够深入. 听说写博客可以提高自己的水平,因此趁着这个机会我想把它重新看一遍,并把每一章的笔记写在博客里面. 我学习的时 ...
- httpModules 与 httpHandlers
ASP.NET对请求处理的过程:当请求一个*.aspx文件的时候,这个请求会被inetinfo.exe进程截获,它判断文件的后缀(aspx)之后,将这个请求转交给ASPNET_ISAPI.dll,AS ...
- 基于Django进行简单的微信开发
代码地址如下:http://www.demodashi.com/demo/11756.html 一.微信公众号的准备: 1. 注册 访问地址:https://mp.weixin.qq.com/ 按照提 ...
- 创建标题栏,UINavigationBar的使用
IOS 开发有关界面的东西不仅可以使用代码来编写,也可以使用Interface Builder可视化工具来编写.今天有个朋友问我这两个有什么区别,首先说说IB ,使用它编辑出来的控件其实底层还是调用代 ...
- struct和class的不同以及struct的应用场景
struct在C#中被用来定义结构,它是一种比类小的数据类型.和类一样都是创建对象的模板,可以有自己的数据以及处理和访问数据的方法. struct的用法: struct FurnitureSize { ...
- Linux系统下如何监测磁盘的使用空间
不管是我们在安装软件还是监测软件的使用性能,我们都要随时掌握系统磁盘的使用情况. 使用df命令 df df命令用于显示磁盘分区上的可使用的磁盘空间.默认显示单位为KB.可以利用该命令来获取硬盘被占用了 ...