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的时候, ...
随机推荐
- php object
一.访问控制 <?php class Computer{ public $cpu = 880; private $name = 'xiaomi'; public function getname ...
- alert(1) to win 5
function escape(s) { var text = s.replace(/</g, '<').replace(/"/g, '"'); // URLs tex ...
- Ubuntu下批量使用Tecplot的preplot命令对数据进行处理
首先把.PLT文件后缀批量修改为.dat rename 's/.PLT$/.dat/' *.PLT 对所有.dat文件批量执行preplot find ./ -name "*.dat&quo ...
- deque的简单使用
depue 是python提供的一个数据结构,线程安全,功能比list强大 from collections import deque user_list = ['admin', 'root'] us ...
- nodejs通过async/await来操作MySQL
在nodejs中从数据库得到数据后是通过回调函数来操作数据的,如果嵌套多层将非常可怕,代码逻辑和可读性将变得非常差.有时用promise也并不能很好得解决问题,因为如果用了promise后,代码将会有 ...
- CKEditor粘贴图片上传功能
很多时候我们用一些管理系统的时候,发布新闻.公告等文字类信息时,希望能很快的将word里面的内容直接粘贴到富文本编辑器里面,然后发布出来.减少排版复杂的工作量. 下面是借用百度doc 来快速实现这个w ...
- 关闭本机的代理服务(Proxy)
若您使用了代理服务(Proxy),可能会导致战网游戏发生网络连接.安装或更新方面的问题.请参考以下步骤来关闭您电脑的网络代理服务. Windows 按下 Windows 按鍵 + R . 在运行框中, ...
- 170817关于JSON知识点
1. JSON [1] JSON简介 JSON全称 JavaScript Object Notation ...
- 31 October
https://www.cnblogs.com/RabbitHu/p/51nod1353.html 树形 DP 求所有联通块 \(\ge K\) 的方案数. 切断:\(\forall i\in\lef ...
- [CSP-S模拟测试]:砖块(模拟)
题目描述 在一个二维网格平面上,一个网格的坐标由其左下角的点的坐标定义$(x,y)$.在一个二维网格平面上,一个网格的坐标由其左下角的个点的坐标定义$(0,0)$的区域中,此时存在高度为$k$的初始砖 ...