1013. battle over cities (25)-pat

Source: Internet
Author: User
1013. battle over cities (25) Time Limit 400 ms memory limit 32000 kb code length limit 16000 B discriminant program standard author 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 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 responred, that is the highway City2-City3.

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. 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

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

Sample Input

3 2 31 21 31 2 3

Sample output

100
Recommendation index :※※
Note: The question shows that the original graph is connected ..
# Include <iostream> # include <string. h> using namespace STD; # include <queue> # define n 1001int main () {int n, m, K, I, j, T, C1, C2; bool highways [N] [N]; int concern [N]; memset (highways, 0, sizeof (highways); memset (concern, 0, sizeof (concern )); cin> N; CIN> m; CIN> K; for (I = 0; I <m; I ++) {CIN> C1; CIN> C2; highways [C1] [C2] = true; highways [C2] [C1] = true;} for (I = 0; I <K; I ++) {CIN> concern [I] ;}for (I = 0; I <K; I ++) {int visit [N], Count, TMP; queue <Int> q; memset (visit, 0, sizeof (visit); visit [concern [I] = 1; Count = 0; For (j = 1; j <= N; j ++) {If (visit [J]! = 1) {queue <int> DQ; visit [J] = 1; DQ. push (j); // perform bfswhile (! DQ. empty () {TMP = DQ. front (); DQ. pop (); For (t = 0; t <= N; t ++) {If (highways [TMP] [T]! = False & visit [T]! = 1) {DQ. Push (t); visit [T] = 1 ;}}/ * if an existing node can traverse all vertices, Edge Addition is not required and exit. Otherwise, the independent "island" + 1 searches for the next island */count ++ ;}} cout <count-1 <Endl; /* The path to be added is the minimum path required to connect to each island */} return 0 ;}
9 degree question 1526: the circle of friends is similar to this

 
 
Description:

If we know that there are n people and m friends (stored in the number R ). If two people are friends directly or indirectly (friends of friends ...), they think they belong to the same circle of friends. Please write a program to find out the total number of circle of friends among the N people.
Assume that n = 5, M = 3, r = {1, 2}, {2, 3}, {4, 5} indicates that there are 5 people, 1 and 2 are friends, 2 and 3 are friends, 4 and 5 are friends, then 1, 2, 3 belong to one circle of friends, 4, 5 belong to another circle of friends, the result is two moments.

Input:

The input contains multiple test cases. The first line of each test case contains two positive integers n, m, 1 = <n, m <= 100000. Next, there are m rows. Enter the numbers F and t (1 = <F, T <= N) of two people in each row, indicating that F and t are friends. When N is 0, the input ends and the case is not processed.

Output:

For each test case, the total number of friends in the N persons is displayed.

Sample input:
5 31 22 34 53 31 21 32 30
Sample output:
21
#include<iostream>#include<string.h>#include<vector>using namespace std;#include<queue>#define  N 100000int main(){int n,m,i,j,t,c1,c2,k;cin>>n;while(n>0){vector<int> highways[N];cin>>m;for(i=0;i<m;i++){cin>>c1;cin>>c2;highways[c1].push_back(c2);highways[c2].push_back(c1);}int visit[N],count,tmp;queue<int> q;memset(visit,0,sizeof(visit));count=0;for(j=1;j<=n;j++){if(visit[j]!=1){queue<int> dq;visit[j]=1;dq.push(j);while(!dq.empty()){tmp=dq.front();dq.pop();//for(t=0;t<=n;t++){for(int i=0;i

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.