欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

1122. Hamiltonian Cycle (25) 图论 23'

程序员文章站 2022-03-08 16:17:58
...
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
bool e[201][201];

int main(){
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < m; i++) {
        int a, b;
        cin >> a >> b;
        e[a][b] = e[b][a] = 1;
    }
    int k;
    cin >> k;
    for (int i = 0; i < k; i++) {
        int c, flag = 1;
        cin >> c;
        vector<int> temp(c), visited(c, 0);

        for (int j = 0; j < c; j++) cin >> temp[j];
        if(c != n + 1 || temp[0] != temp[c-1]) flag = 0;
        if (flag) {
            for (int i = 0; i < c - 1; i++) {
                visited[temp[i]]++;
            }
            for (int i = 1; i < c; i++) {
                if(visited[i] != 1) {flag = 0; break;}
            }
        }
        if(flag) cout << "YES\n";
        else cout << "NO\n";
    }

    return 0;
}