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
3
题意:
一棵树,后来加了m条新边,形成了一个有环无向图,问删去原树上一条边和m条新边中的一条,使得图不连通。这样的方案有多少。
思路:
以前看到过,完全没有思路,放弃了。今天又看到了,还是没有思路。。。GG
把原图dfs建立成有根树,假设1是根节点,再想,有方向可能就有思路了,oh,还是没有。。。GG
- 试着手动在两点之间加一条新边,我们看到形成了一个环。
- 对于环上加的一条新边,两点间每一条原树边都多进入了一个环。
- 1, 如果一条边进入了两个环,则不用再考虑它。
- 2, 如果只进入一个环,则它与对应的新边是一组答案。
- 3, 如果一条表没有进入任何环,那它与任何一条新边是一组答案。
- 对每一条新边e,其端点为u,v,LCA(u,v)=a,则把路径u-a-v上每一条边加1,最后处理即可。
- 显然可以用线段树+lazy做。数据上感觉可以过。但是学到了树上差分。。。像前缀和一样。感觉很简单,但是没想到。。。
:sum[u]+1;sum[v]+1;sum[a]-2; 由叶子向根root累加即可。
注意:对象是边,而不是顶点的sum[]。
我写的是树剖(因为在练习树剖),注意找LCA的时候注意 if(dpt[u]<dpt[v]) 是刷新。
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<cstring>
#include<algorithm>
#define swap(a,b) {a^=b;b^=a;a^=b;}
using namespace std;
const int maxn=;
int Laxt[maxn],Next[maxn],To[maxn],cnt;
int fa[maxn],son[maxn],size[maxn],dpt[maxn],top[maxn];
int n,m,ans,sum[maxn];
void add(int u,int v)
{
Next[++cnt]=Laxt[u];
Laxt[u]=cnt; To[cnt]=v;
}
void dfs1(int u,int pre)
{
fa[u]=pre;dpt[u]=dpt[pre]+;size[u]=;son[u]=;
for(int i=Laxt[u];i;i=Next[i]){
int v=To[i]; if(v==pre) continue;
dfs1(v,u);size[u]+=size[v];
if(!son[u]||size[v]>size[son[u]]) son[u]=v;
}
}
void dfs2(int u,int Top)
{
top[u]=Top; if(!son[u]) return ; dfs2(son[u],Top);
for(int i=Laxt[u];i;i=Next[i])
if(To[i]!=fa[u]&&To[i]!=son[u]) dfs2(To[i],To[i]);
} int dfs3(int u,int pre)
{
for(int i=Laxt[u];i;i=Next[i]){
int v=To[i];
if(v!=pre){
int tmp=dfs3(v,u);
sum[u]+=tmp;
if(tmp==) ans+=m;
if(tmp==) ans+=;
}
}
return sum[u];
}
int find(int u,int v)
{
int a=top[u],b=top[v];
while(a!=b){
if(dpt[a]<dpt[b]) { swap(a,b);swap(u,v);}
u=fa[a]; a=top[u];
}
if(dpt[u]<dpt[v]) return u; return v;
}
int main()
{ while(~scanf("%d%d",&n,&m)){
memset(Laxt,,sizeof(Laxt)); cnt=;
memset(sum,,sizeof(sum)); ans=;
for(int i=;i<n;i++){
int v,u; scanf("%d%d",&u,&v);
add(u,v);add(v,u);
}
dfs1(,);
dfs2(,);
for(int i=;i<=m;i++){
int u,v; scanf("%d%d",&u,&v);
int anc=find(u,v);
sum[u]++;sum[v]++;sum[anc]-=;
}
dfs3(,);
printf("%d\n",ans);
} return ;
}