##[1001.Average](http://acm.hdu.edu.cn/showproblem.php?pid=5353)
Summary
$n$ people are sitting around a circle and every two adjacent people can exchange only on candy. The goal is to find a valid exchange order to make everyone have equal amount of candies (or determine it is impossible). Solution
Find the sum of the candies. If the sum is not a multiple of $n$, such order does not exist.
let $c_{i}$ be the difference between the candies $i$-th person have and the average candies.
Enumerate 1-st person’s operation: give a candy to 2, receive a candy from 2, do nothing.
Then determine next one’s operation according to the value $c_{i}$:
$c_{i}$= −1: receive a candy from $i$ + 1.
$c_{i}$ = 0: just do nothing.
$c_{i}$ = 1: give a candy to $i$ + 1.
otherwise, we can not find a valid exchange order.
Time complexity: $O(n)$.
##[1002.Bipartite Graph](http://acm.hdu.edu.cn/showproblem.php?pid=5354) Summary
We are given an undirected graph $G$ with $n$ vertices and $m$ edges. You are to determine for each vertex $i$, after deleting it, whether the graph becomes a bipartite graph. Solution
For every edge $(u, v)$, set a time interval $[s, t)$, which means the edge is only available from time $s$ to time $t$.
Every deletion will make some edges become unavailable at some time points. More specifically, if we delete $i$-th vertex, every edge adjacent to vertex $i$ will unavailable at time $i$. Then the number of available time intervals for each edge will be at most 3.
If we know whether the graph is a bipartite graph at a specific time $i$, we also know whether $i$-th vertex satisfy our will.
Solve the problem: You are given an undirected graph $G$ with $n$ vertices and $m$ edges. Each edge will available only in a specific time interval $[st, ed)$. For every time $t$, you are to determine whether the graph is a bipartite graph.
Let function $Solve(l, r)$ solve all time from $l$ to $r$ and $E(l, r)$ is a set of edges which can exist at some time in interval $[l, r)$.
Let mid = $\frac{l+r}{2}$, for each edge in $E$, assume the interval is $[x, y)$:
$x = l$,$ y = r$, add the edge and check if it forms an odd circle with other edges added before.
$y \leq mid$, add the edge to set $E(l,mid)$.
$x \geq mid$, add the edge to set $E(mid, r)$.
otherwise, add the edge to both sets $E(l,mid)$ and $E(mid, r)$.
We can use disjoint-set to maintain odd circles. After each $Solve(l, r)$, remember to resume the disjoint-set.
How to resume the disjoint-set:
Just union by rank and there is no need to use path compression.
Before every union operation, use a stack to store the state before.
Resuming the disjoint-set is just to restore the state according to the stack.
Time complexity: $O(m \ log^{2}\ n)$ ##[1003.Cake](http://acm.hdu.edu.cn/showproblem.php?pid=5355) Summary You are given n integers 1, 2, . . . , $n$ and an integer $m$. You need to divide the $n$ integer into $m$ equal sum parts (or determine it is impossible). Solution
If $m$ is not divisible by $\frac {n(n+1)}{2}$ or $n < 2m - 1$, it is impossible.
If we have 2$m$ number 1, 2, . . . , 2$m$, we can divide it into $m$ equal sum parts easily.
If $n$ is large, we can reduce $n$ to $n - 2m$. If $n (n \leq40)$ is small, we can use backtracking to find a possible solution or we can construct the solution by hand.
Time complexity: $O(n)$. ##[1004.Deal](http://acm.hdu.edu.cn/showproblem.php?pid=5356) Summary Given are a weighted tree with $n$ vertices. There’s an item with weight $w_{i}$ in $i$-th vertex. The cost for transporting an item with weight $w$ from vertex $u$ to vertex $v$ is $w$ multiply the distance from vertex $u$ to vertex $v$. Item will be cached in the vertex it passed. You are going to make $m$ transportations and make the sum of cost maximum. Each item will be transported from a vertex containing it and closet to the destination. Solution
Each item is independent, so just consider item by item.
For item $i$, the first transportation must be the longest, same as the second transportation and so on.
Using greedy to find the path. Sort all the possible transportations and get the largest $m$.
Time complexity: $O(n^{2}\ log\ n)$. ##[1005.Easy Sequence](http://acm.hdu.edu.cn/showproblem.php?pid=5357) Summary You are given a parentheses string. For $i$-th character, you need to find the number valid strings starting with it or ending with it. For $i$-th character, you need to find the number valid substrings containing it. Solution
let $match_{i}$ be the position of matched parenthese for $i$-th character, $a_{i}$ be the number of valid substrings starting with $i$-th character, and $b_{i}$ be the number of valid substrings ending with $i$-th character.
$a_{i}=a_{match_{i+1}}+1$, $b_{i}=b_{match_{i-1}}+1$
assume $s_{i}$ is ’(’, let $up_{i}$ be smallest matched parentheses contain $s_{i}$ and $s_{match_{i}}$
$ans_{i}=ans_{match_{i}}=ans_{up_{i}}+a_{i}*b_{match_{i}}$
both $match_{i}$ and $up_{i}$ can be computed by using a stack.
Time complexity: $O(n)$. ##[1006.First One](http://acm.hdu.edu.cn/showproblem.php?pid=5358) Summary For a given sequence $a_{1}$, $a_{2}$, . . . , $a_{n}$, we want to find the value of $\sum_{i=1}^{n} \sum_{j=i}^{n}(\left \lfloor log_{2} S(i ,j)\right \rfloor+1)*(i+j)$ Whrer S(i ,j) is the sum of $a_{i}$,$a_{i+1}$,…,$a_{j}$ Solution
$\left \lfloor log_{2}x \right \rfloor +1$ is the number of bits in the binary representation of $x$.
Consider the contribution of $k$-th bit: if $S(i, j)\geq 2^{k}$ ,$ i + j$ is added to the answer.
Enumerate $k$-th bit, and use two pointers to maintain the contribution.
Time complexity: $O(n\ log\ n)$. ##[1007.Group](http://acm.hdu.edu.cn/showproblem.php?pid=5359) Summary We are given an directed graph $G$ with $n$ vertices and $m$ edges. You are to determine for each edge $i$,after deleting it, whether the number of strongly connected components increases. Solution Let us first solve the vertex deleting one:
Find all the strongly connected components, each strongly connected component can be considered independently.
Let $G = (V,E)$ is a strongly connected graph, $G^{R} = (V,E^{R})$ is the reversal graph of $G$ (if $(u, v)$ in $G$ then $(v, u)$ in $G^{R}$), $G(s) = (V,E, s)$ be the flowgraph with start vertex $s$,$D(s)$ the set of non-trivial dominators in $G(s)$, $G^{R}(s) = (V,E^{R}, s)$ be the flowgraph with start vertex $s$, $D^{R}(s)$ the set of non-trivial dominators in $G^{R}(s)$.
Then vertex $v\neq s$ is a strong articulation point in $G$ if and only if $v \in D(s)\cup D^{R}(s)$.
proving it is not easy, you may google it if you have interest.
Now let us solve the edge deleting one: for each edge $x\rightarrow y$, add an extra vertex $z$ between the edge, $x \rightarrow z \rightarrow y$. We can use the mothod above to solve it. Time complexity: $O((n + m) \alpha (n + m))$ ##[1008.Hiking](http://acm.hdu.edu.cn/showproblem.php?pid=5360) Summary There are $n$ people and $i$-th people will go out if the number of people going out if no less than $l_{i} + 1$ and no larger than $r_{i} + 1$. You need to invite them one by one to make the number of people going out as large as possible. Solution
sort all the people according to $l_{i}$ and $r_{i}$.
maintain an integer $cur$ as the number of people agree to go out yet. For all the people will agree to go out, pick the one with smallest $r$.
Time complexity: $O(n\ log\ n)$. ##[1009.In Touch](http://acm.hdu.edu.cn/showproblem.php?pid=5361) Summary $n$ vertices are in a straight line and $i$-th vertex is in position $i$. $i$-th vertex can teleport to other vertices whose distance between $i$-th vertex is no less than $l_{i}$ and no larger than $r_{i}$ with cost $c_{i}$. For each vertex,find the minimum cost from 1-st vertex to it. Solution
let $d_{i}$ be the cost entering vertex $i$.
use dijkstra to calculate $d_{i}$ and use disjoint-set to maintain the vertex not visited yet.
the answer is $d_{i}-c_{i}$.
Time complexity: $O(n\ log\ n)$. ##[1010.Just A String](http://acm.hdu.edu.cn/showproblem.php?pid=5362) Summary You are given a random string of length $n$ and an alphabet of size $m$. You are to tell the expected number of such substrings that can be rearranged to a palindrome in the random string. Solution
The answer is obvious after enumerating the length of the substring:
$\sum_{l=1}^{n}(n-l+1)m^{n-l}T(l,m)$ where $T(l,m)$ is the number of such string with length $l$ and alphabet of size $m$.
$l$ can be odd or even, let’s solve the even case first:
——$T(2n,m)=\sum_{i=0}^{n}(2i,m-1)* \begin{pmatrix}2n\\2i \end{pmatrix}$ ——$\frac {T(2n,m)}{(2n)!}=\sum_{i=0}^{n}\frac {T(2i,m-1)}{(2i)!}*\frac{1}{(2(n-i))!}$ ——Let $G_{m}(x)$ be the generating function for $\frac{T(2n,m)}{(2n)!}$ , F(x) be the generating function for $\frac {1}{(2n)!}$. Then $F(x)=cosh \sqrt {x}=\frac{e^{\sqrt{x}}+e^{-\sqrt{x}}}{2} $and $G_{m}(x)=F^{m}(x)=( \frac{e^{\sqrt{x}}+e^{-\sqrt{x}}}{2})^{m}$. ——$T(2n,m)$ is the coefficient of $n$-th item of $G_{m}(x)$, after doing some calculation, we have $T(2n,m)=\sum_{i=0}^{\frac{m-1}{2}}\frac{\begin{pmatrix}m\\i \end{pmatrix}*(m-2i)^{2n}}{2^{m-1}}$ For a fixed $n$, which can be computed in $O(m)$ time.
Now it is time for the odd $l$:
$T(2n+1,m)=\sum_{i=0}^{n}T(2i,m-1)* \begin{pmatrix}2n+1\\2i \end{pmatrix}$ If we precompute the value for $T(2i,m - 1)$ then $T(2n + 1,m)$ can be computed in $O(n)$ time. Time complexity: $O(n(m + n))$. ##[1011.Key Set](http://acm.hdu.edu.cn/showproblem.php?pid=5363) Summary For a given set {1, 2, . . . , $n$}, you are to find the number of nonempty subsets with even sum. Solution Let $a$ be the number of even integers and $b$ be the number of even integers. The answer is: $2^{a}(\begin{pmatrix}b\\0 \end{pmatrix}+\begin{pmatrix}b\\2 \end{pmatrix}+$…$)=2^{a+b-1}=2^{n-1}-1$ Time complexity: $O(log\ n)$