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<cstdio> #include<vector> using namespace std; const int maxn = 1010; vector<int> G[maxn]; bool vis[maxn]; int n,m,k; int current; void DFS(int v){ if(v == current) return; vis[v] = true; for(int i = 0; i < G[v].size(); i++){ if(vis[G[v][i]] == false ){ DFS(G[v][i]); } } } int main(){ scanf("%d%d%d",&n,&m,&k);// 城市数量,铁路数量,要检查城市数量 int u,v; for(int i = 0; i < m; i++){ scanf("%d%d",&u,&v); G[u].push_back(v); G[v].push_back(u); } for(int i = 0; i < k; i++){ scanf("%d",&current); memset(vis,false,sizeof(vis)); int block = 0; for(int j = 1; j <= n; j++){ //如果j从0到n-1 if(j != current && vis[j] == false){ DFS(j); block++; } } printf("%d ",block - 1); } return 0; }
    //并查集
    #include<cstdio> #include<vector> using namespace std; const int maxn = 1010; vector<int> G[maxn]; bool vis[maxn]; int father[maxn]; int n,m,k; int current; int findFather(int x){ int a = x; while(x != father[x]){ x = father[x]; } while(a != father[a]){ int z = a; a = father[a]; father[z] = x; } return x; } void Union(int a,int b){ int faA = findFather(a); int faB = findFather(b); if(faA != faB) father[faA] = faB; } void init(){ for(int i = 0; i < n; i++){ father[i] = i; vis[i] = false; } } int main(){ scanf("%d%d%d",&n,&m,&k);// 城市数量,铁路数量,要检查城市数量 int u,v; int i,j; for(i = 0; i < m; i++){ scanf("%d%d",&u,&v); G[u].push_back(v); G[v].push_back(u); } int qurey; for(int query = 0; query < k; query++){ scanf("%d",&current); init(); for(i = 1; i <= n; i++){ for(j = 0; j < G[i].size(); j++){ int u = i; v = G[i][j]; if(u == current || v == current) continue; Union(u,v); } } int block = 0; for(i = 1; i <= n; i++){ if(i == current) continue; int fa_i = findFather(i); if(vis[fa_i] == false){ block++; vis[fa_i] = true; } } printf("%d ",block - 1); } return 0; }
  • 相关阅读:
    红黑树
    二叉搜索树
    散列表
    基本数据结构
    顺序统计量
    RabbitMQ一些实用方法
    (转)elasticsearch连接不到head插件解决方案
    (转)如何优雅的使用rabbit mq
    (转)elasticsearch6.0版本安装head插件
    Rabbit MQ一些参数解释
  • 原文地址:https://www.cnblogs.com/wanghao-boke/p/8573332.html
Copyright © 2011-2022 走看看