zoukankan      html  css  js  c++  java
  • ZJU PAT 1013

    http://pat.zju.edu.cn/contests/pat-practise/1013

    本来以为会卡时间,偷懒没用并查集,没想到DFS就过了。不过第一次访问标志重置时居然重置的1。。。

    PS:想起了扫雷游戏中的蒙板数组。。。用一下,其实不用也行的

     1 #include <stdio.h>
    2 #include <string.h>
    3 #include <stdlib.h>
    4 int map[1002][1002]={0};
    5 int tem_map[1002][1002]={0};
    6 bool visit[1002]={false};
    7 bool need_to_check[1002]={true};
    8 int N,M,K,count=0;
    9 void dfs(int n)
    10 {
    11 int i;
    12 for(i=1;i<=N;i++){
    13 if(i!=n&&need_to_check[i]&&tem_map[i][n]&&!visit[i]){
    14 visit[i]=true;
    15 dfs(i);
    16 }
    17 }
    18 }
    19 int main()
    20 {
    21
    22 scanf("%d%d%d",&N,&M,&K);
    23 int i,j;
    24 for(i=1;i<=M;i++){
    25 int s,e;
    26 scanf("%d%d",&s,&e);
    27 map[s][e]=map[e][s]=1;
    28 tem_map[s][e]=tem_map[e][s]=1;
    29 }//input is over
    30 //how many roads to connected?
    31 int temp=count;
    32
    33 int city;
    34 for(i=1;i<=K;i++){
    35 memset(need_to_check,true,sizeof(need_to_check));
    36 count=0;
    37 scanf("%d",&city);
    38 need_to_check[city]=false;
    39 for(j=1;j<=N;j++){
    40 tem_map[city][j]=tem_map[j][city]=0;
    41 }
    42 int k;
    43 for(k=1;k<=N;k++){
    44 if(need_to_check[k]&&!visit[k]){
    45 count++;
    46 dfs(k);
    47 }
    48 }
    49 if(count!=1)
    50 printf("%d\n",count-1);
    51 else printf("0\n");
    52 need_to_check[city]=true;
    53 for(j=1;j<=N;j++){
    54 tem_map[city][j]=map[city][j];
    55 tem_map[j][city]=map[j][city];
    56 }
    57 memset(visit,0,sizeof(visit));
    58 }
    59 }


     

  • 相关阅读:
    VIJOS-P1340 拯救ice-cream(广搜+优先级队列)
    uva 11754 Code Feat
    uva11426 GCD Extreme(II)
    uvalive 4119 Always an Interger
    POJ 1442 Black Box 优先队列
    2014上海网络赛 HDU 5053 the Sum of Cube
    uvalive 4795 Paperweight
    uvalive 4589 Asteroids
    uvalive 4973 Ardenia
    DP——数字游戏
  • 原文地址:https://www.cnblogs.com/yangce/p/2203395.html
Copyright © 2011-2022 走看看