九度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 题目描述: 省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但 ...
随机推荐
- java学习之输入,输出流
输入流与输出流 1,流的分类:字节流和字符流 区别如下: 1,字节流(8位Unicode)在操作的时候本身是不会用到缓冲区(内存)的byte,是与文件本身直接操作的,而字符流(16位Unicode)在 ...
- ASP.NET MVC学习---(一)ORM框架,EF实体数据模型简介
现如今 对象关系映射(ORM)框架 被大量的使用于企业级应用的开发 为什么要使用ORM? ADO.NET操作数据库不好吗? 我们可以仔细想想 当我们使用ADO.NET操作数据库的时候 我们需要先获取连 ...
- C#设计模式---观察者模式简单例子
在开发过程中经常遇到一个模块中的 一个方法调用了其他模块中相关的方法 比如说在一个系统中,如果出现了错误,就调用专门进行错误处理的模块中的方法进行错误处理 而因为错误处理的操作有很多,所以将这些具体的 ...
- Python 自用代码(scrapy多级页面(三级页面)爬虫)
2017-03-28 入职接到的第一个小任务,scrapy多级页面爬虫,从来没写过爬虫,也没学过scrapy,甚至连xpath都没用过,最后用了将近一周才搞定.肯定有很多low爆的地方,希望大家可以给 ...
- ECSHOP站内页面跳转,避免死链
2.x版本域名重定向: # For ISAPI_Rewrite 2.x RewriteCond Host: ^steveluo\.name$ RewriteRule (.*) http\://www\ ...
- Sending SMS And Dialing Numbers without User Consent(Context is not needed)
Sending SMS And Dialing Numbers without User Consent Sending SMS does not require context or user in ...
- Linux组件封装(五)一个生产者消费者问题示例
生产者消费者问题是计算机中一类重要的模型,主要描述的是:生产者往缓冲区中放入产品.消费者取走产品.生产者和消费者指的可以是线程也可以是进程. 生产者消费者问题的难点在于: 为了缓冲区数据的安全性,一次 ...
- WebGL 启动载入触发更新流程分析
WebGL 启动载入触发更新流程分析 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致"创作公用协议 转载 ...
- Elasticsearch 2014年10月简报
1. Elasticsearch Updates 1.1 公布了Kibana 4 Beta 1 和Beta 1.1 Kibana 4不管是在界面的布局,使用配置方法,还是底层绘制图表的方式都与Kiba ...
- Sequence contains no matching element
1.linq查询Single方法出错 var c = DbCache.UserRoles.Single(ur => ur.RoleId == roleId); 2.使用方法System.Linq ...