哈密顿回路

需要满足四个条件:

  • 路径的起点和终点一样
  • 路径的长度为图中顶点数目+1
  • 路径经过了每一个点
  • 路径上每相邻的两个点之间都有边 ```cpp

    include

    include

    include

using namespace std;

const int N = 210;

bool g[N][N], st[N]; int nodes[N * 2]; int n, m;

bool check(int cnt) { if (cnt != (n + 1) || nodes[0] != nodes[cnt - 1]) { return false; }

  1. memset(st, false, sizeof st);
  2. for (int i = 0; i < cnt - 1; i++) {
  3. st[nodes[i]] = true;
  4. if (!g[nodes[i]][nodes[i + 1]]) {
  5. return false;
  6. }
  7. }
  8. for (int i = 1; i <= n; i++) {
  9. if (st[i] == false) {
  10. return false;
  11. }
  12. }
  13. return true;

}

int main() {

  1. #ifdef SUBMIT
  2. freopen("in.txt", "r", stdin);
  3. freopen("out.txt", "w", stdout);
  4. long _begin_time = clock();
  5. #endif
  6. cin >> n >> m;
  7. while (m--) {
  8. int a, b;
  9. cin >> a >> b;
  10. g[a][b] = g[b][a] = true;
  11. }
  12. int k;
  13. cin >> k;
  14. while (k--) {
  15. int cnt;
  16. cin >> cnt;
  17. for (int i = 0; i < cnt; i++) cin >> nodes[i];
  18. if (check(cnt)) puts("YES");
  19. else puts("NO");
  20. }
  21. #ifdef SUBMIT
  22. long _end_time = clock();
  23. printf("\n\ntime = %ld ms", _end_time - _begin_time);
  24. #endif
  25. return 0;

} ```