dfs
不可知论大祭司
Know how to solve every problem that has been solved.
展开
-
全排列的简单实现 java 回溯
介绍从1到n的全排列,我们可以开一个vis数组表示这个数字是否在当前的排列方式种,回溯算法的全排列实际可以看作深度优先搜索codepublic class Test { static boolean[] vis = new boolean[100]; public static void getPermutation(int n, int steps, List<Integer> path, List<List<Integer>> res){原创 2021-01-04 22:25:42 · 285 阅读 · 0 评论 -
【CF1056D】Decorate Apple Tree
链接传送门思路统计每根节点叶子节点的个数,然后排序代码#include <iostream>#include <algorithm>#include <cstring>using namespace std;const int N = 1e5 + 10;int n, cnt;int head[N], leaf[N];struct edge{ int to, nxt;}e[N];inline void add(int u, int原创 2020-12-27 19:39:50 · 233 阅读 · 0 评论 -
【CF682C】Alyona and the Tree
Alyona and the Tree链接分析代码链接传送门分析一开始是打算统计每个sad节点的子树大小,然后在dfs的过程中不断更新答案和子树的大小,后来发现根本不用这么麻烦,直接统计不sad的节点即可,注意在dfs的过程中更新dis。代码#include <iostream>#include <cstdio>#include <algorithm>using namespace std;typedef long long ll;const i原创 2020-12-14 08:24:01 · 212 阅读 · 0 评论 -
【CF115A】Party
题目概述A company has n employees numbered from 1 to n. Each employee either has no immediate manager or exactly one immediate manager, who is another employee with a different number. An employee A is said to be the superior of another employee B if at least原创 2020-12-12 21:45:07 · 283 阅读 · 0 评论 -
【CF161D】Distance in Tree (dfs + 动态规划浅析)
Distance in Tree题目描述题目链接分析代码图解题目描述A tree is a connected graph that doesn’t contain any cycles.The distance between two vertices of a tree is the length (in edges) of the shortest path between these vertices.You are given a tree with n vertices and a po原创 2020-12-10 21:15:07 · 363 阅读 · 0 评论 -
【CF106E】Military Problem
Military Problem题目概述题目链接分析代码题目概述一棵树,给出q个query每个query(u, k)表示从u开始的第k个dfs的节点,优先dfs编号小的节点。如果不存在的话输出-1。题目链接传送门分析我们可以用set存边以保持顺序。询问最大的数量级为e5, 如果每次都dfs一下的话很显然会超时。我们选择记录dfs序和它的反函数pre还有子树的大小,和树剖里面的一样。根据子树大小和判断是否存在第k个dfs的点,如果存在根据pre和dfn确定答案。dfsshijain复杂度O(n原创 2020-12-10 07:28:05 · 192 阅读 · 0 评论 -
【CF690C2】Brain Network (medium)树的直径
Brain Network medium题目大意题目链接分析代码题目大意Further research on zombie thought processes yielded interesting results. As we know from the previous problem, the nervous system of a zombie consists of n brains and m brain connectors joining some pairs of brains to原创 2020-12-09 17:19:38 · 168 阅读 · 0 评论 -
【CF580C】Kefa and Park dfs
Kefa and Park dfs题目大意题目链接代码题目大意Kefa decided to celebrate his first big salary by going to the restaurant.He lives by an unusual park. The park is a rooted tree consisting of n vertices with the root at vertex 1. Vertex 1 also contains Kefa’s house. Unfo原创 2020-12-09 16:54:35 · 152 阅读 · 0 评论 -
【CF982C】Cut ‘em all!【dfs】
Cut 'em all!题目链接解法代码题目链接传送门解法直接dfs统计子树大小的奇偶性就行了,注意如果一开始n个节点为奇数的话是不可分的,直接上代码。代码#include <iostream>#include <cstdio>//size关键字using namespace std;const int N = 1e5 + 10;int head[N], size[N];int n, cnt, ans;struct edge{ int to, n原创 2020-12-09 10:51:51 · 121 阅读 · 0 评论 -
【CF839C】journey(树上dfs求期望)
Journey题目大意题目链接分析错误示范AC代码题目大意There are n cities and n - 1 roads in the Seven Kingdoms, each road connects two cities and we can reach any city from any other by the roads.Theon and Yara Greyjoy are on a horse in the first city, they are starting traveli原创 2020-12-08 15:18:31 · 302 阅读 · 0 评论