URAL 1136 Parliament (DFS)】的更多相关文章

题意 输入一棵树的后缀表达式(按左-右-中顺序访问),这棵树的每一个结点的数值都比它的左子树结点的数值大,而比它的右子树结点的数值小,要求输出其按右-左-中顺序访问的表达式.所有的数都为正整数,而且不会重复. 思路 很像根据中缀和后缀表达式求前缀表达式之类的题.方法自然也差不多.由DFS的括号性质可知,每一个树都对应表达式的一个区间,而此题中区间的最后一个就是树的根,然后根据根的大小可以把区间分为值小于和大于根值的两部分,即左子树和右子树,然后递归地输出右子树.左子树,最后再输出根即可. 代码…
题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1136 题目描述:给定一个按照(左子树-右子树-根)(即先序)遍历序列的树,求其按照 右子树-左子树-根 遍历的结果.(每个数都不同) 题目思路:按照题目意思其实构造的是一个二叉查找树,满足左子树元素都不大于当前根的元素,右子树元素都不小于当前根的元素. 而且二叉查找树按照 中序遍历 的结果是元素按照递增顺序输出(二叉查找树的性质).所以实际上又告诉了你中序遍历的结果(即把所给元素递增排序的结果…
二叉树水题,特别是昨天刚做完二叉树用中序后序建树,现在来做这个很快的. 跟昨天那题差不多,BST后序遍历的特型,找到最后那个数就是根,向前找,比它小的那块就是他的左儿子,比它大的那块就是右儿子,然后递归儿子继续建树. 代码: #include <cstdio> #include <cstdlib> const int maxn = 70000; struct Node { int v; Node *l; Node *r; }; int arr[maxn]; bool flag =…
URAL.1033 Labyrinth (DFS) 题意分析 WA了好几发,其实是个简单地DFS.意外发现这个俄国OJ,然后发现ACRUSH把这个OJ刷穿了. 代码总览 #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <sstream> #include <set> #include <map> #…
Parliament Time limit: 1.0 secondMemory limit: 64 MB A new parliament is elected in the state of MMMM. Each member of the parliament gets his unique positive integer identification number during the parliament registration. The numbers were given in…
Parliament Time limit: 1.0 secondMemory limit: 64 MB A new parliament is elected in the state of MMMM. Each member of the parliament gets his unique positive integer identification number during the parliament registration. The numbers were given in…
1136 先由后左 再父 建一个二叉树 #include <iostream> #include<cstdio> #include<cstring> #include<stdlib.h> #include<algorithm> using namespace std; #define N 3010 int a[N]; int tr[N]; int g; void build(int s,int te,int ro) { if(s==te) { g…
题目传送门 /* 记忆化搜索(DP+DFS):dp[i][j] 表示第i到第j个字符,最少要加多少个括号 dp[x][x] = 1 一定要加一个括号:dp[x][y] = 0, x > y; 当s[x] 与 s[y] 匹配,则搜索 (x+1, y-1); 否则在x~y-1枚举找到相匹配的括号,更新最小值 */ #include <cstdio> #include <algorithm> #include <cmath> #include <iostream&…
Legendary Teams Contest Time limit: 1.0 secondMemory limit: 64 MB Nothing makes as old as years. A lot of cool contests are gone, a lot of programmers are not students anymore and are not allowed to take part at the contests. Though their spirit is f…
Z - Bus Routes Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice URAL 1137 Description Several bus routes were in the city of Fishburg. None of the routes shared the same section of road, though commo…
题目链接: URAL 1890 . Money out of Thin Air 题目描述: 给出一个公司里面上司和下级的附属关系,还有每一个人的工资,然后有两种询问: 1:employee x y z ,如果编号为x的员工如果工资小于y,就给他加薪z. 2:department x y z ,如果编号为x的员工所管辖的范围内(包括自己),所有员工的工资平均数小于y,给该范围加薪z. 问q次操作后这个公司内每个员工的工资为多少? 解题思路: 根据上司和下级的附属关系,可以先建一个有向图,然后对有向…
题目传送门 /* 记忆化搜索(DFS+DP):dp[x][y] 表示x个蛋,在y楼扔后所需要的实验次数 ans = min (ans, max (dp[x][y-i], dp[x-1][i-1]) + 1):前者表示蛋没碎,则往高处(y-i)搜索 后者表示蛋碎了,往低处(i-1)方向搜索 这样写不好,每次memset (dp)就会超时:( 详细解释:http://blog.csdn.net/fulongxu/article/details/27110435 */ #include <cstdio…
题目传送门 /* 题意:一个圈,每个点有怪兽,每一次射击能消灭它左右和自己,剩余的每只怪兽攻击 搜索水题:sum记录剩余的攻击总和,tot记录承受的伤害,当伤害超过ans时,结束,算是剪枝吧 回溯写挫了,程序死循环,跑不出来.等回溯原理搞清楚了,下次自己重写一遍:) */ #include <cstdio> #include <algorithm> #include <cstring> #include <cmath> #include <iostre…
题目传送门 /* 题意:给了两堆牌,每次从首部取出一张牌,按颜色分配到两个新堆,分配过程两新堆的总数差不大于1 记忆化搜索(DFS+DP):我们思考如果我们将连续的两个操作看成一个集体操作,那么这个操作必然是1红1黑 考虑三种情况:a[]连续两个颜色相同,输出11:b[]连续两个相同,输出22: a[x] != b[y], 输出12:否则Impossible 详细解释:http://blog.csdn.net/jsun_moon/article/details/10254417 */ #incl…
题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1106 乍一眼看上去,好像二分图匹配,哎,想不出和哪一种匹配类似,到网上查了一下,DFS染色一遍就可以啦. 两种颜色的很好写.直接没有访问的是1,然后扫邻接表,为2,DFS邻接表. #include <bits/stdc++.h> using namespace std; #define maxn 105 vector<int> G[maxn]; int color[max…
Werewolf Time limit: 1.0 secondMemory limit: 64 MB Knife. Moonlit night. Rotten stump with a short black-handled knife in it. Those who know will understand. Disaster in the village. Werewolf. There are no so many residents in the village. Many of th…
题目:click here :这个题可以先dfs深搜下,规律dp dfs: #include <bits/stdc++.h> using namespace std; #define S second typedef long long ll; const int INF = 0x3f3f3f3f; ; int s; ; ]; void dfs( int last, int num ) { if( num == s ) { cnt++; return ; } ; i<s+; i++ )…
J - 皇后像廣場 题目连接: http://acm.hust.edu.cn/vjudge/contest/123332#problem/J Description Vova was walking along the Statue Square (皇后像廣場) in Hong Kong, when he noticed that the square was paved with multi-colored square tiles. Vova took a careful look at t…
http://acm.timus.ru/problem.aspx?space=1&num=1557 1557. Network Attack Time limit: 2.0 secondMemory limit: 64 MB In some computer company, Mouse Inc., there is very complicated network structure. There are a lot of branches in different countries, so…
大概就是dfs?当前区间(l,r)的答案可以由(l,m)和(m+1,r)区间推出,如果某个区间已经完全被某种颜色覆盖,那么就返回该颜色.否则按照递归层数判定,奇数层Alice占优势,只需左右区间中一者为必胜即可,而Bob需要左右区间均为其必胜色才可以.无解判一下即可.感觉还是很巧妙. There is a strip 1 × n with two sides. Each square of the strip (their total amount is 2 n, n squares on ea…
1500. Pass Licenses Time limit: 2.5 secondMemory limit: 64 MB A New Russian Kolyan believes that to spend his time in traffic jams is below his dignity. This is why he had put an emergency flashlight upon the roof of his Hummer and had no problems un…
题意 小组里有N个人,每个人都有一个或多个朋友在小组里.将小组分成两个队伍,每个队伍的任意一个成员都有至少一个朋友在另一个队伍. 思路 一开始觉得和前几天做过的一道2-sat(每个队伍任意两个成员都必须互相认识)相似然后就往那边想了--看了题解才发现这题很简单-- 我们注意到同组里的人是互不影响的,所以一个人如果已经确定在哪组的话是不会对后面进入这个组的人产生影响的. 所以我们按顺序直接搜就可以了,每个人在他朋友里面找一个已经确定组的,然后放到对面组.如果他的所有朋友都没确定,把他随便放一个组就…
1073. Square Country Time limit: 1.0 secondMemory limit: 64 MB There live square people in a square country. Everything in this country is square also. Thus, the Square Parliament has passed a law about a land. According to the law each citizen of th…
题目链接 以前做过的一题,URAL数据强点,优化了一下. #include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <map> #include <ctime> #include <cmath> #include <algorithm> using namespace std; ][]; int dfs(…
题目传送门 /* 题意:就是从上到下,找到最短路,输出路径 DP+路径:状态转移方程:dp[i][j] = min (dp[i-1][j], dp[i][j-1], dp[i][j+1]) + a[[i][j]; (类似数塔问题) 关键在记录路径,可以用pre[x][y] = -1/0/1/2 区分,DFS回溯输出 详细解释:http://www.cnblogs.com/staginner/archive/2012/05/02/2479658.html */ #include <cstdio>…
题目传送门 /* 题意:已知丢失若干卡片后剩余的总体积,并知道原来所有卡片的各自的体积,问丢失的卡片的id DP递推:首先从丢失的卡片的总体积考虑,dp[i] 代表体积为i的方案数,从dp[0] = 1递推,累加的条件是dp[j]上一个状态已经有方案, 若dp[w] > 1表示有多种方案,外加p[j+a[i]] = i的记录路径数组 我开始用了暴力DFS超时,dp不会写,看了题解发现是个递推,有点像01背包的题目:) 详细解释:http://blog.csdn.net/neko01/articl…
题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=17662 题目大意:树枝上间连接着一坨坨苹果(不要在意'坨'),给定留下m根树枝,问最后剩下的最多苹果是多少. 解题思路: 其实意思和Vijos 1180(选课)的意思差不多.只不过权在边而已. 首先建无向图dfs. for(f+1...j....cost) for(1....k...j-cost) 其中f为当前已经dfs子结点个数.之所以+1,是因为当前点也需要…
NYOJ 239:http://acm.nyist.net/JudgeOnline/problem.php?pid=239 ural 1109 :http://acm.timus.ru/problem.aspx?space=1&num=1109 NYOJ 月老的难题,是裸的最大匹配,很烦的是邻接阵超时.改用邻接表. #include <bits/stdc++.h> using namespace std; #define maxn 1005 vector <int> G[m…
C. Valera and Elections   The city Valera lives in is going to hold elections to the city Parliament. The city has n districts and n - 1 bidirectional roads. We know that from any district there is a path along the roads to any other district. Let's…
邱老师玩游戏 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/problem/show/1136 Description 邱老师最近在玩一种战略游戏,在一个地图上,有N座城堡,每座城堡都有一定的宝物,在每次游戏中邱老师允许攻克M个城堡并获得里面的宝物. 但由于地理位置原因,有些城堡不能直接攻克,要攻克这些城堡必须先攻克其他某一个特定的城堡.你能帮邱老师算出要获得尽量多的宝物应该攻克哪M个城堡吗? Inp…