1013 Battle Over Cities (25 分)(C语言实现)

题目来源自PAT:https://pintia.cn/problem-sets/994805342720868352/problems/994805500414115840

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 city​1-city​2 and city1-city​3 . Then if city​1​​ is occupied by the enemy, we must have 1 highway repaired, that is the highway city​2 -city​3​​ .

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

程序&算法

这道题主要考察的是图的遍历,比较简单。
注意一点,每次check city删除该城市对应边时,不能真的直接删除,需要打上标签,check完该city后,还原图。

#include <stdio.h>
#include <stdlib.h>
#define MaxVertexNum 1001
#define DEL -1

typedef int Vertex;
typedef struct ENode *Edge;
struct ENode{
    Vertex V1,V2;
};

typedef struct GNode *MGraph;
struct GNode{
    Vertex G[MaxVertexNum][MaxVertexNum];
    int Ne;
    int Nv;
};
MGraph CreatGraph(int N);
void InsertGraph(MGraph Graph,Edge E);
void DFS(MGraph Graph,Vertex S);
void DeleteVertex(MGraph Graph,Vertex D);
void ResetVertex(MGraph Graph,Vertex D);
int visited[MaxVertexNum];

int main()
{
    int N,M,K,cnt;
    Vertex D;
    scanf("%d %d %d",&N,&M,&K);
    MGraph Graph = CreatGraph(N);
    Graph->Ne = M;
    Edge E = (Edge)malloc(sizeof(struct ENode));
    for(int i=0;i<M;i++)
    {
        scanf("%d %d",&E->V1,&E->V2);
        InsertGraph(Graph,E);
    }

// {//test1:
// for(Vertex V=1;V<=Graph->Nv;V++)
// {
// for(Vertex W=1;W<=Graph->Nv;W++)
// printf("%d ",Graph->G[V][W]);
// printf("\n");
// }
// }

    for(int i=0;i<K;i++)
    {
        scanf("%d",&D);
        DeleteVertex(Graph,D);
// {//test2:
// for(Vertex V=1;V<=Graph->Nv;V++)
// {
// for(Vertex W=1;W<=Graph->Nv;W++)
// printf("%d ",Graph->G[V][W]);
// printf("\n");
// }
// }
        for(Vertex V=1;V<=Graph->Nv;V++) visited[V]=0;
        cnt = 0;
        for(Vertex V=1;V<=Graph->Nv;V++)
        {
            if(V!=D&&!visited[V])
            {
                DFS(Graph,V);
                cnt++;
            }
        }
        printf("%d\n",cnt-1);
        ResetVertex(Graph,D);
    }
    return 0;
}

MGraph CreatGraph(int N)
{
    MGraph Graph = (MGraph)malloc(sizeof(struct GNode));
    Graph->Nv = N;
    Graph->Ne = 0;
    for(Vertex V=1;V<=Graph->Nv;V++)
    {
        for(Vertex W=1;W<=Graph->Nv;W++)
            Graph->G[V][W] = 0;
    }
    return Graph;
}

void InsertGraph(MGraph Graph,Edge E)
{
    Graph->G[E->V1][E->V2] = 1;
    Graph->G[E->V2][E->V1] = 1;
}

void DFS(MGraph Graph,Vertex S)
{
    visited[S] = 1;
// printf("visited[%d] = 1\n",S);
    for(Vertex V=1;V<=Graph->Nv;V++)
    {
        if(!visited[V]&&Graph->G[S][V]==1)
        {
            DFS(Graph,V);
        }
    }
}

void DeleteVertex(MGraph Graph,Vertex D)
{
    for(Vertex V=1;V<=Graph->Nv;V++)
    {
        if(Graph->G[D][V])
        {
            Graph->G[D][V] = DEL;
            Graph->G[V][D] = DEL;
        }
    }
}

void ResetVertex(MGraph Graph,Vertex D)
{
    for(Vertex V=1;V<=Graph->Nv;V++)
    {
        if(Graph->G[D][V]==DEL)
        {
            Graph->G[D][V] = 1;
            Graph->G[V][D] = 1;
        }
    }
}
全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务