CodeForces 982 C Cut 'em all!】的更多相关文章

Cut 'em all! 题意:求删除了边之后,剩下的每一块联通块他的点数都为偶数,求删除的边最多能是多少. 题解:如果n为奇数,直接返回-1,因为不可能成立.如果n为偶数,随意找一个点DFS建树记录下他的子孙+本身的个数.然后再DFS一下,对于每一个点,他的个数为偶数,就把他与父节点的边隔断, cnt++. 最后cnt就是答案. 代码: #include<bits/stdc++.h> using namespace std; #define Fopen freopen("_in.t…
解题思路: 代码中有详细注解,以任意一点为根,dfs遍历这棵树. 每一个节点可能有好几个子树,计算每棵子树含有的节点数,再+1即为这整棵树的节点. 判断子树是否能切断与根之间的联系,如果子树含有偶数个节点,则这棵子树可以被切断. 注意: 若由于我们建立这棵树的时候不知道两个连接的节点谁是谁的父节点. 所以我们在dfs中加个标记,找出除父节点以外的其他节点. #include <bits/stdc++.h> using namespace std; typedef long long ll; l…
K - Cut 'em all! CodeForces - 982C 给一棵树 求最多能切几条边使剩下的子树都有偶数个节点 如果n是奇数 那么奇数=偶数+奇数 不管怎么切 都会有奇数 直接打印-1 贪心 如果子树中的点是偶数 就可以直接切了 最后答案要减一 因为原来的树也是偶数个节点 会被统计进去 #include <cstdio> #include <vector> #include <algorithm> using namespace std; ; int res…
 Cut 'em all! time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output You're given a tree with nn vertices. Your task is to determine the maximum possible number of edges that can be removed in suc…
A /*Huyyt*/ #include<bits/stdc++.h> #define mem(a,b) memset(a,b,sizeof(a)) #define pb push_back using namespace std; typedef long long ll; typedef unsigned long long ull; const ll LLmaxn = 2e18; ; inline int readint() { char c = getchar(); ; ') { c…
题意:给你一棵树,让你切掉尽可能多的边,使得产生的所有连通块都有偶数个结点. 对于一棵子树,如果它有奇数个结点,你再从里面怎么抠掉偶数结点的连通块,它都不会变得合法.如果它本来就有偶数个结点,那么你怎么抠,都是合法的. 所以,我们只需要切断所有有偶数结点的子树的父边即可. 然后再判一遍最后是否仍是合法的. #include<cstdio> #include<cstring> using namespace std; int n; int e,first[100005],nex[20…
题目链接 题意:给你一棵树,让你尽可能删除多的边使得剩余所有的联通组件都是偶数大小. 思路:考虑dfs,从1出发,若当前节点的子节点和自己的数目是偶数,说明当前节点和父亲节点的边是可以删除的,答案+1,因为最开始的节点没有父节点,所以最后答案-1 #include<bits/stdc++.h> #define LL long long #define fi first #define se second #define mp make_pair #define pb push_back usi…
题目链接: https://cn.vjudge.net/problem/1576783/origin 输入输出: ExamplesinputCopy42 44 13 1outputCopy1inputCopy31 21 3outputCopy-1inputCopy107 18 48 104 76 59 33 52 102 5outputCopy4inputCopy21 2outputCopy0NoteIn the first example you can remove the edge bet…
题意: 给出一棵树,问最多去掉多少条边之后,剩下的连通分量的size都是偶数. 思路: 如果本来就是奇数个点,那么无论去掉多少条边都不可能成立的. 如果是偶数个点,就进行一次dfs,假设一个点的父亲是u,儿子是v,那么可以去掉(u,v)的条件就是v及其子树有偶数个点,任何一条这样的边都是可以去掉的. 所以一边dfs,一边统计答案就可以了. 代码: #include <stdio.h> #include <string.h> #include <algorithm> #i…
Word Cut 题目连接: http://codeforces.com/problemset/problem/176/C Description Let's consider one interesting word game. In this game you should transform one word into another through special operations. Let's say we have word w, let's split this word in…
Word Cut Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 176B Description Let's consider one interesting word game. In this game you should transform one word into another through specia…
题目链接:http://codeforces.com/problemset/problem/883/H Time limit: 3000 ms Memory limit: 262144 kB Kolya has a string s of length n consisting of lowercase and uppercase Latin letters and digits. He wants to rearrange the symbols in s and cut it into th…
B. Word Cut   Let's consider one interesting word game. In this game you should transform one word into another through special operations. Let's say we have word w, let's split this word into two non-empty parts x and y so, that w = xy. A split oper…
http://codeforces.com/gym/101246/problem/C 题意:给出一个n*m的图,“*”表示这个地方需要炸掉,炸弹可以如果丢在(i,j)位置的话,那么可以炸掉第i行第j列的所有“*”.问最少需要丢多少个炸弹可以使得所有“*”被炸掉. 思路:一看就以为是个最小顶点覆盖.然后发现做不了... 枚举行的状态i,1表示这一行不炸,0表示炸了这一行. 然后递推. 这里用bitset维护行的状态. f[i][j]表示第i行j列是否有“*”. dp[i]表示不炸的行状态有哪些列是…
time limit per test : 1 second memory limit per test : 256 megabytes input : standard input output : standard output Polycarpus has a ribbon, its length is nnn. He wants to cut the ribbon in a way that fulfils the following two conditions: After the…
#include<cstdio> #include<cstring> #include<cmath> #include<stack> #include<vector> #include<string> #include<iostream> #include<algorithm> using namespace std; long long L,R,K; long long POW(long long m,lon…
Description 题库链接 给你两个字符串 \(S\) 和 \(T\) ,准许你 \(k\) 次操作,每次将字符串左右分成两个非空的部分,再交换位置,问你有多少种不同的操作方法将 \(S\) 串变为 \(T\) 串. \(1\leq k\leq 100000, 1\leq |S|=|T|\leq 1000\) Solution 容易发现不论经过多少次操作,其操作后的字符串一定是在原字符串上截开两段再拼接而成. 所以不妨记 \(f_{i,j}\) 为操作 \(i\) 次后在 \(j\) 处截…
给你一棵树 让你进行切割 问你最多能切多少刀   使得每个连通分量size都是偶数 思路:首先  要是有奇数个节点的话   那么不管你怎么切割  都会有一个连通分量的size是奇数 所以只有偶数的情况才可能进行切割 切割的话  只要切割size为偶数的节点就行    把size为偶数的节点和他的父节点切开   就能保证连通分量的size为偶数 dfs一下就过了 #include <iostream> #include <cstdio> #include <cstring>…
[链接]:CF982C [题意]:有一颗树,你需要切掉一些边,使这颗树分拆成若干个节点为偶数的联通分量,最多能切掉几条边.若不能切,输出-1. [分析]: 1.若点数n为奇数,因为奇数不可能分为偶数,那么一定输出-1 2.若点数n为偶数,偶数=偶数+偶数.就从顶点1开始,当作父顶点开始dfs.dfs用于计算子树的顶点数,如果子树是偶数个顶点,那么ans就可以++,然后把该子树标记成搜索过的,最后的答案要-1:因为整棵树肯定是偶数顶点,ans也会+1: [代码]: [不用vis数组] #inclu…
Description Let's consider one interesting word game. In this game you should transform one word into another through special operations. Let's say we have word w, let's split this word into two non-empty parts x and y so, that w = xy. A split operat…
思路: 在深搜过程中,贪心地把树划分成若干个连通分支就可以了.划分的条件是某个子树有偶数个节点.注意到在一次划分之后并不需要重新计数,因为一个数加上一个偶数并不影响这个数的奇偶性. 实现: #include <bits/stdc++.h> using namespace std; ; vector<int> G[MAXN]; bool vis[MAXN]; ; int dfs(int u) { vis[u] = true; ; ; i < G[u].size(); i++)…
解题思路: 排序之后模拟一个栈(也可以用真的栈),时间复杂度o(n). 代码: #include <bits/stdc++.h> using namespace std; typedef long long ll; struct node{ int val;int idx; }w[]; bool cmp(node x, node y){ return x.val < y.val; } ]; stack <node> s; int main(){ int n; scanf(&q…
Shark 题意:一个研究员观察了一条鲨鱼n天的运动,然后这条鲨鱼他只会往前走,不会回到去过的地方,现在有一个k,,如果鲨鱼当天游过的距离 >= k, 代表的鲨鱼在这天会前往下一个地点,现在求鲨鱼在每个停留的地点所待的时间是一样的,然后在上面那个情况下使得鲨鱼所待得地点数目最多,然后再地点数目的情况下保证K最小. 题解:从小到大处理元素,每次将k = 当前元素+1,因为只有k比一个元素大了才会有新的停留地点,然后对于所有出现过的元素鲨鱼都会在这个点停留,然后我们需要对每一段连续的停留片段计算出他…
题目链接:http://codeforces.com/contest/982 A. Row time limit per test:1 second memory limit per test:256 megabytes input:standard input output:standard output You're given a row with nn chairs. We call a seating of people "maximal" if the two follow…
C. Cut 'em all! time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output You're given a tree with nn vertices. Your task is to determine the maximum possible number of edges that can be removed in s…
A - Lake Counting POJ - 2386 最最最最最基础的dfs 挂这道题为了提高AC率(糖水不等式 B - Paint it really, really dark gray CodeForces - 717E dfs 待会写题解 C - New Year Transportation CodeForces - 500A 简单的模拟 D - Binary Tree Traversals HDU - 1710 给树的先序中序输出后序 贴下代码 #include <algorith…
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Typ…
DP! 黄题: 洛谷P2101 命运石之门的选择 假装是DP(分治 + ST表) CF 982C Cut 'em all! 树形贪心 洛谷P1020 导弹拦截 单调队列水题 绿题: 洛谷P1594 护卫队 一维线性DP 洛谷P1137 旅行计划 拓扑序DP(递推) 洛谷P2736 “破锣摇滚”乐队 Raucous Rockers 三维DP 洛谷P3197 [HNOI2008]越狱 假装是递推(组合数学) 洛谷P1122 最大子树和 树形贪心(笑) 洛谷P1063 能量项链 极水的区间DP,断环复…
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-…
A. Link/Cut Tree 题目连接: http://www.codeforces.com/contest/614/problem/A Description Programmer Rostislav got seriously interested in the Link/Cut Tree data structure, which is based on Splay trees. Specifically, he is now studying the expose procedure…