链接 大意: 给定邻接表表示两点是否可以连接, 要求将图连成树, 且边不相交的方案数 n范围比较小, 可以直接区间dp $f[l][r]$表示答案, $g[l][r]$表示区间[l,r]全部连通且l,r间连边的方案 #include <iostream> #include <algorithm> #include <cstdio> #define PER(i,a,n) for(int i=n;i>=a;--i) #define REP(i,a,n) for(int…
大意: 求a->b最短路长度为m的n节点树的个数, 边权全部不超过m 枚举$a$与$b$之间的边数, 再由拓展$Caylay$定理分配其余结点 拓展$Caylay$定理 $n$个有标号节点生成k棵树的森林, 且给定$k$个点各属于$k$棵树的方案数为$kn^{n-k-1}$ 可以得到有$x$条边的方案数为$\binom{m-1}{x-1}\binom{n-2}{x-1}(x-1)!m^{n-1-x}(x+1)n^{n-x-2}$ int n, m; ll fac[N], ifac[N]; ll…
题目链接:http://codeforces.com/contest/745/problem/C C. Hongcow Builds A Nation time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Hongcow is ruler of the world. As ruler of the world, he wants t…
Prelude 真是一道好题,然而比赛的时候花了太多时间在B题上,没时间想这个了QAQ. 题目链接:萌萌哒传送门(.^▽^) Solution 观察样例和样例解释,我们发现,假如有四个点,恰好占据在某一个矩形的四个顶点上,那么这个矩形的四条边,都是可以自由选择画或者不画的. 这时候,我们称这四个点每个点"控制"了四条直线中的一条,可以自由选择这条直线画或者不画.这里可以配合样例解释理解一下. 考虑建立常用的图论模型--行列模型,把每一行抽象成一个点,每一列抽象成一个点,假如在\((x,…
题意: 给你两个串s1,s2和一个K, 有一种操作是在一个串切开然后交换位置, 问s1有多少种方法经过K次这样的操作变成s2: 思路: (从来没接触过计数DP...还是太菜...参考了[大牛blog] 首先呢,不管怎么切怎么换,都是原串自己转来转去有没有???看到这个其实我还是不懂.... 然后呢,我们搞一个DP数组记下数,纯粹就是计数的: dp[now][0]代表到第i步变成原串的方案数: dp[now][1]代表到第i步变成非原串的方案数: 从哪里变成原串啊?一个原串可以变成len-1个非原…
大意: 有一段$n$千米的路, 每一次走$1$千米, 每走完一次可以休息一次, 每连续走$x$次, 消耗$a[1]+...+a[x]$的能量. 休息随机, 求消耗能量的期望$\times 2^{n-1}$. 简单计数题, 枚举每种长度的贡献. #include <iostream> #include <algorithm> #include <cstdio> #define REP(i,a,n) for(int i=a;i<=n;++i) using namesp…
大意:定义一个长为$k>1$且首项为$k-1$的区间为好区间. 定义一个能划分为若干个好区间的序列为好序列. 给定序列$a$, 求有多少个子序列为好序列. 刚开始一直没想出来怎么避免重复计数, 看了别人题解才会. 设$dp[i]$为以$a_i$开头的个数, 枚举$a_i$所在好区间的最后一个数$j$, 有$dp[i]=\sum \binom{j-1-1}{a_i-1}\sum\limits_{k=j+1}^n dp[k]$ #include <iostream> #include <…
/* 不要低头,不要放弃,不要气馁,不要慌张. 题意: 给你一个有n个点,m条边的无向图,给每条边规定一个方向,使得这个图变成有向图,并且使得尽可能多的点入度与出度相同. 输出有多少个这样的点并且输出有向图. 思路: 1.针对每个连通分支. 2.所有点入度与出度相同,显然这是欧拉回路存在的判定定理,但是欧拉回路的另外一个等价定理是所有点的度数是偶数.那如果给我们的图中的某些点是奇数度该怎么办. 3.显然原图中给的点如果度数是奇数,那么该点的入度与出度一定不相同. 4.根据握手定理,无向图中度数是…
题目链接 题意 给一棵树,对于一个节点,与它相邻的结点可以连一条边,求所有点对间距离之和 思路 任意两点间的距离被优化为$\left \lceil \frac{s}{2} \right \rceil$,转化为任意两点间距加间距为奇数的路径数,即$\frac{\sum_{i\in G,j\in G,i<j}dis[i][j]+\sum_{i\in G,j\in G,i<j[dis[i][j]=1(mod 2)]}}{2}$,奇数路径可由深度奇偶性不同的点数相乘得到,即$\sum_{i\in G}…
一开始没有思路,以为要判联通块. 其实不是判断联通块,而是判断边是否连在一起,没有连边的点可以忽略不计 /* 分情况讨论: 1.忽略自环,那么要取出两条相连的普通变作为只经过一次的边 2.一条自环,一条普通边 3.两条自环 自环独立存储 */ #include<bits/stdc++.h> using namespace std; #define maxn 1000005 #define ll long long struct Edge{ int to,next; }edge[maxn<…
大意: 给定树, 多组询问, 每个询问给出一个点集$S$, 给定$m, r$, 求根为$r$时, $S$的划分数, 满足 每个划分大小不超过$m$ 每个划分内不存在一个点是另一个点的祖先 设点$x$的祖先包括x的个数为$f[x]$ (不属于集合S的点不计算), 按$f$排序后, 有转移 $$dp[i][j]=dp[i-1][j-1]+(j-f[i]+1)dp[i-1][j]$$ dp[i][j]为前i个, 划分为j组的方案数 所以讨论一下$r$与$1$的位置关系求出f就行了 #include <…
大意: 给定括号字符串, 求多少个子序列是RSGS. RSGS定义如下: It is not empty (that is n ≠ 0). The length of the sequence is even. First $\frac{n}{2}$ charactes of the sequence are equal to "(". Last $\frac{n}{2}$ charactes of the sequence are equal to ")". 枚举…
大意: 给N个点,起点S终点T,每个点有X,A,B,C,D,根据I和J的X坐标可得I到J的距离计算公式 |xi - xj| + ci + bj seconds if j< i |xi - xj| + di + aj seconds otherwise (j > i). 求从起点到终点,经过N个点恰好一次的最短路 这题好巧妙啊, 以前都没见过这种做法, 看题解说是要维护一条从S->T的链, 每次贪心往链里添点就行了, 显然这个贪心是与顺序无关的, 所以一定最优. 复杂度是$O(n^2)$的…
链接 大意: 给定有向图, 求选择一个点集$S$, 使得$S$任意两点不相连, 且对于不属于$S$的任意点$x$, 均存在$S$中的点$y$, 使得$d(x,y)<=2$, $d(x,y)$为从$x$到$y$的最短路 对于图G, 任选一点M, 删除d(M,x)=1的所有x以及M, 得到生成子图G', 假设已经得到G'的解S', 若S'中有点x使得d(x,M)=1,则S'就是G的解, 否则S'并M构成G的解 #include <iostream> #include <algorith…
链接 大意:求只含小写字母, 长度为n, 且可以与给定模板串匹配的字符串个数 (多组数据) 记模板串为P, 长为x, 总串为S. 设$f_i$为S为i时的匹配数, 考虑P最后一位的首次匹配位置. 若为S的最后一位,枚举P的前x-1位的首次匹配位置,其余位不能为下一个要匹配的字符, 有方案数$25^{i-x}\binom{i-1}{x-1}$; 否则的话, 最后一位可以放任意字符, 方案数$26f_{i-1}$. 即$f_i=25^{i-x}\binom{i-1}{x-1}+26f_{i-1}$.…
大意:$n$结点$m$条边无向图, 满足 $(1)$经过$m-2$条边$2$次 $(2)$经过其余$2$条边$1$次 的路径为好路径, 求所有好路径数 相当于边加倍后再删除两条边, 求欧拉路条数 首先欧拉路径存在等价于奇数度点不超过2个 所以删除自环边一定成立,若删除两条非自环边, 则两边必须有公共点 特判下不连通输出0即可 #include <iostream> #include <algorithm> #include <cstdio> #define REP(i,…
大意: 定义$f(a)$表示序列$a$本质不同子序列个数. 给定$n,m$, 求所有长$n$元素范围$[1,m]$的序列的$f$值之和. 显然长度相同的子序列贡献是相同的. 不考虑空串, 假设长$x$, 枚举第一次出现位置, 可以得到贡献为$\sum\limits_{i=x}^n\binom{i-1}{x-1}(m-1)^{i-x}m^{n-i}$ 总的答案就为$\sum\limits_{x=1}^n m^x \sum\limits_{i=x}^n\binom{i-1}{x-1}(m-1)^{i…
大意: 一个$k$层完全二叉树, 每个节点向它祖先连边, 就得到一个$k$房子, 求$k$房子的所有简单路径数. $DP$好题. 首先设$dp_{i,j}$表示$i$房子, 分出$j$条简单路径的方案数, 那么最终答案就为$dp_{i,1}$. 考虑两棵$i-1$房子转移到$i$房子的情况, 分四种情况. 两个子树间不与根节点连边, 那么$dp_{i,j+k}=\sum dp_{i-1,j}dp_{i-1,k}$ 两个子树只有一条路径与根节点连边, $dp_{i,j+k}=\sum dp_{i-…
大意: 给定$n,k,l,m$, 求有多少个长度为$n$, 元素全部严格小于$2^l$, 且满足 的序列. 刚开始想着暴力枚举当前or和上一个数二进制中$1$的分布, 但这样状态数是$O(64^3)$在加上矩阵幂的复杂度显然不行. 看了题解发现可以按每位单独来考虑. #include <iostream> #include <sstream> #include <algorithm> #include <cstdio> #include <math.h…
You are given an array a of length n. We define fa the following way: Initially fa = 0, M = 1; for every 2 ≤ i ≤ n if aM < ai then we set fa = fa + aM and then set M = i. Calculate the sum of fa over all n! permutations of the array a modulo 109 + 7.…
首先可以发现的是,因为两条线段不能在除了端点处相交,因此整个多边形最终一定会被连接成最上方由若干条线段在端点处相交连成,每条线段下方又是一个子结构. 因此你会发现,每个合法的状态都能被分成两个合法的子结构,因此可以考虑使用区间 \(dp\) 来解决这个问题. 首先,我们简单地考虑令 \(dp_{i, j}\) 表示只使用 \(i, j\) 这个区间之间的边将 \(i, j\) 这个区间联通的方案. 直接在 \(i, j\) 之间连边. 那么枚举每个 \(k\) 做为中间的断点,那么就有转移: \…
Description Given an undirected graph, in which two vertices can be connected by multiple edges, what is the size of the minimum cut of the graph? i.e. how many edges must be removed at least to disconnect the graph into two subgraphs? Input Input co…
C. Alyona and the Tree 题目连接: http://www.codeforces.com/contest/682/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 verte…
Make It Connected CodeForces - 1095F You are given an undirected graph consisting of nn vertices. A number is written on each vertex; the number on vertex ii is aiai. Initially there are no edges in the graph. You may add some edges to this graph, bu…
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…
洛谷 Codeforces 思路 看到计数和\(998244353\),可以感觉到这是一个DP+生成函数+NTT的题. 设\(s_i\)表示\(i\)是否在集合中,\(A\)为\(s\)的生成函数,即\(A(x)=\sum_n s_nx^n\). 设\(f_n\)为有\(n\)分时二叉树的个数. 考虑枚举左子树大小和根节点权值,得到 \[ f_n=[n=0]+\sum_{i=1}^{mx} s_i \sum_{j=0}^{n-i} f_jf_{n-i-j} \] 然后记\(F(x)\)为\(f\…
A. Alyona and Numbers time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output After finishing eating her bun, Alyona came up with two integers n and m. She decided to write down two columns of inte…
C. Alyona and the Tree time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Alyona decided to go on a diet and went to the forest to get some apples. There she unexpectedly found a magic rooted…
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…
A. Minimizing the String time limit per test 1 second memory limit per test 256 megabytes Description: You are given a string ss consisting of nn lowercase Latin letters. You have to remove at most one (i.e. zero or one) character of this string in s…