#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
typedef long long ll;
typedef pair<int, int>PII;
const int INF = 0x3f3f3f3f;
int e[N], ne[N], h[N], cnt;
int backup[N],di[N];
void add(int a, int b)
{
e[cnt] = b, ne[cnt] = h[a], h[a] = cnt++;
}
int main()
{
int n, m;
memset(h, -1, sizeof h);
cin >> n >> m;
while (m--)
{
int a, b;
cin >> a >> b;
add(a, b);
add(b, a);
di[a]++;//统计入度
di[b]++;
}
int k;
cin >> k;
while (k--)
{
memcpy(backup,di,sizeof di);//每次判断都使用入度表的备份
bool ans = true;
int d;
cin >> d;
for (int i = 1; i <= d; i++)
{
int x;
cin >> x;
backup[x] = 0;//被攻打城市入度归0
for (int k = h[x];k != -1; k = ne[k])
{
int j = e[k];
backup[j]--;
}
}
for (int k = 1; k <= n; k++)
{
if (backup[k] > 0)遍历所有点,如果出现入度不为0的点则说明有联通城市
{
ans = false;
break;
}
}
if (ans)cout << "YES" << endl;
else cout << "NO" << endl;
}
return 0;
}
题意就是判断攻打下给定城市后剩余城市是否均不连通。一开始理解错题意wa了2发,后来过了。思路就是建个图,统计每个点的入度,每次攻打下一个城市将这个城市的入读归0,这个城市通向的城市入度减1。#include <bits/stdc++.h>using namespace std;const int N = 1e5 + 10;typedef long long ll;typedef pair<int, int>PII;const int INF = 0x3f3f3