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;
    }
    

      

  • 相关阅读:
    数据结构【图】—022邻接矩阵的深度和广度遍历
    第一百三十一天 how can I 坚持
    第一百三十天 how can I 坚持
    第一百二十九天 how can I坚持
    第一百二十八天 how can i 坚持
    第一百二十七天 how can I 坚持
    第一百二十六天 how can I 坚持
    第一百二十五天 how can I坚持
    第一百二十四天 how can I坚持
    第一百二十三天 how can I 坚持
  • 原文地址:https://www.cnblogs.com/ccccnzb/p/3830389.html
Copyright © 2011-2022 走看看