
dfs
xuanweiace
一个热爱算法竞赛的弱校ACMer路过。青大本,浙大硕,方向后端开发,菜鸡一枚,奋斗ing...
展开
-
Mr. Kitayuta‘s Technology CodeForces - 505D(并查集+拓扑排序或dfs找环) 题解
题目Shuseki Kingdom is the world’s leading nation for innovation and technology. There are n cities in the kingdom, numbered from 1 to n.Thanks to Mr. Kitayuta’s research, it has finally become poss...原创 2020-12-15 23:35:33 · 584 阅读 · 1 评论 -
【牛客 - 301哈尔滨理工大学软件与微电子学院第八届程序设计竞赛同步赛(高年级)】小乐乐搭积木(状压dp)
题干: 小乐乐想要给自己搭建一个积木城堡。积木城堡我们假设为n*m的平面矩形。小乐乐现在手里有1*2,2*1两种地砖。小乐乐想知道自己有多少种组合方案。 输入描述:第一行输入整数n,m。(1<=n,m<=10)输出描述:输出组合方案数。示例1输入复制2 3输出复制3说明示例2输入复制...原创 2018-12-03 22:42:51 · 236 阅读 · 0 评论 -
【牛客 - 301哈尔滨理工大学软件与微电子学院第八届程序设计竞赛同步赛(高年级)】小乐乐下象棋(记忆化搜索dp,dfs)
题干: 小乐乐一天天就知道玩,这一天又想玩象棋。我们都知道马走日。现在给定一个棋盘,大小是n*m,把棋盘放在第一象限,棋盘的左下角是(0,0),右上角是(n - 1, m - 1);小乐乐想知道,一个马从左下角(0, 0)开始,走了k步之后,刚好走到右上角(n - 1, m - 1)的方案数。输入描述:输入:多组样例输入,每组一行,三个整数n, m, k(1 <= n,...原创 2018-12-03 14:33:36 · 285 阅读 · 0 评论 -
【HDU - 1078】FatMouse and Cheese (记忆化搜索dp)
题干:FatMouse has stored some cheese in a city. The city can be considered as a square grid of dimension n: each grid location is labelled (p,q) where 0 <= p < n and 0 <= q < n. At each gr...原创 2018-12-04 14:43:33 · 255 阅读 · 0 评论 -
【HihoCoder - 1851】D级上司 (树形图,dfs)
题干:H公司一共有N名员工,编号为1~N,其中CEO的编号是1。除了CEO之外,每名员工都恰好有唯一的直接上司;N名员工形成了一个树形结构。 我们定义X的1级上司是他的直接上司,2级上司是他上司的上司,以此类推…… 请你找出每名员工的D级上司是谁。Input第一行包含2个整数N和D。 以下N-1行每行包含一个整数,依次代表编号是2-N的员工的直接上司的编号。 对...原创 2018-11-19 19:21:51 · 334 阅读 · 0 评论 -
【CodeForces - 219D 】Choosing Capital for Treeland (树形dp)
题干:The country Treeland consists of n cities, some pairs of them are connected with unidirectional roads. Overall there are n - 1 roads in the country. We know that if we don't take the direction of...原创 2018-11-19 11:40:38 · 276 阅读 · 0 评论 -
【CodeForces - 266C】Below the Diagonal (递归,子问题,贪心模拟)
题干:You are given a square matrix consisting of n rows and n columns. We assume that the rows are numbered from 1 to n from top to bottom and the columns are numbered from 1to n from left to right. S...原创 2018-11-15 22:09:02 · 521 阅读 · 0 评论 -
【HDU - 2376】Average distance (树,平均距离,算贡献)
题干:Given a tree, calculate the average distance between two vertices in the tree. For example, the average distance between two vertices in the following tree is (d 01 + d 02 + d 03 + d 04 + d 12 +d...原创 2018-11-15 18:21:48 · 1053 阅读 · 0 评论 -
【CodeForces - 244B】Undoubtedly Lucky Numbers (dfs打表 + 二分)
题干:Polycarpus loves lucky numbers. Everybody knows that lucky numbers are positive integers, whose decimal representation (without leading zeroes) contain only the lucky digits x and y. For example,...原创 2018-11-08 16:39:18 · 1299 阅读 · 0 评论 -
【牛客 - 318F】关于我转生变成史莱姆这档事(二分,搜索)
题干:有一天,利姆鲁在这个世界最重要的人静被魔王带走,并将其困在一个n*n的迷宫内的某一处,迷宫的每个格子都可能有一只魔物,魔物的攻击力为a[i][j],因而利姆鲁只有当攻击力大于等于a[i][j]才能通过这个方格,否则就只能绕道(只能朝上下左右四个方向)而行。 为了拯救静,利姆鲁决定勇闯迷宫! 现在利姆鲁站在(sx,sy)处,静在(ex,ey)处,为了保证自己能够成功拯救静,...原创 2019-02-16 00:11:07 · 514 阅读 · 0 评论 -
*【HDU - 4272 】LianLianKan (dfs 或 状压dp,贪心不行)
题干:I like playing game with my friend, although sometimes looks pretty naive. Today I invent a new game called LianLianKan. The game is about playing on a number stack. Now we have a number stack, ...原创 2019-02-18 18:10:50 · 333 阅读 · 1 评论 -
【2019牛客暑期多校训练营(第二场)- F】Partition problem(dfs,均摊时间优化)
题干:链接:https://ac.nowcoder.com/acm/contest/882/F来源:牛客网Given 2N people, you need to assign each of them into either red team or white team such that each team consists of exactly N people and the...原创 2019-08-21 14:16:36 · 238 阅读 · 0 评论 -
*【ZOJ - 3781】Paint the Grid Reloaded(dfs求连通块缩点,bfs求最短路,建图技巧)
题干:Leo has a grid withNrows andMcolumns. All cells are painted with either black or white initially.Two cellsAandBare calledconnectedif they share an edge and they are in the same color,...原创 2019-04-24 20:18:27 · 260 阅读 · 0 评论 -
【CodeForces - 616C 】The Labyrinth点石成金(并查集,dfs)
题干:小O无意间发现了一张藏宝图,它跟随藏宝图的指引来到了一个宫殿,宫殿的地板被分成了n*m块格子,每个格子上放置了金子或者石头藏宝图告诉小O,它可以选择一块石头变成金子,并且带走与变化后的金子联通区域的所有金子(联通指的是上下左右,不能斜着)小O想计算一下点每个石头能带走的金子个数,帮帮他吧。输入:第一行两个数n,m (1 <= n,m <= 1000 )随后n行...原创 2019-03-18 16:49:10 · 365 阅读 · 0 评论 -
【牛客 - 157B】凤凰(树上并查集,dfs)
题干:链接:https://ac.nowcoder.com/acm/contest/157/B来源:牛客网题目描述传说,凤凰是百鸟之王。有一天,凤凰要召开百鸟大会,百鸟国是一个由n个节点组成的树,每个节点有一只鸟,开会的节点定在1号节点。每只鸟可以花费1s通过一条边,由于每根树枝(边)的载重有限,只允许一只鸟同时通过。作为会议的策划师,HtBest想知道百鸟国的所有鸟在1点集...原创 2019-03-20 13:40:15 · 390 阅读 · 0 评论 -
【牛客 - 157D】插排树(dfs,树形dp)
题干:链接:https://ac.nowcoder.com/acm/contest/157/D来源:牛客网一年一度的山东省oi夏令营又开始了,每到这个季节,山东的oier们都会欢聚这里,一起学(tuí)习(feì)。当然,为了能更加愉快地学(tuí)习(feì),就少不了要自带电脑,用电便开始成了一种问题,于是便有一种神奇的数据结构诞生了!这就是山东省oi专用数据结构——插排树(如图)...原创 2019-04-08 10:57:27 · 313 阅读 · 0 评论 -
【蓝桥杯官网试题 - 历届试题】发现环(dfs+并查集,或无向图tarjan判环,无向环,或拓扑排序)
题干:问题描述 小明的实验室有N台电脑,编号1~N。原本这N台电脑之间有N-1条数据链接相连,恰好构成一个树形网络。在树形网络上,任意两台电脑之间有唯一的路径相连。 不过在最近一次维护网络时,管理员误操作使得某两台电脑之间增加了一条数据链接,于是网络中出现了环路。环路上的电脑由于两两之间不再是只有一条路径,使得这些电脑上的数据传输出现了BUG。 为了恢复正常传输。小明需要找到所有...原创 2019-03-13 22:02:11 · 521 阅读 · 0 评论 -
【POJ - 1724 】ROADS (带限制的最短路 或 dfs 或 A*算法,双权值)
题干:N cities named with numbers 1 ... N are connected with one-way roads. Each road has two parameters associated with it : the road length and the toll that needs to be paid for the road (expressed ...原创 2019-02-14 23:51:22 · 599 阅读 · 0 评论 -
*【洛谷 - P1025】数的划分(dfs 或 dp 或 母函数,第二类斯特林数Stirling)
题干:题目描述将整数n分成k份,且每份不能为空,任意两个方案不相同(不考虑顺序)。例如:n=7,k=3,下面三种分法被认为是相同的。1,1,51,5,15,1,1问有多少种不同的分法。输入输出格式输入格式:n,kn,k(6<n \le 2006<n≤200,2 \le k \le 62≤k≤6)输出格式:11个整数,即不同...原创 2019-03-01 13:38:58 · 416 阅读 · 0 评论 -
【 HDU - 1796】How many integers can you find (容斥原理,二进制枚举或者dfs)
题干: Now you get a number N, and a M-integers set, you should find out how many integers which are small than N, that they can divided exactly by any integers in the set. For example, N=12, and M-in...原创 2018-10-31 22:03:36 · 247 阅读 · 0 评论 -
【HihoCoder - 1268】九宫 (dfs,深搜)
题干:小Hi最近在教邻居家的小朋友小学奥数,而最近正好讲述到了三阶幻方这个部分,三阶幻方指的是将1~9不重复的填入一个3*3的矩阵当中,使得每一行、每一列和每一条对角线的和都是相同的。三阶幻方又被称作九宫格,在小学奥数里有一句非常有名的口诀:“二四为肩,六八为足,左三右七,戴九履一,五居其中”,通过这样的一句口诀就能够非常完美的构造出一个九宫格来。有意思的是,所有的三阶幻方,都可以...原创 2018-09-01 13:55:57 · 335 阅读 · 0 评论 -
【POJ - 2676】Sudoku (经典深搜,dfs数独)
题干:Sudoku is a very simple task. A square table with 9 rows and 9 columns is divided to 9 smaller squares 3x3 as shown on the Figure. In some of the cells are written decimal digits from 1 to 9. The...原创 2018-09-01 13:40:15 · 732 阅读 · 0 评论 -
*【HDU - 1242 】 Rescue (反向dfs,或bfs)
题干:Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison. Angel's friends wa...原创 2018-08-29 21:18:41 · 343 阅读 · 0 评论 -
【 POJ - 3628 】Bookshelf 2(dfs 或 dp,0-1背包)
题干:Farmer John recently bought another bookshelf for the cow library, but the shelf is getting filled up quite quickly, and now the only available space is at the top.FJ hasNcows (1 ≤N≤ 20) ea...原创 2019-05-09 21:53:34 · 296 阅读 · 0 评论 -
【Codeforces - 378C】Maze(dfs,思维)
题干:Pavel loves grid mazes. A grid maze is ann × mrectangle maze where each cell is either empty, or is a wall. You can go from one cell to another only if both cells are empty and have a common si...原创 2018-08-30 22:36:03 · 653 阅读 · 0 评论 -
【POJ - 3310】Caterpillar(并查集判树+树的直径求树脊椎(bfs记录路径)+dfs判支链)
题干:An undirected graph is called a caterpillar if it is connected, has no cycles, and there is a path in the graph where every node is either on this path or a neighbor of a node on the path. This p...原创 2018-08-02 15:32:16 · 491 阅读 · 0 评论 -
【HDU - 1241】Oil Deposits (连通块问题 属于求大海中的岛屿个数 类似问题)
题干: The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that di...原创 2018-07-15 20:14:36 · 493 阅读 · 0 评论 -
【51NOD - 1523】 非回文(dfs)
题干:一个字符串是非回文的,当且仅当,他只由前p个小写字母构成,而且他不包含长度大于等于2的回文子串。给出长度为n的非回文串s。请找出字典序比s大的,而且字典序要最小的长度为n的非回文。Input单组测试数据。 第一行有两个整数n 和p (1≤n≤1000; 1≤p≤26)。 第二行包含一个字符串s,它的长度是n。输入保证他是非回文的。Output输出字典序比s大的且字典序要最小的长度为n的非回文...原创 2018-07-14 22:45:32 · 1318 阅读 · 0 评论 -
【HDU - 1269】迷宫城堡 (tarjan算法模板)
题干:为了训练小希的方向感,Gardon建立了一座大城堡,里面有N个房间(N<=10000)和M条通道(M<=100000),每个通道都是单向的,就是说若称某通道连通了A房间和B房间,只说明可以通过这个通道由A房间到达B房间,但并不说明通过它可以由B房间到达A房间。Gardon需要请你写个程序确认一下是否任意两个房间都是相互连通的,即:对于任意的i和j,至少存在一条路径可以从房间i...原创 2018-09-11 14:46:18 · 912 阅读 · 0 评论 -
【HDU - 1518】Square (经典的dfs + 剪枝)
题干:Given a set of sticks of various lengths, is it possible to join them end-to-end to form a square? InputThe first line of input contains N, the number of test cases. Each test case begins wit...原创 2018-09-11 16:22:45 · 434 阅读 · 0 评论 -
【51Nod - 1416】两点 (dfs 或 并查集+dfs)
题干:福克斯在玩一款手机解迷游戏,这个游戏叫做”两点”。基础级别的时候是在一个n×m单元上玩的。像这样: 每一个单元有包含一个有色点。我们将用不同的大写字母来表示不同的颜色。这个游戏的关键是要找出一个包含同一颜色的环。看上图中4个蓝点,形成了一个环。一般的,我们将一个序列 d1,d2,...,dk 看成一个环,当且仅当它符合下列条件时:1. 这k个点不一样,即当 i≠j...原创 2018-10-17 19:18:03 · 270 阅读 · 0 评论 -
【51Nod - 1268】和为K的组合 (背包 或 dfs)
题干:给出N个正整数组成的数组A,求能否从中选出若干个,使他们的和为K。如果可以,输出:"Yes",否则输出"No"。Input第1行:2个数N, K, N为数组的长度, K为需要判断的和(2 <= N <= 20,1 <= K <= 10^9) 第2 - N + 1行:每行1个数,对应数组的元素Aii (1 <= Aii <= 10^6)Ou...原创 2018-10-17 19:16:31 · 391 阅读 · 0 评论 -
【HDU - 1045】Fire Net (dfs 或二分图)
题干:Suppose that we have a square city with straight streets. A map of a city is a square board with n rows and n columns, each representing a street or a piece of wall. A blockhouse is a small cast...原创 2018-10-17 00:54:35 · 244 阅读 · 0 评论 -
【PAT - 甲级 - 1018】Public Bike Management (带权最短路,多条最短路中加条件,DFS)
题干:链接:https://www.nowcoder.com/questionTerminal/4b20ed271e864f06ab77a984e71c090f来源:牛客网There is a public bike service in Hangzhou City which provides great convenience to the tourists from all over...原创 2018-10-10 16:04:19 · 465 阅读 · 0 评论 -
*【CodeForces - 791B】Bear and Friendship Condition (图论,判断完全图,dfs乱搞或带权并查集)
题干:Bear Limak examines a social network. Its main functionality is that two members can become friends (then they can talk with each other and share funny pictures).There arenmembers, numbered1...原创 2018-09-15 15:53:24 · 317 阅读 · 0 评论 -
【POJ - 1562】Oil Deposits (dfs搜索,连通块问题)
题干:The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that divid...原创 2018-09-07 14:17:24 · 238 阅读 · 0 评论 -
【HDU - 1455】Sticks (dfs + 剪枝)
题干:George took sticks of the same length and cut them randomly until all parts became at most 50 units long. Now he wants to return sticks to the original state, but he forgot how many sticks he had...原创 2018-09-12 09:42:31 · 247 阅读 · 0 评论 -
【HDU - 2553】N皇后问题 (dfs经典问题,回溯,搜索)
题干:在N*N的方格棋盘放置了N个皇后,使得它们不相互攻击(即任意2个皇后不允许处在同一排,同一列,也不允许处在与棋盘边框成45角的斜线上。 你的任务是,对于给定的N,求出有多少种合法的放置方法。 Input共有若干行,每行一个正整数N≤10,表示棋盘和皇后的数量;如果N=0,表示结束。Output共有若干行,每行一个正整数,表示对应输入行的皇后的不同放置数量。Sam...原创 2018-09-05 15:09:55 · 244 阅读 · 0 评论 -
素数环(dfs&&STL做法)HDU - 1016
这题第一思路显然dfs,但是最近想了一下可以用next_permutation做,结果TLE了,究其原因,就是深搜的时候剪枝做的比较好,所以运行的时间一对比(样例是n=18),会发现dfs的解法程序在不停的输出数据,但是STL的却仅仅是在闪光标。 得结论:有的题剪枝的十分优雅(比如这题,如果1 2 判断不成立,那么第二个数放2这一大支直接剪掉了),便不能用next_permutati...原创 2018-06-07 22:22:39 · 264 阅读 · 0 评论