7-7 汉密尔顿回路 (25 分)
这道题就是问是否是回路,回路满足:1.过所有的点 2.没有返回
#include <string.h>
#include <stdio.h>
#include <stdbool.h>
#define fer for (int i = 0; i < m; i++)
int main()
{int n, m;scanf("%d %d", &n, &m);bool G[301][301];memset(G, false, sizeof(G));fer{int a, b;scanf("%d %d", &a, &b);G[a][b] = G[b][a] = true;}scanf("%d", &m);fer{int k, start, pre;scanf("%d", &k);bool vis[1001], flag = (k == n + 1);memset(vis, false, sizeof(vis));for (int j = 0; j < k; j++){int num;scanf("%d", &num);if (j == 0)start = num;else if (G[pre][num] == false)flag = false;if (vis[num] && (num != start || j != n))//这里也可以把n改成k-1;flag = false;pre = num;vis[num] = true;}if (flag==false) printf("NO\n");else printf("YES\n");}return 0;
}