[CodeForces-375E]Red and Black Tree】的更多相关文章

[CC-BLREDSET]Black and Red vertices of Tree 题目大意: 有一棵\(n(\sum n\le10^6)\)个结点的树,每个结点有一种颜色(红色.黑色.白色).删去一个由红色点构成的连通块,使得存在一个黑点和一个白点,满足这两个点不连通.问有多少种删法. 思路: 设满足删掉这个点后,使得存在一个黑点和一个白点,满足这两个点不连通的红点为关键点.那么我们可以用两个\(\mathcal O(n)\)的树形DP求出所有的关键点.剩下的问题就变成了求有多少种全红连通…
codeforces 741D Arpa's letter-marked tree and Mehrdad's Dokhtar-kosh paths 题意 给出一棵树,每条边上有一个字符,字符集大小只有22. 对于每一个子树,询问其中最长的,满足:路径上的字符集可以重组成字符串的路径的长度. 题解 明显是用mask维护信息,然后启发式合并一下. 一般启发式合并需要用map维护信息,这样的复杂度是log^2的.如果保留每个点重儿子的信息,就可以用全局变量维护,全局变量的大小就可以开很大,可以做到l…
codeforces 812E Sagheer and Apple Tree 题意 一棵带点权有根树,保证所有叶子节点到根的距离同奇偶. 每次可以选择一个点,把它的点权删除x,它的某个儿子的点权增加x.点权中途中不能为负.如果选中的是叶子节点,则只删除它的点权. 两个人玩博弈,后手可以先交换两个点点权,问有多少种方法使得他必胜? 题解 观察一下可以发现,把和叶节点同奇偶的那些点拉出来,相比nim博弈多了一种操作:增加一些石子在某堆.不过本质不会有什么变化,因为如果A选择增加,B可以选择减少相同的…
题目链接 codeforces 220 C. Game on Tree 题解 对于 1节点一定要选的 发现对于每个节点,被覆盖切选中其节点的概率为祖先个数分之一,也就是深度分之一 代码 #include<cstdio> #include<algorithm> const int maxn = 1000007; struct node { int u,v,next; } edge[maxn << 1] ; int head[maxn],num = 0; inline vo…
Red/Blue Spanning Tree Time Limit: 2000ms Memory Limit: 131072KB This problem will be judged on HDU. Original ID: 426364-bit integer IO format: %I64d      Java class name: Main   Given an undirected, unweighted, connected graph, where each edge is co…
题目描述: Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Alyona has a tree with n vertices. The root of the tree is the vertex 1. In each vertex Alyona wrote an positive int…
CF375E Red and Black Tree(线性规划) Luogu 题解时间 很明显有一个略显复杂的 $ n^3 $ dp,但不在今天讨论范围内. 考虑一些更简单的方法. 设有 $ m $ 个点为黑,转化成线性规划问题,很明显有 \[minimum:\sum\limits_{i} ( 1 - col_{i} ) x_{i} \] \[\sum\limits_{ dis(i,j) \le lim } x_{j} \ge 1 \] \[\sum\limits_{i} x_{i} =m \]…
Description Xenia the programmer has a tree consisting of n nodes. We will consider the tree nodes indexed from 1 to n. We will also consider the first node to be initially painted red, and the other nodes — to be painted blue. The distance between t…
\(>Codeforces \space 379 F. New Year Tree<\) 题目大意 : 有一棵有 \(4\) 个节点个树,有连边 \((1,2) (1,3) (1,4)\) ,一共有 \(q\) 次操作,每次选择一个节点,新建两个节点 \(n + 1, n + 2\) 向其连边,每次操作完之后求树的直径的长度 $1 \leq q \leq 5 \times 10^5 $ 解题思路 : 观察发现,每次加点后,树的直径的端点至多只会变化一个 证明:首先显然新的直径的端点不可能是两个…
题目链接:http://codeforces.com/problemset/problem/399/B 题目意思:给出 n 个只由 R 和 B 组成的字符串(由上到下排列,相当于栈),问最多可以操作多少轮.一轮包括三个步骤:(1)如果栈顶的球为 red,则弹出(注意可以弹出多个),直到遇到栈顶的球为blue  (2)将这个blue球替换为red球(只操作一次)  (3)在替换完red 球上面添加blue球,直到有 n 个球为止.当然一轮中不一定3个步骤都执行.当 n 个球都为red时,就不能再操…
time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard output Anton is growing a tree in his garden. In case you forgot, the tree is a connected acyclic undirected graph. There are n vertices in the tree, e…
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard output A tree is an undirected connected graph without cycles. Let's consider a rooted undirected tree with n vertices, numbered 1 through n. There are…
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Connected undirected weighted graph without self-loops and multiple edges is given. Graph contains n vertices and m edges. For each edge (u,…
题目链接:http://codeforces.com/problemset/problem/682/C 题意:如果点v在点u的子树上且dist(u,v)>a[v]则u和其整个子树都将被删去,求被删去的点数. 思路:1为根节点,从1开始DFS遍历,记录距离dis为到祖宗节点的最大距离. #include<bits/stdc++.h> using namespace std; typedef long long ll; const int N=1e5+5; int a[N],num[N],a…
题目链接:http://codeforces.com/problemset/problem/161/D 题意: 给你一棵树,问你有多少对点的距离为k. 思路: dp[i][j]表示离i节点距离为j的点个数,2次dfs,一次从底向上,另一次从顶向下. //#pragma comment(linker, "/STACK:102400000, 102400000") #include <algorithm> #include <iostream> #include &…
Alyona and the Tree 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/C Description Alyona decided to go on a diet and went to the forest to get some apples. There she unexpectedly found a magic rooted tree with root in the vertex 1, every v…
题目链接: http://codeforces.com/problemset/problem/698/B http://codeforces.com/problemset/problem/699/D 题目大意: 通过给定当前节点的父亲给你一棵有错的树,可能有多个根和环,输出改成正确的一棵树至少要修改几个节点的父亲和修改后所有点的父亲值 题目思路: [并查集][模拟] 用并查集把成环的归在一起(类似强连通分量),然后统计分量数并修改. 第一个出现的当作根,其余的每一块连通分量都去掉一条边改为连接到…
Ilya is very fond of graphs, especially trees. During his last trip to the forest Ilya found a very interesting tree rooted at vertex 1. There is an integer number written on each vertex of the tree; the number written on vertex i is equal to ai. Ily…
题目链接: http://codeforces.com/problemset/problem/698/B 题解: 还是比较简单的.因为每个节点只有一个父亲,可以直接建反图,保证出现的环中只有一条路径. 然后发现,有多少个环,就需要改多少条边.然后设出现连向自己的节点为x,这些也要改边,对答案的贡献应为:$max(x-1,0)$.对于最后的根节点,有自环选一个,没自环在其他环上任选一个点就行. #include<cstdio> inline int min(int a,int b){return…
感觉dsu on tree一定程度上还是与点分类似的.考虑求出跨过每个点的最长满足要求的路径,再对子树内取max即可. 重排后可以变成回文串相当于出现奇数次的字母不超过1个.考虑dsu on tree,容易想到遍历时记录每种情况的最大深度,合并时类似点分的逐个计算贡献再合并即可.这里有个问题是得到某子树信息后,对于原来的根来说,这个信息还要再加上一个偏移量,但直接暴力显然复杂度就不对了.实际上维护信息过程中不断传递偏移量即可. 一开始就发现了这个题只开了256M,于是机智的开了个map,悲惨的T…
Darth Vader and Tree 感觉是个很裸的矩阵快速幂, 搞个100 × 100 的矩阵, 直接转移就好啦. #include<bits/stdc++.h> #define LL long long #define fi first #define se second #define mk make_pair #define PLL pair<LL, LL> #define PLI pair<LL, int> #define PII pair<int,…
题目链接 \(Description\) 给定一棵树,每条边上有一个字符(a~v).对每个节点,求它的子树中一条最长的路径,满足 路径上所有边上的字符可以重新排列成一个回文串.输出其最长长度. \(n\leq 5\times10^5\). \(Solution\) 可以构成回文串,即要么所有字符都出现了偶数次,要么有一个出现了奇数次.其余都出现了偶数次. 转化为异或!把每个字符c(0~21)映射到1<<c上去. 令\(s[x]\)表示根节点到\(x\)路径上边权的异或和.那么路径\((u,v)…
题目链接:Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths 第一次写\(dsu\ on\ tree\),来记录一下 \(dsu\ on\ tree\)主要维护子树信息,往往可以省掉一个数据结构的启发式合并.大体思路如下: 轻重链路径剖分之后,对每个点先递归处理他的所有轻儿子,每次处理完轻儿子之后把这棵子树的信息清空.最后再来处理重孩子,重儿子的信息就可以不用清空了.由于我们是用一个全局数组来记录信息的,重儿子子树的信息就仍然保留…
A - Link/Cut Tree Programmer Rostislav got seriously interested in the Link/Cut Tree data structure, which is based on Splay trees. Specifically, he is now studying the exposeprocedure. Unfortunately, Rostislav is unable to understand the definition…
260D - Black and White Tree 思路:把两种颜色先按值sort一下,最小值肯定是叶子,然后把这个叶子和另外一中颜色的一个最小值的节点连接起来,再把这个节点变成叶子,把值减掉就可以了. 如下图: 代码1: #include<bits/stdc++.h> using namespace std; ; struct node { int val,id; bool operator <(node &a) { return val<a.val; } }a[N]…
题目链接:http://codeforces.com/problemset/problem/682/C 题目大意:取树上任意一个点v,若点v的子树中有一个点u使得dist(v,u)>a[u]那么称节点v是伤心的.给你一个根为1的树,每个节点有一个权值a[i],每条边也有一个权值w,现在让你删最少的结点,使得树上不存在伤心的点.解题思路:删除最少的点,我们可以反一下,变成找最多的点,使得这些点不伤心.只要对这棵树进行DFS,同时记录路径长度dis,当到达某点u时,若dis>a[u],那么要将u及…
B. Destruction of a Tree time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output You are given a tree (a graph with n vertices and n - 1 edges in which it's possible to reach any vertex from any ot…
You are given a tree (a connected acyclic undirected graph) of n vertices. Vertices are numbered from 1 to n and each vertex is assigned a character from a to t. A path in the tree is said to be palindromic if at least one permutation of the labels i…
D. Alyona and a tree time limit per test  2 seconds memory limit per test  256 megabytes input  standard input output  standard output Alyona has a tree with n vertices. The root of the tree is the vertex 1. In each vertex Alyona wrote an positive in…
Alyona decided to go on a diet and went to the forest to get some apples. There she unexpectedly found a magic rooted tree with root in the vertex 1, every vertex and every edge of which has a number written on. The girl noticed that some of the tree…