• hdu1272


    题目链接:

    题目给出哪些边之间有连线,问图中是否构成环,还有每两个点之间是否相通,就是问给出的这些点是否构成树,我们只要扫一遍是否边的数量加一是点的数量然后判在同一棵树中的两个点是否重复连边就可以了。其实这道题也不需要判断是否存在环,直接用树的特性就解决了。判断不重复的点的数量可用set解决。

    代码如下:

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 typedef unsigned int ui;
     4 typedef long long ll;
     5 typedef unsigned long long ull;
     6 #define pf printf
     7 #define mem(a,b) memset(a,b,sizeof(a))
     8 #define prime1 1e9+7
     9 #define prime2 1e9+9
    10 #define pi 3.14159265
    11 #define lson l,mid,rt<<1
    12 #define rson mid+1,r,rt<<1|1
    13 #define scand(x) scanf("%llf",&x) 
    14 #define f(i,a,b) for(int i=a;i<=b;i++)
    15 #define scan(a) scanf("%d",&a)
    16 #define mp(a,b) make_pair((a),(b))
    17 #define P pair<int,int>
    18 #define dbg(args) cout<<#args<<":"<<args<<endl;
    19 #define inf 0x3f3f3f3f
    20 const int maxn=1e6+10;
    21 int n,m,t;
    22 inline int read(){
    23     int ans=0,w=1;
    24     char ch=getchar();
    25     while(!isdigit(ch)){if(ch=='-')w=-1;ch=getchar();}
    26     while(isdigit(ch))ans=(ans<<3)+(ans<<1)+ch-'0',ch=getchar();
    27     return ans*w;
    28 }
    29 int f[maxn];
    30 bool cir=false;
    31 set<int> s;
    32 int num=0;//统计边的数量 
    33 int init()
    34 {
    35     f(i,1,maxn-1)f[i]=i;
    36     cir=false;
    37     s.clear();
    38     num=0;
    39 }
    40 int find(int x)
    41 {
    42     if(x==f[x])return x;
    43     return f[x]=find(f[x]);
    44 }
    45 void Union(int x,int y)
    46 {
    47     int fx=find(x);
    48     int fy=find(y);
    49     if(fx==fy)return;
    50     else f[fx]=fy;
    51 }
    52 int main()
    53 {
    54     //freopen("input.txt","r",stdin);
    55     //freopen("output.txt","w",stdout);
    56     std::ios::sync_with_stdio(false);
    57     int a,b;
    58     while(scanf("%d%d",&a,&b)&&!(a==-1&&b==-1))
    59     {
    60         if(a==0&&b==0)
    61         {
    62             pf("Yes
    ");
    63             continue;
    64         }
    65         init();
    66         Union(a,b);
    67         s.insert(a);
    68         s.insert(b); 
    69         num++;
    70         while(scanf("%d%d",&a,&b))
    71         {
    72             if(a==0&&b==0)break;
    73             s.insert(a);
    74             s.insert(b);
    75             Union(a,b);
    76             num++;
    77         }
    78         if(s.size()==num+1)pf("Yes
    ");//点的数量等于边的数量加一,是树的基本特征 
    79         else pf("No
    ");
    80     }
    81  } 

     

  • 相关阅读:
    SSH 远程执行任务
    C# 创建压缩文件
    迁移 SQL Server 到 Azure SQL 实战
    在 Azure 上部署 Asp.NET Core Web App
    linux kill 命令
    VS 远程调试 Azure Web App
    Azure 基础:自定义 Table storage 查询条件
    NSOperation的使用细节 [2]
    NSOperation的使用细节 [1]
    [翻译] SSKeychain
  • 原文地址:https://www.cnblogs.com/randy-lo/p/12560894.html
Copyright © 2020-2023  润新知