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

边栏推荐
- Double pointer problem (middle)
- 4. Web Development
- 模拟问题(中)
- 5. View parsing and template engine
- 斐波那契数列的递归优化《备忘录递归》
- Xiamen SenseCore Technology MC3172(1): Introduction and Environment Construction
- SVN View Username and Password
- 【 notes 】 the beauty of the software engineering - column 31 | software testing are responsible for the quality of products?
- Plan for many situations in the processing chain
- Discourse Custom Header Links
猜你喜欢

Hexagon_V65_Programmers_Reference_Manual (14)

mysql无法远程连接 Can‘t connect to MySQL server on ‘xxx.xxx.xxx.xxx‘ (10060 “Unknown error“)

动态规划问题(完结篇)

解决报错SyntaxError: (unicode error) ‘utf-8‘ codec can‘t decode byte 0xb7 in position 0: invalid start b

Protobuf compound data types, speaking, reading and writing

Repetition XXL - JOB scheduling center background arbitrary command execution

pyinstaller打包程序所遇问题记录

How with Mexico Volkswagen VW EDI connection to Mexico

DAY17: weak password detection and test

Alibaba Cloud's EasyNLP Chinese text image generation model takes you to become an artist in seconds
随机推荐
2.6 Merge Sort
[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
1. Get data - requests.get()
POJ1321 chessboard problem (detailed explanation)
Six, read application configuration + log configuration
22. Why do you need a message queue?What are the advantages of using the message queue?
Solve the error SyntaxError: (unicode error) 'utf-8' codec can't decode byte 0xb7 in position 0: invalid start b
Machine Learning: Knowing the Dimensionality Reduction Process Through Low Variance Filtering
WPF study notes "WPF Layout Basics"
如何让 (a == 1 && a == 2 && a == 3) 的值为true?
Building and sharing the root of the digital world: Alibaba Cloud builds a comprehensive cloud-native open source ecosystem
LeetCode Algorithm 2326. Spiral Matrix IV
String Problem (Part 1)
Naive Bayes Classification
小程序npm包--API Promise化
Hexagon_V65_Programmers_Reference_Manual(12)
四、Web开发
WPF introduces ttf icon file usage record
六、读取应用配置+日志配置
[High Performance Computing] openMP