1013. Battle Over Cities (25)

Source: Internet
Author: User

Description: 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 retried red, 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 retries red, that is the highway city2-city3.InputEach 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 highwa Ys, 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. outputFor each of the K cities, output in a line the number of highways need to be retried red if that ci Ty is lost. Sample Input3 2 31 21 31 2 3 Sample Output100 analysis: (1) the question shows a graph and several edges. It is required that you remove one of the vertices and then increase the number of edges required to connect the remaining vertices. (2) In fact, as long as you consider removing this vertex, the remaining vertex can form several independent areas, assuming that the number of areas is t, the edge that needs to be added is the T-1. (3) To achieve what (2) says, we adopt DFS (depth-first) Best. Reference code:

#include<stdio.h>#include<string.h>#define max 1001int edge[max][max];int visited[max];int query[max];int N; // the total number of citiesint M; // the number of remaining highwaysint K; // the number of cities to be checkedvoid dfs(int t){visited[t] = 1;int i;for(i=1; i<=N; i++){if(!visited[i] && edge[i][t] == 1)dfs(i);}}int main(){int i,j;int a,b;scanf("%d%d%d",&N,&M,&K);for(i=0; i<M; i++){scanf("%d%d",&a,&b);edge[a][b] = 1;edge[b][a] = 1;}int temp;int num;for(i=0; i<K; i++){num = 0;scanf("%d",&temp);memset(visited,0,sizeof(visited));visited[temp] = 1;for(j=1; j<=N; j++){if(visited[j] == 0){dfs(j);num ++;}}printf("%d\n",num-1);}}

 


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.