![](https://img-blog.csdnimg.cn/20201014180756757.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
dfs和bfs
Dejavu1z
做题,总结,不断的学习
展开
-
codeforces 1283D(BFS)
题意描述有n个人,要求找到m个圣诞树,使n个人到m个圣诞树之间的距离之和最小思路使用bfs来做,贪心的考虑,我们先找与n个点距离为1的圣诞树,然后再找距离为2的圣诞树,依次类推。AC代码#include<bits/stdc++.h>#define x first#define y second#define PB push_back#define mst(x,a) memset(x,a,sizeof(x))#define all(a) begin(a),end(a)#def原创 2020-09-17 08:12:11 · 195 阅读 · 0 评论 -
codeforces1385D (递归+分治)
题意描述You are given a string s[1…n] consisting of lowercase Latin letters. It is guaranteed that n=2k for some integer k≥0.The string s[1…n] is called c-good if at least one of the following three conditions is satisfied:The length of s is 1, and it consi原创 2020-09-14 16:15:10 · 237 阅读 · 0 评论 -
codeforces 982C (dfs)
题意描述You’re given a tree with n vertices.Your task is to determine the maximum possible number of edges that can be removed in such a way that all the remaining connected components will have even size.给定一棵树,询问最多删除多少条边可以使每个连通块的数量都是偶数思路这道题的even最初理解成了相同的原创 2020-07-27 14:13:28 · 167 阅读 · 0 评论 -
codeforces 320B(dfs)
题意描述In this problem at each moment you have a set of intervals. You can move from interval (a, b) from our set to interval (c, d) from our set if and only if c < a < d or c < b < d. Also there is a path from interval I 1 from our set to interv原创 2020-07-18 15:55:58 · 232 阅读 · 0 评论 -
codeforces 1385D(dfs)
题目描述You are given a string s[1…n] consisting of lowercase Latin letters. It is guaranteed that n=2k for some integer k≥0.The string s[1…n] is called c-good if at least one of the following three conditions is satisfied:The length of s is 1, and it consi原创 2020-07-18 10:50:17 · 425 阅读 · 0 评论 -
codeforces329B(bfs)
题意描述给定RxC的网格,网格中存在猎人,询问走到终点前要和多少组猎人碰面思路由于猎人事先知道我们行走的路线,所以猎人们可以先走到终点前等待,可以使用bfs预处理出终点到各个点之间的距离,如果猎人到终点的距离小于等于我们从起点到终点的距离,那么一点可以相遇,最后遍历一遍即可。代码#include<bits/stdc++.h>#define x first#define y second#define IOS ios::sync_with_stdio(false);cin.tie(原创 2020-07-16 19:07:54 · 157 阅读 · 0 评论 -
超级书架2(dfs)
题意描述给定n个数字,求sum-b的最小值思路每个数字都有选和不选两种思路,所以可以直接dfsAC代码#include<bits/stdc++.h>#define x first#define y second#define IOS ios::sync_with_stdio(false);cin.tie(0);using namespace std;typedef unsigned long long ULL;typedef pair<int,int> PII;原创 2020-07-15 19:44:01 · 419 阅读 · 0 评论 -
codeforces 977D(DFS)
题意描述给你n个数字,要求你改变数字顺序,使后一个数字是前一个数字的2倍或者三分之一思路由于题目保证一定有解,所以我们使用dfs来解决,如果两个数字间满足题目条件,就连一条单向边,最后dfs找到一个能够遍历全部的路径即可AC代码#include<bits/stdc++.h>#define x first#define y second#define IOS ios::sync_with_stdio(false);cin.tie(0);using namespace std;t原创 2020-07-13 15:45:42 · 313 阅读 · 0 评论 -
codeforces 377A(dfs)
题意描述给你一个NxM的网格,要求你添k堵墙,最后仍使所有的空地是联通的思路我们可以反过来想,添k堵墙可以想成给cnt-k个空地构成的连通块打上标记,没有打上标记的即为空地AC代码#include<bits/stdc++.h>#define x first#define y second#define IOS ios::sync_with_stdio(false);cin.tie(0);using namespace std;typedef unsigned long lon原创 2020-07-13 09:28:24 · 214 阅读 · 0 评论 -
codeforces 107A(dfs)
题意描述The German University in Cairo (GUC) dorm houses are numbered from 1 to n. Underground water pipes connect these houses together. Each pipe has certain direction (water can flow only in this direction and not vice versa), and diameter (which character原创 2020-07-11 15:59:13 · 268 阅读 · 0 评论 -
codeforces 429A(dfs)
题意描述给定一棵树,树的每个结点上有一个值,你可以对树上的值进行异或操作,求使树上的结点值成为目标值所需的最少操作思路我们可以发现,如果第一次是对奇数层进行操作,那么第二次再对偶数层进行操作时,上一步的操作就会作废。发现这个性质后,就可以dfs求解。如果需要翻转奇数层,则之前偶数层的操作将作废;如果需要反转偶数层,则之前奇数层的操作将作废。这样的话只需要dfs一次就可以求出解AC代码#include<bits/stdc++.h>#define x first#define y se原创 2020-07-11 15:19:16 · 231 阅读 · 0 评论 -
codeforces 902B(dfs)
题意描述给你一棵树,要求给树染色,给树的一个父结点染色时,该父结点的所有子结点也会被染成同样的颜色,给你颜色列表,求将树染成该列表所用的最小的次数思路遍历树的层次,依次进行染色并统计染色的次数AC代码#include<iostream>#include<cstring>#include<cstdio>#include<algorithm>#include<queue>#define x first#define y secon原创 2020-07-10 16:08:22 · 199 阅读 · 0 评论 -
codeforces 580C(dfs)
题意描述给一棵n个节点树,某个结点可能会打上标记,从根节点走,问有几条路径上的连续标记树不超过m。思路按照题意dfs即可AC代码#include<bits/stdc++.h>#define x first#define y second#define pb push_back#define IOS ios::sync_with_stdio(false);cin.tie(0);using namespace std;typedef unsigned long long ULL原创 2020-06-14 11:08:41 · 331 阅读 · 0 评论 -
codeforces 1182B (DFS)
题意描述You have a given picture with size w×h. Determine if the given picture has a single “+” shape or not. A “+” shape is described below:A “+” shape has one center nonempty cell.There should be some (at least one) consecutive non-empty cells in each di原创 2020-05-22 17:45:24 · 213 阅读 · 0 评论