欧拉回路入门

摘要:
如果从一个节点开始经历整个图的所有边有且仅有一次,然后回到了起点,这个就是欧拉回路。欧拉回路,知识点已经在上个博客提到。用mark[]数组来记录,如果i点奇度,那么i所在图不是欧拉回路,那么i的根节点标为1,代表此图不是欧拉回路。mark[f]=1;ans++;}}ans/=2;for  //统计欧拉回路图{if      //比如输入,939个点只给出了3个关系,肯定有点不算,cnt[i]=0,不能纳入计算。

如果从一个节点开始经历整个图的所有边有且仅有一次,然后回到了起点,这个就是欧拉回路。一笔画问题。

判断方法:

1.无向图中:所给定的图为连通图,且所有节点的度为偶数。
2.有向图中:所给定的图为连通图,且所有节点的度为零。

基础题HDU 1878判断是否为欧拉回路(无向图)

题解:是的话输出1,否则输出0;这里我忘了一个东西,就是如果仅有一个节点i == pr[i],那么此图就构成了一个回路。那么就很明了了,用并查集存图并判断是否为回路,用cnt[]来记录每一个点的出度数目,均为偶数且只有一个i==pr[i],那么就是一个欧拉回路。

#include<iostream>#include<algorithm>#include<cstdio>#include<cstring>
using namespacestd;
int pr[1005],cnt[1005];
int find(intx)
{
    if(x!=pr[x])
        return pr[x]=find(pr[x]);
        returnx;
}
void join(int a,intb)
{
    int f1=find(a),f2=find(b);
    if(f1!=f2)
        pr[f1]=f2;
    return;
}
intmain()
{
    intn,m;
    while(cin>>n)
    {
        if(n==0)
            break;
        cin>>m;
        memset(cnt,0,sizeof(cnt));
        for(int i=1;i<=n;i++)
            pr[i]=i;
        for(int i=1;i<=m;i++)
        {
            inta,b;
            cin>>a>>b;
            cnt[a]++;
            cnt[b]++;
            join(a,b);
        }
        int du=0;
        int ou=0;
        for(int i=1;i<=n;i++)
        {
            if(cnt[i]%2==0)
                ou++;
            if(pr[i]==i)
                du++;
        }
        if(ou==n&&du==1)
            cout<<"1"<<endl;
        elsecout<<"0"<<endl;
    }
}

HDU - 3018

Ant Country consist of N towns.There are M roads connecting the towns.
Ant Tony,together with his friends,wants to go through every part of the country.
They intend to visit every road , and every road must be visited for exact one time.However,it may be a mission impossible for only one group of people.So they are trying to divide all the people into several groups,and each may start at different town.Now tony wants to know what is the least groups of ants that needs to form to achieve their goal.

InputInput contains multiple cases.Test cases are separated by several blank lines. Each test case starts with two integer N(1<=N<=100000),M(0<=M<=200000),indicating that there are N towns and M roads in Ant Country.Followed by M lines,each line contains two integers a,b,(1<=a,b<=N) indicating that there is a road connecting town a and town b.No two roads will be the same,and there is no road connecting the same town.OutputFor each test case ,output the least groups that needs to form to achieve their goal.Sample Input

3 3
1 2
2 3
1 3

4 2
1 2
3 4

Sample Output

1
2


        
 

Hint

New ~~~ Notice: if there are no road connecting one town ,tony may forget about the town.
In sample 1,tony and his friends just form one group,they can start at either town 1,2,or 3.
In sample 2,tony and his friends must form two group.
给出一个图,问几笔画才能经过所有边。
欧拉回路,知识点已经在上个博客提到。对于每个点的出度,如果存在奇数,那么需要奇数/2笔才能经过所有的点。
给出的图并没有说明是否为连通图,所以可能有多个图,那么这种情况,ans=奇数度个数/2+欧拉回路个数(只含偶数点的集合)
解析在代码里
#include<iostream>#include<algorithm>#include<cstdio>#include<cstring>
using namespacestd;
typedef long longll;
const int maxn=1e5+10;    //jishu/2+oulatu
intpr[maxn],cnt[maxn],mark[maxn];
intn,m;
int ans=0;
voidfirst()
{
    for(int i=1;i<=n;i++)
    {
        pr[i]=i;
    }
    memset(cnt,0,sizeof(cnt));
    memset(mark,0,sizeof(mark));
    ans=0;
}
int find(intx)
{
    if(x!=pr[x])
        return pr[x]=find(pr[x]);
        returnx;
}
void join(int a,intb)
{
    int f1=find(a),f2=find(b);
    if(f1!=f2)
        pr[f1]=f2;
        return;
}
voidac()
{
    for(int i=1;i<=n;i++)
    {
        if(cnt[i]%2!=0)
        {
            int f=find(i);    //统计奇数度点数量。用mark[]数组来记录,如果i点奇度,那么i所在图不是欧拉回路,那么i的根节点标为1,代表此图不是欧拉回路。
            mark[f]=1;
            ans++;
        }
    }
    ans/=2;
    for(int i=1;i<=n;i++)  //统计欧拉回路图
    {
        if(cnt[i]>0)      //比如输入,9  3  9个点只给出了3个关系,肯定有点不算,cnt[i]=0,不能纳入计算。
        {                
            int f=find(i);    //找到i的根节点,如果没被标为1,说明i出度为偶数,而且满足pr[i]==i(i==f)(即搜到x==pr[x]时还是没被标记)说明此图是个欧拉回路,因为如果存在奇度点,i==pr[i]
//处肯定被标记了。      ans++; if(mark[f]==0&&pr[i]==i) { ans++; } } } } intmain() { while(cin>>n>>m) { first();    //初始化 for(int i=1;i<=m;i++) { inta,b; cin>>a>>b; join(a,b); cnt[a]++;  //加入并查集,统计入度出度 cnt[b]++; } ac(); cout<<ans<<endl; } return 0; }

免责声明:文章转载自《欧拉回路入门》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Jmeter模拟http请求MFC中Carray的使用下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

随便看看

docker run hangs问题排查记录

1.故障描述过去两天遇到了一个非常奇怪的问题。现在完整的故障描述如下:1)首先,我的同事告诉我,K8S集群中的一个工作节点将其状态更改为NoReady,并且在节点kubelet_truntime的错误日志中发现了大量此类日志E060301:50:51.45511776268remote。go:332]ExecSync1f0e3ac13faf224129bc4...

01 . 美团全链路监控CAT简介及部署

现任携程架构总监)领导基于Java开发的实时应用程序监控平台的设计。作为大众点评网的基本监控组件,AT为大众点评网业务线提供系统的性能指标、健康状态、基本警报等。如何有效定位故障并缩短故障。。。监控是运维工作中最重要的环节,吴启民也是开源实时监控系统CAT的作者。系统故障监控、业务指标监控、应用程序性能监控、用户行为监控、安全合规性监控等,如分布式监控系统C...

MarkDown技巧:两种方式实现页内跳转

MarkDown技术:有两种方法可以跳转到页面上的电子邮件地址:JohnTsai.Work@gmail.com,欢迎交流讨论。我喜欢MarkDown简单直观的写作风格。...

PB各对象常用事件

1.触发窗口中事件名称的时间01.在激活窗口之前激活触发器02。单击触发器03.Close触发器04.CloseQuery在窗口被清除或关闭时触发。...

Vue中在移动端如何判断设备是安卓还是ios

u、 匹配(/(i[^;]+;(U;)?CPU+MacOSX/);如果(isiOS){return“ios”;}否则{return“android”;}},...

vue+echarts组件销毁

使用echarts.init(document.getElementById('xxx'))。dispose();然后获取渲染数据。根据下拉框进行切换。切换绘图时,divv if=“alive”&gt//Scriptdata(){return{alive;true}}:方法,{//查询按钮触发器:根据下拉列表确定要调用的接口。...