http://poj.org/problem?id=3417

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 5294   Accepted: 1517

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

3

Source

 
 
n-1条边构成的树,m条边中每加一条比构成环,假设新边为(u,v),那么环为u->LCA(u,v)->v->u,我们给这个环上的边计数1,表示这些边被一个环覆盖了一次。
可以发现

1.覆盖0次,说明这条边不在任何一个环上,这样的边最脆弱,单单是毁掉它就已经可以使树断裂了,这时候只要任意选一条新边去毁,树还是断裂的,所以这样的树边,就产生m种方案(m为新边条数)

2.覆盖1次,说明这条边在一个环上,且,仅在一个环上,那么要使树断裂,就毁掉这条树边,并且毁掉和它对应的那条新边(毁其他的新边无效),就一定能使树断裂,这种树边能产生的方案数为1,一条这样的树边只有唯一解

3.覆盖2次或以上,无论怎么样都不能使树断裂,产生的方案数为0

树上查分维护覆盖次数,树剖求得lca

 #include <cstdio>

 #define swap(a,b) {int tmp=a;a=b;b=tmp;}
inline void read(int &x)
{
x=; register char ch=getchar();
for(; ch>''||ch<''; ) ch=getchar();
for(; ch>=''&&ch<=''; ch=getchar()) x=x*+ch-'';
} const int N();
int head[N],sumedge;
struct Edge {
int v,next;
Edge(int v=,int next=):v(v),next(next){}
}edge[N<<];
inline void ins(int u,int v)
{
edge[++sumedge]=Edge(v,head[u]); head[u]=sumedge;
edge[++sumedge]=Edge(u,head[v]); head[v]=sumedge;
} int top[N],size[N],dep[N],son[N],dad[N];
void DFS(int u,int depth)
{
size[u]=,dep[u]=depth;
for(int v,i=head[u]; i; i=edge[i].next)
{
v=edge[i].v;
if(v==dad[u]) continue;
dad[v]=u; DFS(v,depth+); size[u]+=size[v];
if(size[son[u]]<size[v]) son[u]=v;
}
}
void DFS_(int u,int Top)
{
top[u]=Top;
if(son[u]) DFS_(son[u],Top);
for(int v,i=head[u]; i; i=edge[i].next)
{
v=edge[i].v;
if(v!=dad[u]&&v!=son[u]) DFS_(v,v);
}
}
inline int LCA(int x,int y)
{
for(; top[x]!=top[y]; x=dad[top[x]])
if(dep[top[x]]<dep[top[y]]) swap(x,y);
return dep[x]<dep[y]?x:y;
} long long val[N],ans;
void DFSVAL(int u)
{
for(int i=head[u]; i; i=edge[i].next)
if(edge[i].v!=dad[u]) DFSVAL(edge[i].v),val[u]+=val[edge[i].v];
} int Presist()
{
int n,m; read(n),read(m);
for(int u,v,i=; i<n; ++i)
read(u),read(v),ins(u,v);
DFS(,); DFS_(,);
for(int lca,u,v,i=; i<=m; ++i)
{
read(u),read(v);
lca=LCA(u,v);
val[u]++,val[v]++;
val[lca]-=;
}
DFSVAL();
for(int i=; i<=n; ++i)
if(val[i]==) ans++;
else if(!val[i]) ans+=m;
printf("%lld\n",ans);
return ;
} int Aptal=Presist();
int main(int argc,char*argv[]){;}

POJ——T3417 Network的更多相关文章

  1. POJ 1236 Network of Schools(强连通 Tarjan+缩点)

    POJ 1236 Network of Schools(强连通 Tarjan+缩点) ACM 题目地址:POJ 1236 题意:  给定一张有向图,问最少选择几个点能遍历全图,以及最少加入�几条边使得 ...

  2. ZOJ 1542 POJ 1861 Network 网络 最小生成树,求最长边,Kruskal算法

    题目连接:problemId=542" target="_blank">ZOJ 1542 POJ 1861 Network 网络 Network Time Limi ...

  3. POJ 1236 Network of Schools(强连通分量)

    POJ 1236 Network of Schools 题目链接 题意:题意本质上就是,给定一个有向图,问两个问题 1.从哪几个顶点出发,能走全全部点 2.最少连几条边,使得图强连通 思路: #inc ...

  4. poj 3417 Network(tarjan lca)

    poj 3417 Network(tarjan lca) 先给出一棵无根树,然后下面再给出m条边,把这m条边连上,然后每次你能毁掉两条边,规定一条是树边,一条是新边,问有多少种方案能使树断裂. 我们设 ...

  5. Poj 3694 Network (连通图缩点+LCA+并查集)

    题目链接: Poj 3694 Network 题目描述: 给出一个无向连通图,加入一系列边指定的后,问还剩下多少个桥? 解题思路: 先求出图的双连通分支,然后缩点重新建图,加入一个指定的边后,求出这条 ...

  6. Poj 1236 Network of Schools (Tarjan)

    题目链接: Poj 1236 Network of Schools 题目描述: 有n个学校,学校之间有一些单向的用来发射无线电的线路,当一个学校得到网络可以通过线路向其他学校传输网络,1:至少分配几个 ...

  7. POJ 1236 Network of Schools(Tarjan缩点)

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16806   Accepted: 66 ...

  8. [双连通分量] POJ 3694 Network

    Network Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 9434   Accepted: 3511 Descripti ...

  9. poj 1144 Network 图的割顶判断模板

    Network Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8797   Accepted: 4116 Descripti ...

随机推荐

  1. 微信小程序资源

    1.http://blog.csdn.net/wyx100/article/details/52667518 2.http://mp.weixin.qq.com/s?__biz=MzIyMDM2Mjg ...

  2. Bryce1010 Acm模板

    目录 STL标准模板库 STL简介 STL pair STL set STL vector STL string STL stack STL queue STL map upper_bound和low ...

  3. Previous operation has not finished; run 'cleanup' if it was interrupted.SVN报错

    原因: 错误的原因是SVN管理的文件夹改名.删除文件太过频繁,导致有操作挂起了. 解决方式: 1.百度sqlite3.exe并下载. 2.将该文件解压到项目根目录下的.svn文件夹中. 3.打开cmd ...

  4. Spring Boot (29) 定时任务

    使用场景:数据定时增量同步,定时发送邮件,爬虫定时抓取 定时任务概述 定时任务:顾名思义就是在特定/指 定的时间进行工作,比如我们的手机闹钟,他就是一种定时的任务. 实现方式: 1.Timer:JDK ...

  5. bash 博弈

    转载并修改自: http://www.cnblogs.com/wulangzhou/archive/2013/03/14/2959660.html 简单的取拿游戏一堆石子(或者其它的什么东西),下面是 ...

  6. (1)麻省理工:计算机科学和 Python 编程导论

    本门课用的语言是python2.7,我的主要学习语言是C++11,所以不是特殊说明,则认为和C++中的是一样的(不管是语法还是表达式),当然,也有我不懂而错认为与C++一样的东西~ Week1 第一讲 ...

  7. iOS显示一张图片 Objective-C

    图片文件放在项目目录下 #import "ViewController.h" @interface ViewController () @end @implementation V ...

  8. 【C++】朝花夕拾——树(开篇)

    树 ===================我是分割线====================== 1. 定义: 一些结点的集合,集合可以为空.定义树的自然方式是递归的方法. 2. 相关概念: 根(ro ...

  9. HDU_1874_畅通工程续_最短路问题

    畅通工程续 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  10. MFC_2.1使用单选和多选框

    使用单选和多选框 单选 1.拖控件 设置名字,CTRL+D设置顺序,属性设置第一个GROUP为TRUE: 2.设置第一个按钮绑定变量为 值 INT型 名称m_RadioIndxe; 3.设置单击响应内 ...