zoukankan      html  css  js  c++  java
  • 关于欧拉回路&&连通图

    题目描述:
        欧拉回路是指不令笔离开纸面,可画过图中每条边仅一次,且可以回到起点的一条回路。现给定一个图,问是否存在欧拉回路?
    输入:
        测试输入包含若干测试用例。每个测试用例的第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 }
  • 相关阅读:
    ubuntu 下安装Angular2-cli脚手架
    git的使用及常用命令(二)
    framework7+node+mongo项目
    LINUX下安装搭建nodejs及创建nodejs-express-mongoose项目
    初学strurs基础
    JAVA Struts2 搭建
    mongodb的基本操作
    LightOj_1342 Aladdin and the Magical Sticks
    Codeforces Round #Pi (Div. 2)
    Codeforces Round #315 (Div. 2)
  • 原文地址:https://www.cnblogs.com/wizzhangquan/p/2912971.html
Copyright © 2011-2022 走看看