1.判断无向图的欧拉回路和欧拉路径
#include <iostream>
#include <cstring>
using namespace std;
const int MAX_N = 100;
const int MAX_M = 10000;
struct edge {
int v, next;
int len;
} E[MAX_M];
int p[MAX_N], eid;
void init() {
memset(p, -1, sizeof(p));
eid = 0;
}
void insert(int u, int v) {
E[eid].v = v;
E[eid].next = p[u];
p[u] = eid++;
}
int n,m;
int degree[MAX_N];
int cnt;
bool vis[MAX_N];
void dfs(int u){
vis[u] = true;
cnt++;
for(int i=p[u];i!=-1;i=E[i].next){
int v = E[i].v;
if(!vis[v]){
dfs(v);
}
}
}
void euler(){
dfs(1);
if(cnt!=n){
cout<<"It doesn't have an euler path!"<<endl;
return;
}
int cntodd = 0;
for(int i=1;i<=n;i++){
if(degree[i]%2 == 1){
cntodd++;
}
}
if(cntodd == 0){
cout<<"It has an euler circuit!"<<endl;
}else if(cntodd == 2){
cout<<"It has an euler path!"<<endl;
}else{
cout<<"It doesn't have an euler path!"<<endl;
}
}
int main() {
init();
memset(degree,0,sizeof(degree));
cin>>n>>m;
for(int i=0;i<m;i++){
int u,v;
cin>>u>>v;
insert(u,v);
insert(v,u);
degree[u]++;
degree[v]++;
}
euler();
return 0;
}
2.判断无向图的欧拉回路和欧拉路径
#include <iostream>
#include <cstring>
using namespace std;
const int MAX_N = 100;
const int MAX_M = 10000;
struct edge {
int v, next;
int len;
} E[MAX_M];
int p[MAX_N], eid;
void init() {
memset(p, -1, sizeof(p));
eid = 0;
}
void insert(int u, int v) {
E[eid].v = v;
E[eid].next = p[u];
p[u] = eid++;
}
int n,m;
int degree[MAX_N];
int euler(){
int first = 0,last = 0;
for(int i = 1;i <= n;i++){
if(degree[i]<-1 || degree[i] > 1){
cout<<"It doesn't have an euler path!"<<endl;
return 0;
}else if(degree[i] == -1){
if(first != 0){
cout<<"It doesn't have an euler path!"<<endl;
return 0;
}else{
first = i;
}
}else if(degree[i] == 1){
if(last != 0 ){
cout<<"It doesn't have an euler path!"<<endl;
return 0;
}else{
last = i;
}
}
}
if(first == 0 && last == 0){
cout<<"It has an eluer circuit!"<<endl;
return 1;
}else if(first != 0 && last != 0){
cout<<"It has an euler path!"<<endl;
return first;
}
}
int main() {
init();
memset(degree,0,sizeof(degree));
cin>>n>>m;
for(int i=0;i<m;i++){
int u,v;
cin>>u>>v;
insert(u,v);
degree[u]--;
degree[v]++;
}
euler();
return 0;
}
3.找出有向图的欧拉回路
#include <iostream>
#include <cstring>
#include <stack>
using namespace std;
const int MAX_N = 100;
const int MAX_M = 10000;
struct edge {
int v, next;
int len;
} E[MAX_M];
int p[MAX_N], eid;
void init() {
memset(p, -1, sizeof(p));
eid = 0;
}
void insert(int u, int v) {
E[eid].v = v;
E[eid].next = p[u];
p[u] = eid++;
}
int n, m;
int degree[MAX_N];
int euler() {
int first = 0, last = 0;
for (int i = 1; i <= n; i++) {
if (degree[i] < -1 || degree[i] > 1) {
return 0;
} else if (degree[i] == -1) {
if (first != 0) {
return 0;
} else {
first = i;
}
} else if (degree[i] == 1) {
if (last != 0) {
return 0;
} else {
last = i;
}
}
}
if (first == 0 && last == 0) {
return 1;
} else if (first != 0 && last != 0) {
return first;
} else {
return 0;
}
}
stack<int> path;
void dfs(int u){
while(p[u] != -1){
int v = E[p[u]].v;
p[u] = E[p[u]].next;//删除这条边就是把p[u]赋值位E[p[u]].next
dfs(v);
}
path.push(u);
}
bool euler_find(){
int start;
start = euler();
if(start == 0){
return false;
}else{
dfs(start);
return path.size() == m + 1;
}
}
int main() {
init();
memset(degree,0,sizeof(degree));
cin>>n>>m;
for(int i=0;i<m;i++){
int u,v;
cin>>u>>v;
insert(u,v);
degree[u]--;
degree[v]++;
}
if(!euler_find()){
cout<<"It doesn't have an euler path!"<<endl;
}else{
while(!path.empty()){
cout<<path.top()<<endl;
path.pop();
}
}
return 0;
}