Codeforces - 1176E - Cover it! - bfs
https://codeforc.es/contest/1176/problem/E
久了不写bfs了。一开始用dfs写,的确用dfs是很有问题的,一些奇怪的情况就会导致多染一些色。
注意无向图的边要开双倍。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int n, m;
struct Edge {
int u, v;
int next;
Edge() {}
Edge(int u, int v, int next): u(u), v(v), next(next) {}
};
struct Graph {
Edge edge[400005];
int head[200005], etop;
void init(int n) {
etop = 0;
memset(head + 1, -1, sizeof(head[1])*n);
}
void add_edge(int u, int v) {
edge[++etop] = Edge(u, v, head[u]);
head[u] = etop;
edge[++etop] = Edge(v, u, head[v]);
head[v] = etop;
}
} g;
bool visited[200005];
bool color[200005];
int dis[200005];
int que[200005];
int qhead, qtail;
int cntodd, cnteven;
void enqueue(int id, int d) {
if(visited[id])
return;
visited[id] = true;
dis[id] = d;
if(d & 1)
cntodd++;
else
cnteven++;
que[++qtail] = id;
}
void bfs() {
memset(visited + 1, false, sizeof(visited[1])*n);
qhead = 1, qtail = 0;
cntodd = 0, cnteven = 0;
enqueue(1, 0);
while(qhead <= qtail) {
int u = que[qhead++];
for(int i = g.head[u]; i != -1; i = g.edge[i].next) {
enqueue(g.edge[i].v,dis[u]+1);
}
}
}
int main() {
#ifdef Yinku
freopen("Yinku.in", "r", stdin);
//freopen("Yinku.out", "w", stdout);
#endif // Yinku
int t;
while(~scanf("%d", &t)) {
while(t--) {
scanf("%d%d", &n, &m);
g.init(n);
for(int i = 1; i <= m; i++) {
int u, v;
scanf("%d%d", &u, &v);
g.add_edge(u, v);
}
bfs();
int ch = 0;
if(cntodd <= cnteven)
ch = 1;
int cnt = 0, last = -1;
for(int i = 1; i <= n; i++) {
if((dis[i] & 1) == ch) {
cnt++;
last = i;
}
}
printf("%d\n", cnt);
for(int i = 1; i <= n; i++) {
if((dis[i] & 1) == ch) {
printf("%d", i);
if(i == last) {
printf("\n");
break;
} else {
printf(" ");
}
}
}
}
}
}
Codeforces - 1176E - Cover it! - bfs的更多相关文章
- Vladik and Favorite Game CodeForces - 811D (思维+BFS+模拟+交互题)
D. Vladik and Favorite Game time limit per test 2 seconds memory limit per test 256 megabytes input ...
- Codeforces | CF1037D 【Valid BFS?】
题目大意:给定一个\(n(1\leq n\leq 2\cdot10^5)\)个节点的树的\(n-1\)条边和这棵树的一个\(BFS\)序\(a_1,a_2,\dots,a_n\),判断这个\(BFS\ ...
- Number Clicker CodeForces - 995E(双向bfs)
双向bfs 注意数很大 用map来存 然后各种难受....
- Valid BFS? CodeForces - 1037D(思维 bfs)
我真是一只菜狗......emm... 题意: 判断一个从1开始的队列是否可以按照bfs的顺序 进行遍历..必须从1开始...然后后边依次是bfs顺序 解析: 看代码能看懂吧...emm...就是把每 ...
- Fair CodeForces - 987D(巧妙bfs)
题意: 有n个城市 m条边,每条边的权值为1,每个城市生产一种商品(可以相同,一共k种),求出分别从每个城市出发获得s种商品时所走过路的最小权值 解析: 我们倒过来想,不用城市找商品,而是商品找城市, ...
- CodeForces 540C Ice Cave (BFS)
题意:给定 n * m的矩阵,让你并给定初始坐标和末坐标,你只能走'.',并且走过的'.'都会变成'X',然后问你能不能在末坐标是'X'的时候走进去. 析:这个题,在比赛时就是没做出来,其实是一个水题 ...
- codeforces 1072D Minimum path bfs+剪枝 好题
题目传送门 题目大意: 给出一幅n*n的字符,从1,1位置走到n,n,会得到一个字符串,你有k次机会改变某一个字符(变成a),求字典序最小的路径. 题解: (先吐槽一句,cf 标签是dfs题????) ...
- CodeForces - 540C Ice Cave —— BFS
题目链接:https://vjudge.net/contest/226823#problem/C You play a computer game. Your character stands on ...
- Codeforces 920E(补图BFS)
题意: n(n<=200000)个点的完全图删去了m(m<=200000)条边,求剩下图的连通分量. 分析: 将未访问过的点用一个链表串起来 仍旧进行BFS,每次BFS扩展一个点u的时候, ...
随机推荐
- Python基础入门一文通 | Python2 与Python3及VSCode下载和安装、PyCharm激活与安装、Python在线IDE、Python视频教程
目录 1. 关键词 2. 推荐阅读 2.1. 视频教程 3. 本文按 4. 安装 4.1. 视频教程 4.2. 资源下载 4.3. 安装教程 1. 关键词 Python2 与Python3及VSCod ...
- vsftp 主动模式安装
server:192.168.109.137 client:192.168.109.138 ------------------------------------------------------ ...
- selenium鼠标悬停失效,用js语句模拟
写脚本时,有很多case需要要用的鼠标悬停出菜单 用到了ActionChains(self.driver).move_to_element(el).perform(),但是脚本写完以后,单个case执 ...
- alert(1) to win 8
function escape(s) { return '<script>console.log("' + s.toUpperCase() + '")</scri ...
- php连接access
1.在控制面板中打开管理工具图标. 2.双击其中的数据源(ODBC)图标. 3.选择系统 DSN 选项卡. 4.点击系统 DSN 选项卡中的添加. 5.选择Microsoft Access Drive ...
- 学习python os commands socket模块
import os print(os.getcwd()) #获取当前路径, 导包也是从这个路径下面才能找到 # os.chdir('./..') #返回上一级路径,再获取路径看看 # print(os ...
- Jenkins 使用python进行调度,并下载apphost上的安装包
在持续集成的过程中,Jenkins工具是我们必须要会用的工具,那么今天分享一个使用python对Jenkins进行调度的案例 使用的是python-jenkins 库,借用selenium登陆jenk ...
- Spring MVC (二)
一.使用 @RequestMapping 映射请求 Spring MVC使用@RequestMapping注解为控制器指定可以处理哪些URL请求 在控制器的类定义以及方法定义处都可以标注 类定义处:提 ...
- 如何在mac上使用iMessage发送信息
在Mac上你也可以像iPhone上一样使用iMessage 来发送iMessage 与 普通的短信息. 并且你需要在iPhone上设置中的信息的信息转发中激活对电脑的支持.此时, 你的电脑也可以向你的 ...
- IDEA git 合并多个commit
当前三个commit,demo1,demo2,demo3 选择demo1右键 选择action 跟着指示操作,最后合并 时间线: Log 框时间线:是从上到下,越来越早. 弹出框时间线:是从上到下,越 ...