跳到正文

个人记录

一笔画

taotao2007约 2 分钟
原文
文章信息与版本

文章信息

发布
2021年6月15日
收录
2025年12月12日
更新
2026年12月2日
篇幅
约 2 分钟

版本

  1. v2当前
  2. v1
C++
#include <bits/stdc++.h>
using namespace std;
const int N=100;
int g[N][N],d[N],q[N],v[N],p[N*N],n,e,top;
int bfs(int x){
	int f,r,num;
	memset(v,0,sizeof(v));
	cout<<"bfs:";
	f=r=1; q[f]=x; v[x]=1; num=1;
	cout<<x<<" ";
	while (f<=r) {
		int y=q[f];   
		for (int j=1;j<=n;j++)  
			if (g[y][j]>0 && v[j]==0){ 
				q[++r]=j;  
				v[j]=1;
				num++;  
				cout<<j<<" ";
			}
		f++; 
	}
	cout<<endl;
	if (num==n) return 1; else return 0;
}
void dfs(int x){
	p[top++]=x;
	if (top==e+1) {
		//print(); 
	}
	for (int j=1;j<=n;j++){
		if (g[x][j]>0 ){
			g[x][j]=g[j][x]=0; 
			dfs(j);
			top--;
			g[x][j]=g[j][x]=1;
		}
	}
}
int main(){
	int x,y;
	cin>>n;
	cin>>e;
    for (int i=1;i<=e;i++){
		cin>>x>>y;
		d[x]++; d[y]++;
		g[x][y]=g[y][x]=1;
	}
    int snum=0;
	x=1;
	for (int i=1;i<=n;i++) if (d[i]%2>0) snum++,x=i;
	//可以一笔画?
	if (snum<=2 && bfs(x)>0) {
	}
	else cout<<"NO!";
	
	
}

/*
5 6
1 2
1 5
2 5
2 3
3 4
4 5

6 12
1 2
1 3
1 4
1 5
2 3
2 4
2 6
3 5
3 6
4 5
4 6
5 6

6 6
1 2
2 3
1 3
4 5
4 6
5 6

4 6
1 2
1 3
1 4
2 3
2 4
3 4



*/
原文