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的时候, ...
随机推荐
- 5-基于TMS320C6678+XC7K325T的6U CPCIe高性能处理平台
基于TMS320C6678+XC7K325T的6U CPCIe高性能处理平台 一.板卡概述 本板卡系自主研发,基于CPCI 6U架构,符合CPCI2.0标准.采用 DSP TMS320C66 ...
- 1-基于Xilinx XCKU115的半高PCIe x8 硬件加速卡
基于Xilinx XCKU115的半高PCIe x8 硬件加速卡 一.概述 本板卡系我公司自主研发,采用Xilinx公司的XCKU115-3-FLVF1924-E芯片作为主处理器,主要用于FPGA硬件 ...
- OpenVINO 安装及使用
安装 https://docs.openvinotoolkit.org/latest/_docs_install_guides_installing_openvino_linux.html 使用 文档 ...
- Linux架构之Nginx Web基础1
第41章 Nginx Web基础入门 41.1 Nginx部署 41.1.1 Nginx的安装方式 源码编译 官方仓库 epel仓库 优点 规范 安装简单 安装简单 便于管理 配置易读 缺 ...
- Sass:@at-root
@at-root 从字面上解释就是跳出根元素.当你选择器嵌套多层之后,想让某个选择器跳出,此时就可以使用 @at-root.来看一个简单的示例: .a { color: red; .b { color ...
- MySQL WAL
原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11447794.html WAL: Write-Ahead Logging 先写日志,再写磁盘.具体说, ...
- Callable使用示例
之前工作中也有使用过Callable,但是却没有使用Thread对象去操作过,今晚 小组培训,涨知识了,现特意记录一下,以免忘记. 先看一下Thread的构造器 可以看到,Thread类并没有提供参数 ...
- Django2 + ORM
创建模型类class UserInfo(models.Model): id = models.IntegerField() username = models.CharField(max_length ...
- php stristr()函数 语法
php stristr()函数 语法 作用:返回一个字符串在另一个字符串中开始位置到结束的字符串,不区分大小写.直线电机厂家怎么样? 语法:stristr(string,search,before_s ...
- Strassen __128int
题目链接 题意思路很简单,递归求最小就好了.但__128int没见过.故写博客记下.__128int如果输入输出就要自己写函数. #include<bits/stdc++.h> using ...