7 5 1 2 3 4 4 5 6 7 7 6 5 5 1 2 2 3 3 4 4 5 3 1 4 3 1 2 3 2 4 3 3 3 8 4 4 6 6 8 9 37 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 2 3 2 4 2 5 2 6 2 7 2 8 2 9 3 4 3 5 3 6 3 7 3 8 3 9 4 5 4 6 4 7 4 8 4 9 5 6 5 7 5 8 5 9 6 7 6 8 6 9 7 8 7 9 8 9 9 3 9 8 1 2 2 3 3 4 4 6 5 6 6 7 7 8 8 9 4 3 1 2 2 3 3 4 5 5 1 2 1 3 2 4 2 5 5 4 0 0 //不对 #include<stdio.h> #include<stdlib.h> int shu,bian; int data[10000][2]; struct ueue{ int x; int y; int pre_node; }q; ueue queue[10000]; int head,tail; void BFS(); int main() { freopen("a.txt","r",stdin); while(1) { scanf("%d",&shu); scanf("%d",&bian); if(shu == 0 || bian == 0) { return 0; } for(int i = 0;i < bian;i++) { scanf("%d",&data[i][0]); scanf("%d",&data[i][1]); } BFS(); for(int i = 0;i < bian;i++) { data[i][0] = 0; data[i][1] = 0; } for(int k = 0;k < tail;k++) { q.x = 0; q.y = 0; q.pre_node = 0; queue[k] = q; } tail = 0; head = 0; } return 0; } void BFS() { for(int o = 0;o < bian;o++) { if(data[o][0] != 0 && data[o][1] != 0) { int t = 0; q.x = data[o][0]; q.y = data[o][1]; queue[tail] = q; tail += 1; data[o][0] = 0; data[o][1] = 0; while(head != tail) { for(int i = 1;i < bian;i++) { if(data[i][0] == queue[head].y) { q.x = data[i][0]; q.y = data[i][1]; q.pre_node = head; queue[tail] = q; tail++; data[i][0] = 0; data[i][1] = 0; } } head++; } for(int p = o;p < tail;p++) { for(int q = p + 1;q <= tail;q++) { if(queue[p].x == queue[q].y) { printf("%d",queue[q].x); t = queue[q].pre_node; while(1) { printf("%d",queue[t].x); if(queue[t].x == queue[p].x) { printf("\n"); break; } t = queue[t].pre_node; } return; } } } } } printf("no\n"); } //DFS yes #include<stdio.h> #include<stdlib.h> int shu,bian; int data[10000][2]; int dfs[10][10] = {0}; int cun[10] = {0}; int c[10]; int n; bool ret = false; void DFS(int step,int x); int main() { freopen("a.txt","r",stdin); while(1) { scanf("%d",&shu); scanf("%d",&bian); if(shu == 0 || bian == 0) { return 0; } for(int i = 0;i < bian;i++) { ret = false; scanf("%d",&data[i][0]); scanf("%d",&data[i][1]); dfs[data[i][0]][data[i][1]] = 1; } /*for(int i = 1;i < 10;i++) { for(int j = 1;j < 10;j++) { printf("%d ",dfs[i][j]); } printf("\n"); }*/ for(int g = 1;g < 10;g++) { for(int h = 1;h < 10;h++) { if(dfs[g][h] == 1) { DFS(0,g); if(ret == true) { goto v; } } } } printf("no\n"); v:for(int i = 1;i < 10;i++) { for(int j = 1;j < 10;j++) { dfs[i][j] = 0; cun[j] = 0; c[j] = 0; } } n = 0; } return 0; } void DFS(int step,int x) { //if(step == bian) //{ // return; //} for(int i = x;i < 10;i++) { for(int j = 1;j < 10;j++) { if(dfs[i][j] == 1) { dfs[i][j] = 2; cun[i] = 1; c[n] = i; n++; if(cun[j]) { ret = true; for(int k = n - 1;;k--) { printf("%d ",c[k]); if(c[k] == j) { printf("\n"); break; } } break; } DFS(step + 1,j); if(ret) { break; } cun[i] = 0; n--; c[n] = 0; } } return; } }