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);//多减去被占节点 
    	}
    }
    

      



  • 相关阅读:
    网上找的Backbone.js
    关于数据结构,剑指offer上面的
    软件工程 什么叫高内聚 低耦合
    【丢失的转化率】你的宝贝,有多少人放进了购物车却没有支付?
    怎么都没人提 google 加密搜索呢? google如何稳定打开
    Mustache.js语法学习笔记
    C# POST数据到指定页面,并跳转至该页面
    将多个图片整合到一张图片中再用CSS 进行网页背景定位
    铁通、长宽网络支付时“签名失败”问题分析及解决方案  [88222001]验证签名异常:FAIL[20131101100002-142]
    jqGrid中多选
  • 原文地址:https://www.cnblogs.com/grglym/p/7644070.html
Copyright © 2011-2022 走看看