如果从一个节点开始经历整个图的所有边有且仅有一次,然后回到了起点,这个就是欧拉回路。一笔画问题。
判断方法:
1.无向图中:所给定的图为连通图,且所有节点的度为偶数。
2.有向图中:所给定的图为连通图,且所有节点的度为零。
基础题HDU 1878判断是否为欧拉回路(无向图)
题解:是的话输出1,否则输出0;这里我忘了一个东西,就是如果仅有一个节点i == pr[i],那么此图就构成了一个回路。那么就很明了了,用并查集存图并判断是否为回路,用cnt[]来记录每一个点的出度数目,均为偶数且只有一个i==pr[i],那么就是一个欧拉回路。
#include<iostream> #include<algorithm> #include<cstdio> #include<cstring> using namespace std; int pr[1005],cnt[1005]; int find(int x) { if(x!=pr[x]) return pr[x]=find(pr[x]); return x; } void join(int a,int b) { int f1=find(a),f2=find(b); if(f1!=f2) pr[f1]=f2; return ; } int main() { int n,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++) { int a,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; else cout<<"0"<<endl; } }
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 namespace std;
typedef long long ll;
const int maxn=1e5+10; //jishu/2+oulatu
int pr[maxn],cnt[maxn],mark[maxn];
int n,m;
int ans=0;
void first()
{
for(int i=1;i<=n;i++)
{
pr[i]=i;
}
memset(cnt,0,sizeof(cnt));
memset(mark,0,sizeof(mark));
ans=0;
}
int find(int x)
{
if(x!=pr[x])
return pr[x]=find(pr[x]);
return x;
}
void join(int a,int b)
{
int f1=find(a),f2=find(b);
if(f1!=f2)
pr[f1]=f2;
return;
}
void ac()
{
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++;
}
}
}
}
int main()
{
while(cin>>n>>m)
{
first(); //初始化
for(int i=1;i<=m;i++)
{
int a,b;
cin>>a>>b;
join(a,b);
cnt[a]++; //加入并查集,统计入度出度
cnt[b]++;
}
ac();
cout<<ans<<endl;
}
return 0;
}