CodeForces - 124B-Permutations(DFS)】的更多相关文章

题目传送门 /* 贪心:全排列函数使用,更新最值 */ #include <cstdio> #include <algorithm> #include <cstring> #include <cmath> #include <string> #include <vector> #include <map> #include <set> #include <iostream> #include <…
http://codeforces.com/problemset/problem/124/B Description You are given nk-digit integers. You have to rearrange the digits in the integers so that the difference between the largest and the smallest number was minimum. Digits should be rearranged b…
题目链接:http://codeforces.com/contest/734/problem/E 题意:有一棵黑白树,每次操作可以使一个同色连通块变色,问最少几次操作能使树变成全黑或全白. 思路:先进行缩点,同色连通块当作一点,用dfs实现并得到新图.答案即为(最长直径+1)/2. 关于最长直径的求法:http://www.cnblogs.com/wuyiqi/archive/2012/04/08/2437424.html #include<bits/stdc++.h> using names…
题目链接:http://codeforces.com/contest/731/problem/C 题意:有n只袜子(1~n),k种颜色(1~k),在m天中,左脚穿下标为l,右脚穿下标为r的袜子,问最少修改几只袜子的颜色,可以使每天穿的袜子左右两只都同颜色. 好恶心的袜子,一会儿看成改袜子的颜色,一会儿看成改l,r的颜色,一会下标看混......不过,菜是原罪=_= 思路:先建图,在每个连通分支中,把所有袜子的颜色都改成出现颜色次数最多的那个颜色,即该分支节点个数 - 最多出现次数 #includ…
题目链接:http://codeforces.com/problemset/problem/723/D 题意:n*m的矩阵中,'*'代表陆地,'.'代表水,连在一起且不沿海的水形成湖泊.问最少填多少块water能使湖泊数量降到k个. 思路:本来最有把握的一次CF,D题小错误一直RE,C题最后也FST了...... 先DFS出各湖泊的大小并用其中一个点存在结构体中,最后有num0个湖泊,再按湖泊的大小排序,需要填的湖泊为前n-k小的湖泊,简单DFS一下就好了. #include<bits/stdc…
J - Military Problem CodeForces - 1006E 就是一道dfs序的问题 给定一个树, 然后有q次询问. 每次给出u,k, 求以u为根的子树经过深搜的第k个儿子,如果一个节点有多个儿子,按照儿子从小到大的顺序,依次访问,不存在则输出-1. 预处理记录一下每个节点的出入时间 最后每次query直接判断就好了 #include <cstdio> #include <algorithm> #include <vector> using names…
题目链接:http://codeforces.com/problemset/problem/95/B 题目大意:给你一个正整数n (1 ≤ n ≤ 10100000),求不大小于它的超级幸运数字(超级幸运数字就是只含有数字4和数字7,并且4和7的数目相同) 例: Sample Input 4500 47 Sampule Output 4747 47 解题思路:压根没想到用DFS,开始就想着,先判断数字的个数是否是个奇数,如果是个奇数就很好办,直接加一位数字,前一半是4后一半是7,当它的数字个数为…
题目链接:http://codeforces.com/problemset/problem/589/J 题目大意:一个机器人打扫一个密闭的房间,房间由一个矩形构成,'*'表示家具,'.'表示该位置为空,机器人只能扫空的位置,给出机器人的初位置及朝向,如果当前朝向不能再继续往前走了,机器人就会向右转,问一共能清理多少地方. 例: 输入: 2 3U...*. 输出: 4 解题思路:方法应该有很多种,不过我用的DFS.需要注意的就是,机器人的朝向,只有当前方有障碍时,他才会右转.还有就是DFS的结束的…
题目链接:https://codeforces.com/problemset/problem/1099/F Mitya and Vasya are playing an interesting game. They have a rooted tree with $n$ vertices, and the vertices are indexed from $1$ to $n$. The root has index $1$. Every other vertex $i≥2$ has its p…
Persistent Bookcase CodeForces - 707D time limit per test 2 seconds memory limit per test 512 megabytes input standard input output standard output Recently in school Alina has learned what are the persistent data structures: they are data structures…