当前位置:网站首页>L2-025 分而治之 (25 分)
L2-025 分而治之 (25 分)
2022-06-29 09:23:00 【陌陌623】
本次刷题为2021年天梯
L2-025 分而治之 (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
题解
仅仅需要将击败的点 标注即可
然后遍历1…N一遍,看他们的邻接点,如果有的邻接点可以走。
即这个点可以走向下一个。故不行
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int n = 10005;
vector<int> v[n];
int main()
{
int N, M;
cin >> N >> M;
int x, y;
while (M--)
{
cin >> x >> y;
v[x].push_back(y);
v[y].push_back(x);
}
cin >> M;
while (M--)
{
int K;
cin >> K;
int dp[n] = {0};
for (int i = 0; i < K; i++)
cin >> x, dp[x]++;
int t = 0;
for (int i = 1; i <= N; i++)
if (!dp[i])
for (auto &e : v[i])
if (dp[e] == 0)
{
t = 1;
break;
}
printf("%s", t == 0 ? "YES\n" : "NO\n");
}
return 0;
}
边栏推荐
猜你喜欢

Application of decorator mode, packaging ServletRequest and adding addparameter method

JVM method return address

JVM之虚拟机栈之动态链接

How to traverse objects in the vector container

A 3D Dual Path U-Net of Cancer Segmentation Based on MRI

Fully Automated Gross Tumor Volume Delineation From PET in Head and Neck Cancer Using Deep Learning

Zabbix4.4 configure the indicators of the monitoring server and solve the garbled graphics pages

Container of the basic component of the flutter

A 2.5D Cancer Segmentation for MRI Images Based on U-Net

Sixteen system counter and flow lamp
随机推荐
2019.10.27训练总结
GCC and makefile
gSoap例子——calc
EDA与VHDL题库
完美二叉树、完全二叉树、完满二叉树
gcc与makefile
JVM之方法返回地址
nacos注册中心集群
另类实现 ScrollView 下拉头部放大
ImageView图片填充问题
Application of decorator mode, packaging ServletRequest and adding addparameter method
Wechat applet implements the data listener watch, including the watch that destroys the watch and sub attributes
RecyclerView 粘性(悬浮)头部
The collapsing "2.3 * 10 = 22" produced by multiplying float and int
Language characteristics
URAL1517 Freedom of Choice 【后缀数组:最长公共连续子串】
Leetcode MySQL database topic 177
setInterval、setTimeout和requestAnimationFrame
F5 BIG-IP iControl REST命令执行(CVE-2022-1388)
manacher