• 关于欧拉回路&&连通图


    题目描述:
        欧拉回路是指不令笔离开纸面,可画过图中每条边仅一次,且可以回到起点的一条回路。现给定一个图,问是否存在欧拉回路?
    输入:
        测试输入包含若干测试用例。每个测试用例的第1行给出两个正整数,分别是节点数N ( 1 < N < 1000 )和边数M;随后的M行对应M条边,每行给出一对正整数,分别是该条边直接连通的两个节点的编号(节点从1到N编号)。当N为0时输入结束。
    输出:
        每个测试用例的输出占一行,若欧拉回路存在则输出1,否则输出0。
    样例输入:
    3 3
    1 2
    1 3
    2 3
    3 2
    1 2
    2 3
    0
    样例输出:
    1
    0
    http://ac.jobdu.com/problem.php?pid=1027

    /*******************************************/
    View Code
     1 #include <iostream>
     2 
     3 using namespace std;
     4 
     5 int find(int map[],int x);
     6 
     7 int main()
     8 {
     9     int map[1005],degree[1005],count1,count2;
    10     int N, M,i,x,y;
    11     while(cin>>N && N>0) {
    12         cin>>M;
    13         for(i=0;i<=N;i++) {
    14             map[i]=i;
    15             degree[i]=0;
    16         }
    17         for(i=0;i<M;i++) {
    18             cin>>x>>y;
    19             degree[x]++;
    20             degree[y]++;
    21             map[find(map,y)]=map[find(map,x)];
    22         }
    23         count1=count2=0;
    24         for(i=1;i<=N;i++) {
    25             if(map[i]==i) count1++;
    26             if(degree[i]==0 || degree[i]%2==1) count2++;
    27         }
    28         if(count1==1 && count2==0) cout<<1<<endl;
    29         else cout<<0<<endl;
    30     }
    31     return 0;
    32 }
    33 
    34 int find(int map[], int x) {
    35     return map[x]==x ? x : find(map,map[x]) ;
    36 }
  • 相关阅读:
    [源码]一个简单的源代码行数统计器
    [转载]一个java程序员的面试
    [源码]用c#创建支持多语言的应用程序
    Effective C#
    [源码]类似于 word 的颜色选择器 ColorPicker
    opera中开启WebGL
    Java学习之路
    邮箱正则表达式写法
    AX 2009 扩展类型控件的Lookup的写法
    唐骏的管理名言
  • 原文地址:https://www.cnblogs.com/wizzhangquan/p/2912971.html
Copyright © 2020-2023  润新知