7-5 分而治之 (25 分)

7-5 分而治之 (25 分)

分而治之,各个击破是兵家常用的策略之一。在战争中,我们希望首先攻下敌方的部分城市,使其剩余的城市变成孤立无援,然后再分头各个击破。为此参谋部提供了若干打击方案。本题就请你编写程序,判断每个方案的可行性。

输入格式:

输入在第一行给出两个正整数 N 和 M(均不超过10 000),分别为敌方城市个数(于是默认城市从 1 到 N 编号)和连接两城市的通路条数。随后 M 行,每行给出一条通路所连接的两个城市的编号,其间以一个空格分隔。在城市信息之后给出参谋部的系列方案,即一个正整数 K (≤ 100)和随后的 K 行方案,每行按以下格式给出:

Np v[1] v[2] ... v[Np]

其中 Np 是该方案中计划攻下的城市数量,后面的系列 v[i] 是计划攻下的城市编号。

输出格式:

对每一套方案,如果可行就输出YES,否则输出NO

输入样例:

10 11
8 7
6 8
4 5
8 4
8 1
1 2
1 4
9 8
9 1
1 10
2 4
5
4 10 3 8 4
6 6 1 7 5 4 9
3 1 8 4
2 2 8
7 9 8 7 6 5 4 2

输出样例:

NO
YES
YES
NO
NO
#include <iostream>
#include <algorithm>
#include <string.h>
#include <string>
#include <cstdio>
#include <math.h>
#include <vector>
#include <queue>
using namespace std;

const int MAX = 10005;

struct city{
	int s;
	int e;
};

city c[MAX];
int vis[MAX];

int main(){
	
	ios::sync_with_stdio(false);
	
	int n,m;
	cin>>n>>m;
	
	for(int i=0;i<m;i++){
		int a,b;
		cin>>a>>b;
		c[i].s = a;
		c[i].e = b;
	}
	
	int t;
	cin>>t;
	
	for(int i=0;i<t;i++){
		memset(vis,0,sizeof(vis));
		int temp;
		cin>>temp;
		for(int j=0;j<temp;j++){
			int a;
			cin>>a;
			vis[a] = 1;
		}
		
		bool flag = true;
		
		for(int j=0;j<m;j++){
			if(vis[c[j].e] || vis[c[j].s]){
				continue;
			}
			else{
				flag = false;
				break;
			}
		}
		
		if(flag){
			cout<<"YES"<<endl;
		}
		else{
			cout<<"NO"<<endl;
		}
	}
	
	
	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41700374/article/details/86683680
今日推荐