当前位置:网站首页>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;
}

边栏推荐
- handler+message [message mechanism]
- 1315_Use the LOOPBACK simulation mode to test whether the pyserial installation is successful
- 字符串问题(下)
- The 2nd Shanxi Province Network Security Skills Competition (Enterprise Group) Part of the WP (9)
- Golang channel implementation principle
- 解决go环境编译不了exe
- Usage of EFR32 as sniffer for Zigbee/Thread
- L2-020 功夫传人
- [Vitis] Code implementation of ZCU102 development board PS-side control PL-side reset
- How with Mexico Volkswagen VW EDI connection to Mexico
猜你喜欢

Some understanding of YOLOv7

Six, read application configuration + log configuration

webService interface

2.6 Radix sort (bucket sort)

05 Detailed explanation of the global configuration file application.properties

Discourse Custom Header Links

Perspective transformation matrix of image perspective correction should be matrix (single)/findHomography with getPerspectiveTransformd difference

1. Get data - requests.get()

uni-app realizes cross-end development of mobile phone Bluetooth to receive and send data

小程序使用npm包定制全局样式
随机推荐
1315_Use the LOOPBACK simulation mode to test whether the pyserial installation is successful
双指针问题(下)
如何让 (a == 1 && a == 2 && a == 3) 的值为true?
[High Performance Computing] openMP
字符串问题(下)
5. View parsing and template engine
4. Web Development
go语言学习笔记二
Simulation Problem (Part 1)
Perspective transformation matrix of image perspective correction should be matrix (single)/findHomography with getPerspectiveTransformd difference
Intermediate - interview questions
Building and sharing the root of the digital world: Alibaba Cloud builds a comprehensive cloud-native open source ecosystem
mysql无法远程连接 Can‘t connect to MySQL server on ‘xxx.xxx.xxx.xxx‘ (10060 “Unknown error“)
复现XXL-JOB 任务调度中心后台任意命令执行漏洞
Compound Types--references, pointers
Usage of EFR32 as sniffer for Zigbee/Thread
Three Solutions for SaaS Multi-tenant Data Isolation
GCC Rust is approved to be included in the mainline code base, or will meet you in GCC 13
Image stitching (registration) case based on OpenCV
Mini Program wx.miniProgram.navigateTo jump address cannot be tabbar address