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

      

  • 相关阅读:
    使用python将文字写入word文档中
    将图片显示到excel中
    新的写入xlsxwriter和追加写入openpyxl
    oracle 12.2 alter table move online
    主从复制管理和故障处理方法
    MySQL中的权限管理
    windows的CMD如何全屏最大化
    Troubleshooting query v$asm_disk v$asm_diskgroup hang
    library cache锁争用解决
    一则由ORA-1652引起的fixed object相关问题
  • 原文地址:https://www.cnblogs.com/ccccnzb/p/3830389.html
Copyright © 2011-2022 走看看