rt,写的感觉很合理,但过不了。 题目链接
#include<bits/stdc++.h>
const int maxn=2005;
struct node{
int to,nxt;
}e[maxn<<1];
int t,n,m,tot,h[maxn];
bool vis[maxn],flag;
int sta[maxn],top,siz[maxn];
inline void add(int x,int y){
e[++tot]={y,h[x]},h[x]=tot;
}
bool check(int now){
int res=0;
for(int i=h[now];i;i=e[i].nxt){
int to=e[i].to;
if(!vis[to])res++;
}
if(res>=2)return 1;
return 0;
}
void print(int now,int s){
printf("YES\n");
printf("%d\n",top+2);
for(int i=2;i<=top;i++){
printf("%d %d\n",sta[i-1],sta[i]);
}
printf("%d %d\n",now,s);
int num=0;
for(int i=h[s];i;i=e[i].nxt){
int to=e[i].to;
if(!vis[to]){
printf("%d %d\n",s,to);
num++;
}
if(num==2)break;
}
}
void dfs(int now,int s,int last){
if(flag==1)return;
vis[now]=1,sta[++top]=now;
for(int i=h[now];i;i=e[i].nxt){
int to=e[i].to;
if(to==last)continue;
if(to==s){
if(check(s)){
flag=1;
print(now,s);
return;
}
}else{
if(vis[to])continue;
dfs(to,s,now);
}
}
vis[now]=0,top--;
}
int main(){
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
tot=0;memset(h,0,sizeof(h));
memset(siz,0,sizeof(siz));
for(int i=1;i<=m;i++){
int x,y;
scanf("%d%d",&x,&y);
add(x,y);add(y,x);
siz[x]++,siz[y]++;
}
flag=0;
for(int i=1;i<=n;i++){
if(siz[i]<4)continue;
memset(vis,0,sizeof(vis));top=0;
dfs(i,i,i);
if(flag==1){
break;
}
}
if(flag==0)printf("NO\n");
}
return 0;
}
/*
7 8
6 7
6 4
4 5
4 3
3 2
1 2
1 4
2 4
*/