ural 1069. Prufer Code】的更多相关文章

1069. Prufer Code Time limit: 0.25 secondMemory limit: 8 MB A tree (i.e. a connected graph without cycles) with vertices is given (N ≥ 2). Vertices of the tree are numbered by the integers 1,…,N. A Prufer code for the tree is built as follows: a leaf…
Prufer Code Time limit: 0.25 secondMemory limit: 8 MB A tree (i.e. a connected graph without cycles) with vertices is given (N ≥ 2). Vertices of the tree are numbered by the integers 1,-,N. A Prufer code for the tree is built as follows: a leaf (a ve…
记录每个节点的出度,叶子节点出度为0,每删掉一个叶子,度数-1,如果一个节点的出度变成0,那么它变成新的叶子. 先把所有叶子放到优先队列中. 从左往右遍历给定序列,对于root[i],每次取出叶子中编号最小的那个与root[i]相连,并且--degree[ root[i] ],如果degree[ root[i] ]为0,那么把root[i]放入优先队列. #include <cstdio> #include <cstring> #include <cstdlib> #i…
1069. Prufer Code Time limit: 0.25 secondMemory limit: 8 MB A tree (i.e. a connected graph without cycles) with vertices is given (N ≥ 2). Vertices of the tree are numbered by the integers 1,…,N. A Prufer code for the tree is built as follows: a leaf…
1792. Hamming Code Time limit: 1.0 second Memory limit: 64 MB Let us consider four disks intersecting as in the figure. Each of the three shapes formed by the intersectionof three disks will be called a petal. Write zero or one on each of the disks.…
原题链接:http://acm.timus.ru/problem.aspx?space=1&num=1780 1780. Gray Code Time limit: 0.5 secondMemory limit: 64 MB Denis, Vanya and Fedya gathered at their first team training. Fedya told them that he knew the algorithm for constructing aGray code. Cre…
题意:删除一棵树上的叶子 每删除一片叶子就写下连着该片叶子的节点  让你还原一棵树 记录每个节点连着的叶子数 0表示此时这个节点就是叶子  -1表示这个节点已经删除 删除的只能是0  就是说是叶子 暴力一下就好了 #include <cstring> #include <cstdio> #include <algorithm> #include <vector> using namespace std; vector<int> g[7550];…
ZOJ Problem Set - 1097 Code the Tree Time Limit: 2 Seconds      Memory Limit: 65536 KB A tree (i.e. a connected graph without cycles) with vertices numbered by the integers 1, 2, ..., n is given. The "Prufer" code of such a tree is built as foll…
Code the Tree Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2350   Accepted: 906 Description A tree (i.e. a connected graph without cycles) with vertices numbered by the integers 1, 2, ..., n is given. The "Prufer" code of such a…
Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2292   Accepted: 878 Description A tree (i.e. a connected graph without cycles) with vertices numbered by the integers 1, 2, ..., n is given. The "Prufer" code of such a tree is built…
Code the Tree Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2259   Accepted: 859 Description A tree (i.e. a connected graph without cycles) with vertices numbered by the integers 1, 2, ..., n is given. The "Prufer" code of such a…
G.Code the Tree Time Limit: 2 Sec  Memory Limit: 128 MB Submit: 35  Solved: 18 [Submit][Status][Web Board] Description A tree (i.e. a connected graph without cycles) with vertices numbered by the integers 1, 2, ..., n is given. The "Prufer" code…
本文介绍北京大学ACM网站2567号题目的解法.介绍部分基本翻译自网站上的题目介绍. 题目介绍:    给定一棵各节点编号为整数1,2,3...n的树(例如,无环连通图),其Prufer编码(Prufer code,不知道有没有标准的译法,用金山词霸没有查到,用Google也没有搜索到)构造方法如下:从树中去掉编号值最小的叶子节点(仅与一条边邻接的节点),以及与它邻接的边后,记下与它邻接的节点的编号.在树中重复这个过程,知道只剩下一个节点(总是编号为n的节点)为止.记下的n-1个编号序列就是树的…
问题: 4个标记为1,2,3,4的节点构成自由树(算法导论里的定义,连接着,无环,无向的图),一共有多少种构造方法?如果N个节点呢? 解决方法: 4个节点可以通过穷举的方式得到答案,一共有16中方式. 第一类构造方式,取一个节点做中心,剩余三个节点与其相连,一共4种(每个节点做一次中心). 第二类构造方式,四个节点连成一条线,可以看成个排列,第一个有4种取法,第二个有3种,依次类推.但是因为例如1234与4321结构上是一样的, 所以总的排列数除以2才是总共的构造数,即 $\frac{4!}{2…
2017 CERC Problem A:Assignment Algorithm 题目描述:按照规则安排在飞机上的座位. solution 模拟. 时间复杂度:\(O(nm)\) Problem B:Buffalo Barricades 题目描述:将二维平面划分为一个个单位格,在平面上有一些被标记的格子.将\(x,y\)轴当成是围栏,现在以此给出一些坐标,每次从那个坐标出发向\(x, y\)轴建围栏,并输出新围栏围住的被标记格子数. solution 待解决. C:Cumulative Code…
题目大意:2567是给出一棵树,让你求出它的Prufer序列.2568时给出一个Prufer序列,求出这个树. 思路:首先要知道Prufer序列.对于随意一个无根树,每次去掉一个编号最小的叶子节点,并保存这个节点所连接的节点所得到的序列就是这棵树的Prufer序列. 这个序列有十分优雅的性质.它能与无根树一一相应.因此.两个标号一样的无根树得到的Prufer序列也一定是一样的. 此外,设一个节点的度数是d[i],那么他会在Prufer序列中出现d[i] - 1次. 2567:记录每个节点的度.依…
G - Gray CodeTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87643#problem/G Description Denis, Vanya and Fedya gathered at their first team training. Fedya told them that he knew the algorithm for c…
http://acm.timus.ru/problem.aspx?space=1&num=1932 B - The Secret of Identifier Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status Practice URAL Description Davy Jones: You've been captain of the Black Pearl for 13 y…
ural 1091 题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1091 题意是从1到n的集合里选出k个数,使得这些数满足gcd大于1 解法: 因子有2的数: 2,4,6,8,10,12,14... 因子有3的数:3,6,9,12,15,18,21... 因子有5的数:5,10,15,18,21,24... 可以看出这里求出的集合时会有重复的,得去从.可惜没有学过容斥原理.不过解决这题还是没问题的. 50以内的素因子有:2, 3, 5, 7…
Description 给定一棵树每个节点度的限制为di,求有多少符合限制不同的树. Solution 发现prufer码和度数必然的联系 prufer码一个点出现次数为它的度数-1 我们依然可以把树转成序列进行处理 只是每个元素出现次数受到了限制 于是就是有重复元素的排列问题了 公式很好推 Code 特殊情况判一判 #include<cstdio> #include<algorithm> #include<cstring> #define ll long long u…
Description 一开始森林里面有N只互不相识的小猴子,它们经常打架,但打架的双方都必须不是好朋友.每次打完架后,打架的双方以及它们的好朋友就会互相认识,成为好朋友.经过N-1次打架之后,整个森林的小猴都会成为好朋友. 现在的问题是,总共有多少种不同的打架过程. 比如当N=3时,就有{1-2,1-3}{1-2,2-3}{1-3,1-2}{1-3,2-3}{2-3,1-2}{2-3,1-3}六种不同的打架过程.   Solution 有一种神奇的prufer码,这种码和一棵树一一对应. 由树…
Description Input Output Sample Input Sample Output Solution 题意:给你$n$个点,还有每个点的度数,问你任选$i(1\leq i \leq n)$个点构成树的方案数. 这种统计生成树的题,很容易想到用$prufer$序列搞. $prufer$序列有一个很重要的性质,一个度数为$d$的点会在$prufer$序列里出现$d-1$次. 那么我们设$f[i][j][k]$表示$DP$完前$i$个点,选了其中的$j$个点,当前$prufer$序…
1877. Bicycle Codes Time limit: 0.5 secondMemory limit: 64 MB Den has two four-digit combination locks for protecting his bicycle from thieves. Every evening he arms the bicycle antitheft alarm and fastens the bicycle to a special stand with one of t…
Brief Description 给出标号为1到N的点,以及某些点最终的度数,允许在 任意两点间连线,可产生多少棵度数满足要求的树? Algorithm Design 结论题. 首先可以参考这篇文章了解一下什么是Prufer编码: Cayley公式是说,一个完全图\(K_n\)有\(n^{n-2}\)棵生成树,换句话说n个节点的带标号的无根树有\(n^{n-2}\)个.今天我学到了Cayley公式的一个非常简单的证明,证明依赖于Prüfer编码,它是对带标号无根树的一种编码方式. 给定一棵带标…
树 Time Limit: 10 Sec  Memory Limit: 256 MB Description Input Output Sample Input 3 2 2 1 Sample Output   3 3 2 HINT Solution 由于是带标号的无根树的计数,于是我们运用prufer编码的性质来解题. prufer编码的几个性质: 1.对于大小为s的树,prufer编码是一个长度为 s-2 的序列: 2.i在序列中出现的次数<deg[i]: 3.一个prufer编码表示一棵树.…
题目描述 给定一个完全图,保证\(w_{u,v}=w_{v,u}\)且\(w_{u,u}=0\),等概率选取一个随机生成树,对于每一对\((u,v)\),求\(dis(u,v)\)的期望值对\(998244353\)取模. 输入 第一行一个数\(n\) 接下来\(n\)行,每行\(n\)个整数,第\(i\)行第\(j\)个整数表示\(w_{i,j}\) 输出 输出共\(n\)行,每行\(n\)个整数,第\(i\)行第\(j\)个整数表示\(dis(i,j)\)的期望值 样例 样例输入 4 0 1…
链接: http://acm.timus.ru/problem.aspx?space=1&num=1303 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=26733#problem/D D - Minimal Coverage Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice UR…
链接:  http://acm.timus.ru/problem.aspx?space=1&num=1010 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=26732#problem/E E - Discrete Function Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice …
Description 自从明明学了树的结构,就对奇怪的树产生了兴趣......给出标号为1到N的点,以及某些点最终的度数,允许在任意两点间连线,可产生多少棵度数满足要求的树? Input 第一行为N(0 < N < = 1000),接下来N行,第i+1行给出第i个节点的度数Di,如果对度数不要求,则输入-1 Output 一个整数,表示不同的满足要求的树的个数,无解输出0 Sample Input - - Sample Output HINT 两棵树分别为1-2-3;1-3-2 Soluti…
传送门 Solution 根据prufer序列做的题,具体可以看这里 还知道了一种避免高精除的方法quq Code #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm> #define F(i,a,b) for(register int i=(a);i<=(…