• 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; }
  • 相关阅读:
    docker network
    mongodb索引
    docker中管理数据
    mysql表备份及还原
    Find and run the whalesay image
    Learn about images & containers
    docker installation on ubuntu
    【转载】熟练利用google hacking来辅助我们快速渗透
    xmind常用快捷键
    漏洞挖掘基础
  • 原文地址:https://www.cnblogs.com/wanghao-boke/p/8573332.html
Copyright © 2020-2023  润新知