时间限制: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中hashcode的理解

    Java中hashcode的理解 原文链接http://blog.csdn.net/chinayuan/article/details/3345559 怎样理解hashCode的作用: 以 java. ...

  2. C#基于Socket的CS模式的完整例子

    基于Socket服务器端实现本例主要是建立多客户端与服务器之间的数据传输,首先设计服务器.打开VS2008,在D:\C#\ch17目录下建立名为SocketServer的Windows应用程序.打开工 ...

  3. PHP计算字符串长度函数

    //计算字符串长度 function strlen_utf8($str) { $i = 0; $count = 0; $len = strlen ($str); while ($i < $len ...

  4. Web用户的身份验证及WebApi权限验证流程的设计和实现(尾)

    5. WebApi 服务端代码示例 5.1 控制器基类ApiControllerBase [csharp] view plaincopy   /// /// Controller的基类,用于实现适合业 ...

  5. Delphi 与 C/C++ 数据类型对照表(最新的tokyo)

    更新,下面这table为最新的tokyo基本数据类型与C++的对照关系: Delphi to C++ types mapping   Go Up to Support for Delphi Data ...

  6. 在Java中怎样高效的推断数组中是否包括某个元素

    来自 http://www.hollischuang.com/archives/1269? 怎样检查一个数组(无序)是否包括一个特定的值?这是一个在Java中经经常使用到的并且非常实用的操作.同一时候 ...

  7. sqlserver修改表主键自增

    alter table tname add id int  identity(1,1)

  8. web service json 数组解析

     boolean workexpMark = true;     // 美发师工作经历json数组解析     org.json.JSONObject jsonObject = new org.j ...

  9. 解读Unity中的CG编写Shader系列3——表面剔除与剪裁模式

    在上一个样例中,我们得到了由mesh组件传递的信息经过数学转换至合适的颜色区间以颜色的形式着色到物体上. 这篇文章将要在此基础上研究片段的擦除(discarding fragments)和前面剪裁.后 ...

  10. Spring Boot 如何在类中应用配置文件

    如何在类中应用配置文件 优先级: 当前目录子目录的/config > 当前目录 > classpath的/config包 > classpath的根目录 即:越靠近的优先级越高 ** ...