POJ——T3417 Network
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
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的更多相关文章
- POJ 1236 Network of Schools(强连通 Tarjan+缩点)
POJ 1236 Network of Schools(强连通 Tarjan+缩点) ACM 题目地址:POJ 1236 题意: 给定一张有向图,问最少选择几个点能遍历全图,以及最少加入�几条边使得 ...
- ZOJ 1542 POJ 1861 Network 网络 最小生成树,求最长边,Kruskal算法
题目连接:problemId=542" target="_blank">ZOJ 1542 POJ 1861 Network 网络 Network Time Limi ...
- POJ 1236 Network of Schools(强连通分量)
POJ 1236 Network of Schools 题目链接 题意:题意本质上就是,给定一个有向图,问两个问题 1.从哪几个顶点出发,能走全全部点 2.最少连几条边,使得图强连通 思路: #inc ...
- poj 3417 Network(tarjan lca)
poj 3417 Network(tarjan lca) 先给出一棵无根树,然后下面再给出m条边,把这m条边连上,然后每次你能毁掉两条边,规定一条是树边,一条是新边,问有多少种方案能使树断裂. 我们设 ...
- Poj 3694 Network (连通图缩点+LCA+并查集)
题目链接: Poj 3694 Network 题目描述: 给出一个无向连通图,加入一系列边指定的后,问还剩下多少个桥? 解题思路: 先求出图的双连通分支,然后缩点重新建图,加入一个指定的边后,求出这条 ...
- Poj 1236 Network of Schools (Tarjan)
题目链接: Poj 1236 Network of Schools 题目描述: 有n个学校,学校之间有一些单向的用来发射无线电的线路,当一个学校得到网络可以通过线路向其他学校传输网络,1:至少分配几个 ...
- POJ 1236 Network of Schools(Tarjan缩点)
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16806 Accepted: 66 ...
- [双连通分量] POJ 3694 Network
Network Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 9434 Accepted: 3511 Descripti ...
- poj 1144 Network 图的割顶判断模板
Network Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 8797 Accepted: 4116 Descripti ...
随机推荐
- java 配置信息类 Properties 的简单使用
Properties :(配置信息类) 是一个表示持久性的集合 ,继承 Hashtable ,存值是以键-值得方式 主要用于生产配置文件和读取配置文件信息. 简单的实例: import java.i ...
- [Python实战] 功能简单的数据查询及可视化系统
前言 数据时代,数据的多源集成和快速检索查询是第一步,配上数据分析及可视化才能算窥得大数据一角. 创建这个项目的主要目的一是对前期工作的一些总结,二是提升自己. 这里简单介绍一下sqlpro这个项目的 ...
- VS中设置xml智能提示
1.第一步:将xsd文件复制到VS的安装目录下 默认安装目录为:C:\Program Files (x86)\Microsoft Visual Studio 12.0\Xml\Schemas 2.第二 ...
- 如何向expect脚本里面传递参数
如何向expect脚本里面传递参数 比如下面脚本用来做ssh无密码登陆,自动输入确认yes和密码信息,用户名,密码,hostname通过参数来传递 ssh.exp Python代码 # ...
- 关于加减和es6
console.log(1+ "2"+"2"); //对于加法来说,如果只有一个操作数是字符串,则将另一个操作数也转换为字符串,然后将两者拼接,为122 c ...
- Pro ASP.Net Core MVC 6th 第四章
第四章 C# 关键特征 在本章中,我描述了Web应用程序开发中使用的C#特征,这些特征尚未被广泛理解或经常引起混淆. 这不是关于C#的书,但是,我仅为每个特征提供一个简单的例子,以便您可以按照本书其余 ...
- 三维卷积:全景图像Spherical CNNs(Code)
卷积神经网络(CNN)可以很好的处理二维平面图像的问题.然而,对球面图像进行处理需求日益增加.例如,对无人机.机器人.自动驾驶汽车.分子回归问题.全球天气和气候模型的全方位视觉处理问题. 将球形信号的 ...
- 部署bugzilla(bugzilla+apache+mysql+linux)
工作原因,需要部署bugzilla.在此,容我新造个轮子.官方轮子:https://bugzilla.readthedocs.org/en/latest/installing/quick-start. ...
- (转)淘淘商城系列——使用Jedis操作集群
http://blog.csdn.net/yerenyuan_pku/article/details/72862084 通过上文的学习,我相信大家应该已经知道如何搭建Redis集群了,本文我将为大家介 ...
- c/c++编程排坑(1)-- 数据类型的“安静”转换
这里主要介绍ANSI C的特性:当执行算术运算时,操作数的类型如果不同,就会发生转换.数据类型一般朝着精度更高.长度更长的方向转换,整型数如果转换为signed不会丢失信息,就转换为signed,否则 ...