zoukankan      html  css  js  c++  java
  • hdu 小希的迷宫

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1272

    思路:用并查集来判断是否都在一个集合里面,是否成环   (ps:今天头好晕...)  

    代码:

     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <math.h>
     4 #include <limits.h> 
     5 #include <algorithm>
     6 #include <iostream>
     7 #include <ctype.h>
     8 #include <iomanip>
     9 #include <queue>
    10 #include <map>
    11 #include <stdlib.h>
    12 using namespace std;
    13 
    14 int f[100010],mark[100010];
    15 
    16 int find(int x)
    17 {
    18     if(x==f[x])
    19         return f[x];
    20     f[x]=find(f[x]);
    21     return f[x];
    22 }
    23 
    24 bool Union(int x,int y)
    25 {
    26     int a=find(x);
    27     int b=find(y);
    28     if(a!=b){
    29         f[a]=b;
    30         return true;
    31     }
    32     return false;
    33 }
    34 
    35 int main()
    36 {
    37     int a,b,i,p,count;
    38     while(scanf("%d%d",&a,&b)&&(a!=-1&&b!=-1)){
    39         count=0,p=1;
    40         if(a==0&&b==0){
    41             printf("Yes
    ");
    42             continue;
    43         }
    44         for(i=0;i<100010;i++){
    45             f[i]=i;mark[i]=0;
    46         }
    47         while(a||b){
    48             mark[a]=1,mark[b]=1;
    49             if(Union(a,b)==false)
    50                 p=0;
    51             scanf("%d%d",&a,&b);
    52         }
    53         if(p==0)
    54             printf("No
    ");
    55         else{
    56             for(i=0;i<100010;i++)
    57                 if(mark[i]&&f[i]==i)
    58                     count++;
    59                 if(count==1)
    60                     printf("Yes
    ");
    61                 else
    62                     printf("No
    ");
    63         }
    64     }
    65 }
  • 相关阅读:
    MySQL锁系列3 MDL锁
    MySQL锁系列2 表锁
    MySQL锁系列1
    MySQL open table
    MySQL优化器join顺序
    MySQL优化器cost计算
    MySQL源码 优化器
    MySQL源码 解析器
    MySQL源码 数据结构hash
    微信小程序爬坑日记
  • 原文地址:https://www.cnblogs.com/wangmengmeng/p/4951189.html
Copyright © 2011-2022 走看看