pat 1013

1013. Battle Over Cities (25)

时间限制
400 ms
内存限制
32000 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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.

Input

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.

Output

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

Sample Input
3 2 3
1 2
1 3
1 2 3
Sample Output
1
0
0

//一次通过了啊
#include <stdio.h>
#include <string.h>

int map[1010][1010], vis[1010];
int record[1010];

int n, m, k;

void dfs(int t, int a)
{
	vis[t] = 1;

	for(int i = 1; i <= n; i++)
	{
		if(i != a && map[t][i] == 1 && vis[i] == 0)
		{
			dfs(i, a);
		}
	}
}

int main()
{
	int i, j, a, b, count;

	while(scanf("%d%d%d", &n, &m, &k) != EOF)
	{
		memset(map, 0, sizeof(map));

		for(i = 1; i <= m; i++)
		{
			scanf("%d%d", &a, &b);
			map[a][b] = map[b][a] = 1;
		}

		for(i = 1; i <= k; i++)
		{
			scanf("%d", &a);
			
			memset(record, 0, sizeof(record));

			for(j = 1, b = 0; j <= n; j++)
			{
				if(map[a][j] == 1)
				{
					map[a][j] = map[j][a] = 0;
					record[b++] = j;
				}
			}

			memset(vis, 0, sizeof(vis));

			for(j = 1, count = 0; j <= n; j++)
			{
				if(vis[j] == 0 && j != a)
				{
					dfs(j, a);
					count++;
				}
			}	

			printf("%d\n", count-1);

			for(j = 0; j < b; j++) //恢复到原来的状态
				map[a][record[j]] = map[record[j]][a] = 1;
		}
	}
	return 0;
}


### 关于PAT乙级Python 1013题目解析 对于PAT乙级中的第1013题,虽然直接针对此题目的具体描述未在提供的参考资料中找到,但是可以基于PAT系列试题的一般模式以及相似难度级别的其他题目来推测并提供一种合理的解法。 #### 题目概述 通常情况下,PAT乙级的每一道编程题都会给出明确的任务背景、输入输出格式说明。假设1013题涉及的是某种特定算法的应用或是数据结构的操作,则解答这类问题的关键在于理解题目要求的基础上选取合适的解决方案[^1]。 #### 数据预处理阶段 如果涉及到字符串操作或者是简单的数值计算类问题,在读取输入之后应该先做必要的转换工作,比如去除不必要的空白符、将字符型数字转成整数形式以便后续运算等。这一部分的工作可以通过内置函数轻松完成,例如`strip()`用于清理两端多余空格;而像`int()`, `float()`这样的构造器则能帮助把文本表示的数据变成可以直接参与算术表达式的量[^2]。 #### 主体逻辑构建 考虑到可能存在的多种情况,编程序时应当充分考虑边界条件测试用例的设计。如果是关于序列变换的问题,那么循环控制语句配合条件判断就显得尤为重要了。下面是一个简化版的例子展示如何根据给定规则改变列表内元素的状态: ```python def transform_sequence(seq): result = [] for item in seq: if isinstance(item, int): # 假设这里是对整数类型的特殊处理 new_item = (item * 3 + 1) if item % 2 != 0 else item // 2 elif isinstance(item, str): # 或者说是对字符串的一些基本修改 new_item = ''.join(reversed(item)) else: continue # 对不符合预期类型的跳过 result.append(new_item) return result ``` 这段代码只是一个示意性的框架,并不一定适用于具体的1013题场景下,但它展示了根据不同类型的数据采取不同策略的思想[^3]。 #### 结果呈现方式 最后一步就是按照指定的方式打印或返回最终的结果。注意遵循官方文档里提到的所有细节规定,包括但不限于保留几位小数点后的精度、是否需要额外添加换行符等问题。确保提交版本完全匹配评判系统的期望格式是非常重要的。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值