1013. Battle over Cities (25)

Source: Internet
Author: User
Tags time limit
1013. Battle over Cities (+)Time limit MS
Memory Limit 65536 KB
Code length limit 16000 B
Program Standard author CHEN, Yue

It is vitally important to has all the cities connected by highways in a war. If a city was occupied by the enemy, all of the highways From/toward that city was closed. We must know immediately if we need to repair all other highways to keep the rest of the cities connected. Given the map of cities which has all the remaining highways marked, you is 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 are occupied by the enemy, we must has 1 highway repaired, which is the highway city2-city3.

Input

Each input file contains the one test case. Each case starts with a line containing 3 numbers N (<1000), M and K, which is 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 is the numbers of the cities of Highway. The cities is 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
Main ideas:
1. The number of connected groups of the graph (the number 2 is the number required to Rapair highway) after removing all the edges of a node and the node in the calculation diagram
2, the original image only one city alone.
3, the city's number (that is, the number of nodes in the figure starting from 1), in the depth-first traversal of the graph needs to be noted.
#include <iostream> using namespace std;
    #define MAXNODE 1001 typedef struct Graph//adjacency matrix storage diagram {int Value[maxnode][maxnode];

int n;//The number of edges in the m;//graph of the node int in Figure}graph,*pgraph; void Initgraph (pgraph g,int n,int M)//The number of nodes and sides of the graph is passed in and initialized with the graph {for (int i=0; i<=n; ++i) {for (int k=0; k< N
        k++) {g->value[i][k]=-1;
    }} g->m=m;
g->n=n;
    } void Getgraph (Pgraph g,int M)//Storage topic input Diagram {int v1,v2;
        for (int i=0; i<m; ++i) {cin>>v1>>v2;
        g->value[v1][v2]=1;
    g->value[v2][v1]=1;
    }} void DFS (Pgraph g,int now,int* visited)//start with the now node in depth-first traversal of the connected component containing the Today node {visited[now]=1; for (int i=1; i<=g->n; ++i) {if (g->value[now][i]==1) && (!
        Visited[i]) {DFS (g,i,visited);
}} return;
    } int Dfs_alg (pgraph g)//depth-first traversal of Figure G {int count=0;//the number of connected components int Visited[maxnode]; for (int i=1; i<=g->N
    ++i) {visited[i]=0; } for (int i=1; i<=g->n; ++i) {if (!
    Visited[i]) {DFS (g,i,visited);//Here each time DFS traverses a connected group count++;//the number of connected components in Figure G}}
return count; } int one_city_occupied (int city,graph G)//Note In value mode, create a graph G in the stack space, changes to this figure G will not change the value of figure g outside the function {for (int i=1; i<=g.n; + +)
            i) {if (g.value[city][i]==1) {g.value[city][i]=-1;
        G.value[i][city]=-1;
}} return Dfs_alg (&AMP;G);
    } int main () {int n,m,k;
    
    cin>>n>>m>>k;
    if (n==1)//a city does not have highway {cout<< "0";
        }else//normal situation, the city is greater than or equal to 1 {pgraph g=new Graph;
        Initgraph (G, n,m);
    
        Getgraph (G,M);
        int city=0;
            int link=0;//number of connected components for (int i=0; i<k; ++i) {cin>>city;
            Link=one_city_occupied (city, *g); if (link==1) {cout<< "0";
            }else {cout<<link-2;//The occupied city is a connected component, and the remaining city needs the least number of connected components minus one at this time, so the overall minus two}
            if (i<k-1) {cout<<endl;
}}} return 0; }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.