zoukankan      html  css  js  c++  java
  • 1013. Battle Over Cities (25)

    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

    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 repaired if that city is lost.

    Sample Input

    3 2 3
    1 2
    1 3
    1 2 3
    

    Sample Output

    1
    0
    0

    题目大意:求图中去掉某个顶点及其与之连接的边,求有几个独立的区域。用搜索即可解决。
    #include<iostream>
    #include<stdio.h>
    #include<cstring>
    #include<algorithm>
    #include<queue>
    #include<stack>
    #include<stdlib.h>
    using namespace std;
    int N,M,K;
    #define max 1002
    int map[max][max];
    int visit[max];
    int occupy;
    void DFS(int t){
    	visit[t] = 1;
    	int i;
    	for(i=1;i<=N;i++){
    		if(map[i][t]== 1 && visit[i] == 0){
    			DFS(i);
    		}
    	}
    } 
    int main(){
    	scanf("%d%d%d",&N,&M,&K);
    	int i,j,t;
    	int from,to;
    	int num =0;
    	for(i=1;i<=M;i++){
    		scanf("%d%d",&from,&to);
    		map[from][to]=1;
    		map[to][from]=1;
    	}
    	for(i=0;i<K;i++){
    		num = 0;
    		scanf("%d",&occupy);
    		memset(visit,0,sizeof(visit));
    		visit[occupy]=1;
    		for(t=1;t<=N;t++){
    			if(visit[t] == 0){
    				DFS(t);
    				num++;
    			}
    		}
    		printf("%d
    ",num-1);
    		
    	}
    }
    

      

    另解:并查集求独立区域的集合。

    #include<iostream>
    #include<stdio.h>
    #include<cstring>
    using namespace std;
    #define max 1002
    int map[max];
    int N,M,K;
    int find(int key){
    	if(map[key] == -1)return key;
    	else {
    		int index;
    		index = find(map[key]);
    		map[key]=index;
    		return index;
    	}
    }
    struct Tree{
    	int x;
    	int y;
    };
    int main(){
    	int i,j,t,l;
    	Tree tree[1000000];
    	scanf("%d%d%d",&N,&M,&K);
    	for(i=0;i<M;i++){
    		scanf("%d%d",&tree[i].x,&tree[i].y);
    	}
    	for(i=0;i<K;i++){
    		memset(map,-1,sizeof(map));
    		scanf("%d",&t);
    		for(j=0;j<M;j++){
    			if(tree[j].x != t && tree[j].y != t){
    				int x = find(tree[j].x);
    				int y = find(tree[j].y);
    				if(x != y){
    					map[x]=y;
    				}				
    			}
    		}
    		int sum = 0;
    		for(l=0;l<N;l++){
    			if(map[l]== -1){
    				sum ++;
    			}
    		}
    		printf("%d
    ",sum-2);//多减去被占节点 
    	}
    }
    

      



  • 相关阅读:
    “学霸系统”app——NABC
    Scrum Meeting NO.1
    团队成员角色
    团队作业 #2
    团队作业 #1
    Qt, 我回来了。。。
    boost: tcp client sample
    makefile 中定义宏位置需要注意一下
    libpcap报文解析: ipv4、ipv6 @ 2014.7.2
    编程网站收集
  • 原文地址:https://www.cnblogs.com/grglym/p/7644070.html
Copyright © 2011-2022 走看看