poj3417 LCA + 树形dp
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 4478 | Accepted: 1292 |
Description
Yixght is a manager of the company called SzqNetwork(SN). Now she's very worried because she has just received 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 are N nodes 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 builds M new 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 among the M new 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.
Following N-1 lines represent the channels in the original network of SN, each pair (a,b) denote that there is a channel between node a and node b.
Following M lines represent the new channels in the network, each pair (a,b) denote that a new channel between node a and node b is 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 1 1 2 2 3 1 4 3 4
Sample Output
/* * Author: sweat123 * Created Time: 2016/7/14 8:29:55 * File Name: main.cpp */ #include<set> #include<map> #include<queue> #include<stack> #include<cmath> #include<string> #include<vector> #include<cstdio> #include<time.h> #include<cstring> #include<iostream> #include<algorithm> #define INF 1<<30 #define MOD 1000000007 #define ll long long #define lson l,m,rt<<1 #define key_value ch[ch[root][1]][0] #define rson m+1,r,rt<<1|1 #define pi acos(-1.0) using namespace std; const int MAXN = 100010; struct node{ int to; int next; }edge[MAXN*2]; int pre[MAXN],ind,vis[MAXN],dfn[MAXN*2],dp[MAXN*2][20],rev[MAXN*2],tot,first[MAXN],n,m; int s[MAXN]; void add(int x,int y){ edge[ind].to = y; edge[ind].next = pre[x]; pre[x] = ind ++; } void dfs(int rt,int dep){ vis[rt] = 1; rev[++tot] = rt; first[rt] = tot; dfn[tot] = dep; for(int i = pre[rt]; i != -1; i = edge[i].next){ int t = edge[i].to; if(!vis[t]){ dfs(t,dep+1); rev[++tot] = rt; dfn[tot] = dep; } } } void rmq(){ for(int i = 1; i <= tot; i++){ dp[i][0] = i; } for(int i = 1; i < 20; i++){ for(int j = 1; j + (1 << i) - 1 <= tot; j++){ int l = dp[j][i-1]; int r = dp[j+(1<<(i-1))][i-1]; if(dfn[l] > dfn[r]){ dp[j][i] = r; } else{ dp[j][i] = l; } } } } int lca(int x,int y){ x = first[x]; y = first[y]; if(x > y)swap(x,y); int k = (int)(log(y - x + 1) * 1.0 / log(2.0)); int l = dp[x][k]; int r = dp[y - (1 << k) + 1][k]; if(dfn[l] > dfn[r])return r; return l; } void tree_dfs(int rt){ vis[rt] = 1; for(int i = pre[rt]; i != -1; i = edge[i].next){ int t = edge[i].to; if(!vis[t]){ tree_dfs(t); s[rt] += s[t]; } } } void init(){ ind = tot = 0; memset(pre,-1,sizeof(pre)); memset(s,0,sizeof(s)); } int main(){ while(~scanf("%d%d",&n,&m)){ init(); for(int i = 1; i < n; i++){ int x,y; scanf("%d%d",&x,&y); add(x,y); add(y,x); } memset(vis,0,sizeof(vis)); dfs(1,1); rmq(); for(int i = 1; i <= m; i++){ int x,y; scanf("%d%d",&x,&y); int tp = rev[lca(x,y)]; s[x] ++; s[y] ++; s[tp] -= 2;// the ponint of lca(x,y) has no birdge and I hava count two way } memset(vis,0,sizeof(vis)); tree_dfs(1); ll ans = 0; for(int i = 2; i <= n; i++){ if(s[i] <= 0){ ans += m; } else if(s[i] == 1){ ans += 1; } } printf("%lld\n",ans); } return 0; }