zoukankan      html  css  js  c++  java
  • ZOJ Problem Set

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3321

    Circle

    Time Limit: 1 Second      Memory Limit: 32768 KB

    Your task is so easy. I will give you an undirected graph, and you just need to tell me whether the graph is just a circle. A cycle is three or more nodes V1V2V3, ... Vk, such that there are edges between V1 and V2V2 and V3, ... Vk and V1, with no other extra edges. The graph will not contain self-loop. Furthermore, there is at most one edge between two nodes.

    Input

    There are multiple cases (no more than 10).

    The first line contains two integers n and m, which indicate the number of nodes and the number of edges (1 < n < 10, 1 <= m < 20).

    Following are m lines, each contains two integers x and y (1 <= xy <= nx != y), which means there is an edge between node x and node y.

    There is a blank line between cases.

    Output

    If the graph is just a circle, output "YES", otherwise output "NO".

    Sample Input

    3 3
    1 2
    2 3
    1 3
    
    4 4
    1 2
    2 3
    3 1
    1 4
    

    Sample Output

    YES
    NO




    改了队友的经典并查集代码过了此题,记录下作为经典模板使用
    #include<iostream>
    using namespace std;
    int pre[1010];
    
    int find(int x)
    {
        return pre[x]==x?x:pre[x]=find(pre[x]);
    }
    
    int main()
    {
        int road,root1,root2,num,i,start,end,total;
        while(cin>>num>>road )
        {
            int sum=0;
            total=num-1;
            for(i=1; i<=num; i++)
                pre[i]=i;
            while(road--)
            {
                cin>>start>>end;
    
                sum+=(start+end);
                root1=find(start);
                root2=find(end);
                if(root1!=root2)
                {
                    pre[root1]=root2;
                    total--;
                }
            }
            int tmp=0;
            for(i=1;i<=num;i++)
            {
                tmp+=i;
            }
    
            if(!total && tmp*2 == sum)
            {
                cout<<"YES"<<endl;
            }
            else
            {
                cout<<"NO"<<endl;
            }
        }
        return 0;
    }
    

      

  • 相关阅读:
    C# 协变 逆变
    go slice 理解
    为什么避免使用Task.Wait或者Task.Result
    IL笔记
    docker随笔
    领域事件
    总结笔记
    基于ASP.NET Core 3.x的端点路由(Endpoint Routing)实现控制器(Controller)和操作(Action)分离的接口服务
    CentOS7 多IP搭建SOCKS5代理服务器
    Springboot 实现数据库备份还原
  • 原文地址:https://www.cnblogs.com/ccccnzb/p/3830389.html
Copyright © 2011-2022 走看看