PAT A1013 Battle Over Cities

文章讨论了在战争背景下确保城市间高速公路连接的重要性。给定剩余的公路网络和可能被占领的城市,我们需要快速计算出如果每个城市被占领,需要修复多少条公路以保持其余城市的连通。文章提供了输入输出规格,并展示了两种不同的深度优先搜索(DFS)算法来解决这个问题,一种较简洁,另一种较复杂。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1013 Battle Over Cities

分数 25

作者 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 Specification:

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 Specification:

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

  * 在原图中删除某个点并不好真正的操作,我们可以在dfs函数中传入两个参数,
 * 一个当前的访问节点编号,另外一个被删除的结点编号,当当前节点编号等于
 * 删除节点编号时则结束递归;
 * 不难而知:
 * 当删除结点编号不是孤立的点时,被删除结点以后,当前图的连通块数量cnt
 * 比原图必定多一,此时需要连通的边数是cnt - 2就可以将整个图连接成一个
 * 连通图;
 *
 * 当删除结点编号是孤立的点时,被删除结点以后,当前图的连通块数量cnt
 * 与原图一样多,此时需要连通的边数是cnt - 2就可以将整个图连接成一个
 * 连通图;
 *
 * 综上所述,求出被删除节点以后连通块的数量cnt,cnt-2就是最终答案。

/**
 * 在原图中删除某个点并不好真正的操作,我们可以在dfs函数中传入两个参数,
 * 一个当前的访问节点编号,另外一个被删除的结点编号,当当前节点编号等于
 * 删除节点编号时则结束递归;
 * 不难而知:
 * 当删除结点编号不是孤立的点时,被删除结点以后,当前图的连通块数量cnt
 * 比原图必定多一,此时需要连通的边数是cnt - 2就可以将整个图连接成一个
 * 连通图;
 * 
 * 当删除结点编号是孤立的点时,被删除结点以后,当前图的连通块数量cnt
 * 与原图一样多,此时需要连通的边数是cnt - 2就可以将整个图连接成一个
 * 连通图;
 * 
 * 综上所述,求出被删除节点以后连通块的数量cnt,cnt-2就是最终答案。
*/

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

const int N = 1010;
vector<int> Adj[N];
bool hs[N];
int chk[N];
int Nv, Ne, k;

void add(int u, int v)
{
    Adj[u].push_back(v);
}

void Read()
{
    cin >> Nv >> Ne >> k;
    for(int i=0; i<Ne; ++i)
    {
        int u, v;
        cin >> u >> v;
        add(u, v), add(v, u);
    }
    
    for(int i=0; i<k; ++i)
        cin >> chk[i];
}

void FloodFill(int u, int cut)
{
     //当前访问节点是被删除结点或者已经被放访问过
    if(u == cut || hs[u])  
        return;
    
    hs[u] = 1; //记录已被访问过
    
    for(auto ele : Adj[u])
        if(hs[ele] == 0) //ele未被访问过
            FloodFill(ele, cut);
}

int main()
{
    Read();
    
    for(int i=0; i<k; ++i)
    {
        int cnt = 0;
        fill(hs, hs+N, 0);
        for(int j=1; j<=Nv; ++j)
            if(hs[j] == 0)
            {
                ++cnt;
                FloodFill(j, chk[i]);
            }
                
        cout << cnt-2 << endl;
    }
    
    return 0;
}

 

2)这个是很久之前写的,倒是能看懂,不想写注释了,这个代码现在看来太复杂了。还是前面的DFS更甚一筹。

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

const int N = 1010;
vector<int> Adj[N];
int low[N], num[N];
int stk[N];
bool hs[N];
int flag[N];
int id[N], Size[N];
int tim, top, cnt;
int n, m, k;
int root;

void add(int u, int v)
{
    Adj[u].push_back(v);
}

void floodfill(int u)
{
    hs[u] = 1;
    stk[top++] = u;
    
    for(auto v : Adj[u])
        if(hs[v] == 0)
            floodfill(v);
}

void targan(int u, int fath)
{
    num[u] = low[u] = ++tim;
    int child = 0;
    for(auto v : Adj[u])
    {
        if(num[v] == 0)
        {
            ++child;
            targan(v, u);
            low[u] = min(low[u], low[v]);
            if(u != root && low[v] >= num[u])
                flag[u] = 1;
            if(u == root && child > 1)
                flag[u] = 1;
        }
        else if(v != fath) 
            low[u] = min(low[u], low[v]);
    }
}

int main()
{
    cin >> n >> m >> k;
    while(m--)
    {
        int u, v;
        cin >> u >> v;
        add(u, v), add(v, u);
    }
    
    for(int i=1; i<=n; ++i)
        if(hs[i] == 0)
        {
            floodfill(i);
            ++cnt;
            int y;
            do{
                y = stk[--top];
                id[y] = cnt;
                Size[cnt]++;
            }while(y != i);
        }
        
    for(int i=1; i<=n; ++i)
    {
        root = i;
        if(num[i] == 0)
            targan(i, -1);
    }
    
    while(k--)
    {
        int chk;
        cin >> chk;
        if(flag[chk])   cout << cnt << endl;
        else if(Size[id[chk]] == 1) cout << cnt - 2 << endl;
        else cout << cnt - 1 << endl;
    }
    
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值