PAT 天梯赛 L2-025. 分而治之 【图】

题目链接

https://www.patest.cn/contests/gplt/L2-025

思路
只要把被攻下的城市标记一下 与 其他城市之间的通路都取消

然后判断一下剩下的城市 是否都是孤立的 就可以

AC代码

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define CLR(a) memset(a, 0, sizeof(a))
#define pb push_back

using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair  pll;

const double PI  = acos(-1);
const double E   = exp(1);
const double eps = 1e-6;

const int INF  = 0x3f3f3f3f;
const int maxn = 1e4 + 5;
const int MOD  = 1e9 + 7;

vector <int> G[maxn];

int v[maxn];

void init()
{
    CLR(G);
}

int main()
{
    int n, m;
    scanf("%d%d", &n, &m);
    int x, y;
    for (int i = 0; i < m; i++)
    {
        scanf("%d%d", &x, &y);
        G[x].pb(y);
        G[y].pb(x);
    }
    int T;
    scanf("%d", &T);
    while (T--)
    {
        int k;
        scanf("%d", &k);
        map <int, int> M; 
        for (int j = 0; j < k; j++)
        {
            int num;
            scanf("%d", &num);
            M[num] = 1;
        }
        int flag = 1;
        vector <int>::iterator it;
        for (int i = 0; i < n; i++)
        {
            if (M[i])
                continue;
            for (it = G[i].begin(); it != G[i].end(); it++)
            {
                if (M[*it] == 0)
                {
                    flag = 0;
                    break;
                }
            }
        }
        printf("%s\n", flag? "YES":"NO");
    }
}













转载于:https://www.cnblogs.com/Dup4/p/9433148.html

你可能感兴趣的:(PAT 天梯赛 L2-025. 分而治之 【图】)