Network
Time limit:2000 ms |
|
Memory limit:65536 K |
Total submissions:2496 |
|
Accepted:726 |
Description
Yixght is a manager of the company called szqnetwork (SN ). now she's very worried because she has just received ed a bad news which denotes that dxtnetwork (DN), the SN's business rival, intents to attack the network of Sn. more unfortunately, the original network
OF Sn is so weak that we can just treat it as a tree. Formally, there areNNodes in SN's network,N-1 bidirectional channels to connect the nodes, and there always exists a route from any node to another. In order to protect the network
From the attack, yixght buildsMNew bidirectional channels between some of the nodes.
As the DN's best hacker, you can exactly destory two channels, one in the original network and the other amongMNew channels. Now your higher-up wants to know how many ways you can divide the network of Sn into at least two parts.
Input
The first line of the input file contains two integers:N(1 ≤N≤ 100 000 ),M(1 ≤M≤ 100 000)-the number of the nodes and the number of the new channels.
FollowingN-1 lines represent the channels in the original network of Sn, each pair (A,B) Denote that there is a channel between nodeAAnd NodeB.
FollowingMLines represent the new channels in the network, each pair (A,B) Denote that a new channel between nodeAAnd NodeBIs added to the network of Sn.
Output
Output a single integer-the number of ways to divide the network into at least two parts.
Sample Input
4 11 22 31 43 4
Sample output
3
Source
Poj monthly -- 2007.10.06, Yang Mu question: http://poj.org/problem? Id = 3417 analysis: This question will soon be able to count the number of times the edge is covered by the ring. If the number of covers is 0, the side is the bridge. The answer is + m, and the number of covers is one, it indicates that it belongs to a ring with a new edge. The answer is + 1... Because the fathers of the common ancestor are not in the circle of their children, the number of times the common ancestor of the newly added edge must be-2... I have finished speaking. For more information, see Code ~~~ In the case that X = y exists in the newly added edge, the WAF has not been sentenced to n times ~~~ Code:
# Include <cstdio> using namespace STD; const int Mm = 444444; const int Mn = 111111; int T [mm], p [mm]; int H [Mn] = {0}, Q [Mn] = {0}, F [Mn], MK [Mn] = {0 }; bool vis [Mn] = {0}; int I, j, k, n, m, E, ANS = 0; inline void add (INT U, int V, int H []) {T [e] = V, p [e] = H [u], H [u] = e ++; t [e] = u, P [e] = H [v], H [v] = e ++;} int find (int x) {If (F [x] = x) return X; return f [x] = find (F [x]);} void Tarjan (int u) {int I, V; vis [f [u] = 1; for (I = Q [u]; I; I = P [I]) if (vis [V = T [I]) MK [find (v)]-= 2; for (I = H [u]; I; I = P [I]) if (! Vis [V = T [I]) {Tarjan (V), f [v] = u, MK [u] + = Mk [v]; if (MK [v] = 1) ++ ans; If (MK [v] = 0) ans + = m ;}} void get (Int &) {char C; while (C = getchar () <'0' | C> '9'); for (a = 0; c> = '0' & C <= '9'; C = getchar () A = A * 10 + C-'0';} int main () {Get (N), get (m); For (E = k = 1; k <n; ++ K) Get (I), get (J ), add (I, j, H); For (k = 1; k <= m; ++ K) {Get (I), get (j); if (I! = J) add (I, j, q), ++ MK [I], ++ MK [J];} Tarjan (1 ); printf ("% d \ n", ANS); Return 0 ;}