菜单 学习猿地 - LMONKEY

VIP

开通学习猿地VIP

尊享10项VIP特权 持续新增

知识通关挑战

打卡带练!告别无效练习

接私单赚外块

VIP优先接,累计金额超百万

学习猿地私房课免费学

大厂实战课仅对VIP开放

你的一对一导师

每月可免费咨询大牛30次

领取更多软件工程师实用特权

入驻
320
0

PAT1013

原创
05/13 14:22
阅读数 91677

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.

例如,如果我们有三个城市,两个高速公路,连接C1和C2还有C1和C3

Then if city1 is occupied by the enemy, we must have 1 highway repaired, that is the highway city2-city3.

然后如果city1被敌人占领了,我们就必须要修一条高速公路,就是C2到C3的

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.

每个输入文件包含一个测试用例,每个测试用例由3个数N,M,K开始,分别表示,城市数,剩下的高速公路的数量,占领的城市数

Then M lines follow, each describes a highway by 2 integers, which are the numbers of the cities the highway connects.

接下来M行,每个都描述了两个数,表示两个城市直接有高速公路

The cities are numbered from 1 to N. Finally there is a line containing K numbers, which represent the cities we concern.

城市用1-N标号,最终一行有K个数,表示我们考虑的城市

Output

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

对于K个城市,输出一行数,如果这个城市丢失,需要被修建的高速公路

Sample Input

3 2 3
1 2
1 3
1 2 3

Sample Output

1
0
0
 
 
一开始看见题目以为很简单,简单的以为只要记录所有点的连接数就可以了。
但是仔细想想就不对了,题目要求的是原来有很多条路,但是,原来不一定是单源的,但是选择的那个点拿掉之后,要求它成为一个单源的图。
 
一开始还在想如何保存这个图,因为这个图的路可能会很多,如果单纯用搜索的话很容易会TLE
后面换个思路想想,如果去掉这个点,求出5个源,那么肯定建造4条路就行了,题目又不要求说明造的是那一条
那么就是求源的个数就可以咯
 
求源个数的话,那肯定并查集嘛。
不过并查集我还不是很熟练,还是看了以前的模板才写出来。
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<string.h>

using namespace std;

int x[540000],y[540000];
int maps[1005];

int getRoot(int root)
{
    if(maps[root] == root)
        return root;

    maps[root] = getRoot(maps[root]);
    return maps[root];
}

void update(int a,int b)
{
    int rootA;
    int rootB;

    rootA = getRoot(a);
    rootB = getRoot(b);

    if(rootA != rootB)
        maps[rootB] = rootA;
}

int main()
{
    int n,m,k;
    int c=0;
    int result=0;
    int u;

    for (int i = 0; i <= 1005; i++)
        maps[i] = i;

    cin>>n>>m>>k;
    while(m--)
    {
        cin>>x[c]>>y[c];
        c++;
    }

    while(k--)
    {
        result=0;
        cin>>u;
        
        for (int i = 0; i < c; i++)
        {
            if(x[i]!=u && y[i]!=u)
                update(x[i],y[i]);
        }
        for (int i = 1; i <= n; i++)
        {
            if(i != u && maps[i] == i)
                result++;
            maps[i] = i;
        }

        cout<<result - 1<<endl;
    }

    return 0;  
}

发表评论

0/200
320 点赞
0 评论
收藏