当前位置:网站首页>L2-025 分而治之
L2-025 分而治之
2022-07-30 04:45: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
有关图的一道模拟题。一开始我是使用邻接矩阵来存储各个城市的路径,但提交时最后一个测试用例总是不过,而且倒数第二个耗时总是400ms以上。
后来改用邻接表的方式就能顺利通过所有测试点。而且时间较前者也有很大的提升。
#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;
}

边栏推荐
- Simulation problem (below)
- 3. Dependency configuration management
- 2.6 Radix sort (bucket sort)
- 5. View parsing and template engine
- Notes on "The Law of Construction"---Chapter 10 Typical Users and Scenarios
- Protobuf compound data types, speaking, reading and writing
- file system two
- Introduction to Thymeleaf
- 【 notes 】 the beauty of the software engineering - column 31 | software testing are responsible for the quality of products?
- 2.4 hill sorting
猜你喜欢

VisualStudio2022本地调试进入特别慢问题解决

深圳见!云原生加速应用构建专场:来看云原生 FinOps、SRE、高性能计算场景最佳实践

5. View parsing and template engine

A brief introduction to the SSM framework

Discourse 自定义头部链接(Custom Header Links)

GCC Rust is approved to be included in the mainline code base, or will meet you in GCC 13

Code readability, pre-checks, comments and summaries

protobuf 中复合数据类型的读写

Introduction to Thymeleaf

Android Studio implements login registration - source code (connecting to MySql database)
随机推荐
DAY17、CSRF 漏洞
cnpm installation steps
[C language] Program environment and preprocessing
Six, read application configuration + log configuration
Simulation problem (middle)
BindingExpression path error: 'selectMenusList' property not found on 'object' ''ViewModel'
4. Web Development
Seven, custom configuration
LeetCode Algorithm 328. Parity linked list
KubeMeet 报名 | 「边缘原生」线上技术沙龙完整议程公布!
unity初学5 摄像机跟随,边界控制以及简单的粒子控制(2d)
【 notes 】 the beauty of the software engineering - column 31 | software testing are responsible for the quality of products?
protobuf 中复合数据类型的读写
How with Mexico Volkswagen VW EDI connection to Mexico
聊一聊什么是SaaS,以及遇到的问题......
Repetition XXL - JOB scheduling center background arbitrary command execution
全流程调度——Azkaban入门与进阶
复现XXL-JOB 任务调度中心后台任意命令执行漏洞
Perspective transformation matrix of image perspective correction should be matrix (single)/findHomography with getPerspectiveTransformd difference
handler+message [message mechanism]