当前位置:网站首页>Divide and conquer. L2-025
Divide and conquer. L2-025
2022-07-30 05:02:00 【Cod_ing】
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
A simulation problem about graphs.一开始我是使用邻接矩阵to store the paths of each city,But the last test case when submitting is always not,And the penultimate time-consuming always400ms以上.
后来改用邻接表way to successfully pass all test points.And the time is also greatly improved compared to the former.
#include<iostream>
#include<vector>
using namespace std;
const int MAX = 1e4 + 5;
bool killed[MAX]; //被攻占的城市
int N, M;
vector<int> G[MAX]; //邻接表,存储路径
int main() {
int K,x, y;
cin >> N >> M;
for (int i = 0; i < M; i++) {
cin >> x >> y;
if (x > y) swap(x, y);
G[x].push_back(y);
}
cin >> K;
for (int i = 0; i < K; i++) {
cin >> x;
fill(killed, killed + N + 1, false);
bool flag = true;
for (int j = 0; j < x; j++) {
cin >> y;
killed[y] = true;
}
for (int j = 1; j <= N&&flag; j++) {
if (!killed[j]) {
for (int k = 0; k < G[j].size()&&flag; k++) {
if (killed[G[j][k]]) continue;
flag = false;
}
}
}
if (flag) cout << "YES" << endl;
else cout << "NO" << endl;
}
return 0;
}
边栏推荐
- QT(39)-vs development qt program prompts that the source file cannot be opened
- 六、读取应用配置+日志配置
- C. Qualification Rounds
- file system two
- Image stitching (registration) case based on OpenCV
- [MRCTF2020]Hello_misc
- DAY17: weak password detection and test
- 4. Web Development
- String problem (below)
- Catch That Cow (detailed)
猜你喜欢
动态规划问题(完结篇)
Simple experiment with BGP
See you in shenzhen!Cloud native to accelerate the application building special: see cloud native FinOps, SRE, high-performance computing scenario best practices
mysql无法远程连接 Can‘t connect to MySQL server on ‘xxx.xxx.xxx.xxx‘ (10060 “Unknown error“)
L2-025 分而治之
Small program npm package--API Promise
IGBT wafers used in photovoltaic inverters
ThinkPHP高仿蓝奏云网盘系统源码/对接易支付系统程序
Hexagon_V65_Programmers_Reference_Manual(10)
Hexagon_V65_Programmers_Reference_Manual(14)
随机推荐
GO语言学习笔记一
Whole process scheduling - Azkaban entry and advanced
Predictive maintenance scheduling of multiple power equipment based on data-driven fault prediction
C# One Week Introductory "C# - Classes and Objects" Day Six
String Problem (Part 1)
Perspective transformation matrix of image perspective correction should be matrix (single)/findHomography with getPerspectiveTransformd difference
handler+message [message mechanism]
Classification of decision tree classification
SaaS多租户数据隔离的三种解决方案
error: The following untracked working tree files would be overwritten by
Using the GPU parallel computing 】 【 OpenCL&OpenCLUtilty GPU parallel computing
[3D Detection Series-PointRCNN] Reproduces the PointRCNN code, and realizes the visualization of PointRCNN3D target detection, including the download link of pre-training weights (starting from 0 and
模拟问题(中)
Unity3D Application simulation enters the front and background and pauses
七、自定义配置
Hexagon_V65_Programmers_Reference_Manual(11)
go语言学习笔记二
String problem (below)
L2-025 分而治之
斐波那契数列的递归优化《备忘录递归》