题目传送门

  题目大意:给出一棵树,再给出m条非树边,先割掉一条树边,再割掉一条非树边,问有几种割法,使图变成两部分。

  思路:每一条 非树边会和一部分的树边形成一个环,分三种情况:

    对于那些没有形成环的树边来说,割掉这条边,就已经使图分离,然后随便割一条非树边就可以了,所以这样的边每次答案加上m。

    对于那些只存在在一个环中的树边来说,割掉这条边,再割一条和他存在于同一个环中的那条非树边,也能合法,所以加一。

    对于存在于多个环中的树边,无论怎样,都无法合法。

    也就是此时我们将题目转化成了树上的覆盖问题,非树边的两个端点覆盖的树上简单路径就是一个环,我们用树上差分来解决这个问题,先处理出lca,f数组表示每个点和父节点所在的边的覆盖次数。

    对于每一条非树边,端点是u,v,则f[ u ]和 f [ v ]加一,而u和v的公共祖先是没有影响的,所以f[ lca (u,v) ]要减去2,抵消这个影响。每个节点除了要加上直接对这个点操作的值,还要加上来自子树是否有覆盖,所以dfs求得子树的大小,此时 f 数组则代表每个点的父边所覆盖的次数。

  

//#include<bits/stdc++.h>
#include<cstdio>
#include<algorithm>
#include<string.h>
#include<queue>
#include<iostream>
#define CLR(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
int n,m;
const int maxn=;
struct edges{
int to ,Next;
}a[maxn<<];
int head[maxn],tot,fa[maxn][],deg[maxn];
ll f[maxn];
inline void init(){
CLR(head,-),tot=;
CLR(f,);
}
inline void addv(int u,int v){
a[++tot]={v,head[u]};
head[u]=tot;
}
inline void bfs(){
queue<int >q;
deg[]=;
fa[][]=;
q.push();
while(!q.empty()){
int tmp=q.front();
q.pop();
for(int i=;i<;i++)
{
fa[tmp][i]= fa[fa[tmp][i-]][i-];
}
for(int i=head[tmp];i!=-;i=a[i].Next)
{
int v=a[i].to;
if(v==fa[tmp][])continue;
deg[v]=deg[tmp]+;
fa[v][]=tmp;
q.push(v);
}
}
}
inline int lca(int u,int v){
if(deg[u] > deg[v])swap(u,v);
int hu=deg[u],hv=deg[v];
int tu=u,tv=v;
for(int det=hv-hu,i=;det;det>>=,i++)
if(det&)
tv=fa[tv][i];
if(tu==tv)return tu;
for(int i=;i>=;i--){
if(fa[tu][i]==fa[tv][i])
continue;
tu=fa[tu][i];
tv=fa[tv][i];
}
return fa[tu][]; }
inline void dfs(int u){
for(int i=head[u];i!=-;i=a[i].Next)
{
int v=a[i].to;
if(v!=fa[u][]){
dfs(v);
f[u]+=f[v];
}
}
}
int main(){
init();
cin>>n>>m;
for(int i=;i<n;i++)
{
int u,v;
scanf("%d%d",&u,&v);
addv(u,v);
addv(v,u);
}
bfs();
for(int i=;i<=m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
f[u]++;
f[v]++;
f[lca(u,v)]-=;
// printf("lca %d\n",lca(u,v));
}
dfs();
ll ans=;
for(int i=;i<=n;i++)
{
// cout<<i<<" "<<f[i]<<endl;
if(f[i]==){
ans+=m;
}else if(f[i]==){
ans++;
}
}
cout<<ans<<endl; }
Network
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 6736   Accepted: 1916

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

poj3417 Network 树上差分+LCA的更多相关文章

  1. 树上差分 (瞎bb) [树上差分][LCA]

    做noip2015的运输计划写了好久好久写不出来   QwQ 于是先来瞎bb一下树上差分    混积分 树上差分有2个常用的功能: (1)记录从点i到i的父亲这条路径走过几次 (2)将每条路径(s,t ...

  2. 【COGS 2434】 暗之链锁 树上差分+LCA

    差分就是把一个值拆成许多差的和如 1 2 4 6 9 那么 把这个东西拆成 1 1 2 2 3 就是了,当然也可以理解为对一个问题分解为多个子问题并对其进行操作来得到原问题的答案. 树上差分就更玄妙了 ...

  3. BZOJ 4326 NOIP2015 运输计划(树上差分+LCA+二分答案)

    4326: NOIP2015 运输计划 Time Limit: 30 Sec  Memory Limit: 128 MB Submit: 1388  Solved: 860 [Submit][Stat ...

  4. [NOIP2015]运输计划(树上差分+LCA+二分)

    Description 公元 2044 年,人类进入了宇宙纪元. L 国有 n 个星球,还有 n−1 条双向航道,每条航道建立在两个星球之间,这 n−1 条航道连通了 L 国的所有星球. 小 P 掌管 ...

  5. NOIP2016 Day1 T2 天天爱跑步(树上差分,LCA)

    原文链接 原题链接 题目描述 小c同学认为跑步非常有趣,于是决定制作一款叫做<天天爱跑步>的游戏.<天天爱跑步>是一个养成类游戏,需要玩家每天按时上线,完成打卡任务. 这个游戏 ...

  6. poj3417 Network 树形Dp+LCA

    题意:给定一棵n个节点的树,然后在给定m条边,去掉m条边中的一条和原树中的一条边,使得树至少分为两部分,问有多少种方案. 神题,一点也想不到做法, 首先要分析出加入一条边之后会形成环,形成环的话,如果 ...

  7. BZOJ 3631: [JLOI2014]松鼠的新家 树上差分 + LCA

    Description 松鼠的新家是一棵树,前几天刚刚装修了新家,新家有n个房间,并且有n-1根树枝连接,每个房间都可以相互到达,且俩个房间之间的路线都是唯一的.天哪,他居然真的住在“树”上.松鼠想邀 ...

  8. POJ3417(树上差分)

    会卡vector. ; int n, m, Ans; ], to[maxn * ], tot; int vis[maxn], f[maxn]; int d[maxn], sum[maxn]; vect ...

  9. Wannafly Camp 2020 Day 1E 树与路径 - 树上差分,LCA

    #include <bits/stdc++.h> using namespace std; #define int long long const int N = 1000005; vec ...

随机推荐

  1. 581. Shortest Unsorted Continuous Subarray连续数组中的递增异常情况

    [抄题]: Given an integer array, you need to find one continuous subarray that if you only sort this su ...

  2. shell动态替换变量

    可以对env指定U 或者 L,  得到U或者L下的IP地址列表, env可作为shell的参数列表传入 env=UL_ips=(x.x.x.x  y.y.y.y)U_ips=(w.w.w.w  z.z ...

  3. JUnit 两日游

    从一个简单的Junit demo到一个用户名与密码的验证 学习Junit,首先肯定是要环境的搭建与配置. 第一步,安装JDK,配置环境变量 第二步,安装eclipse.OS X环境下,eclipse并 ...

  4. 黑盒测试实践-小组任务分配和安排-Day01

    实践作业2:黑盒测试实践 小组成员 组长:华同学 成员:覃同学.沈同学.刘同学.郭同学.穆同学 任务分解 任务1:分析系统需求 参考软件需求规格说明书模板描述系统需求.如果系统功能特别多,特别复杂,则 ...

  5. C# 关于接口与基类的理解(二者的区别)

    接口(接口的名称一般用大写字母I开头的)是把公共实例(非静态)方法和属性组合起来,以封装特定功能的一个集合.(其实,接口简单理解就是一种约定,使得实现接口的类或结构在形式上保持一致) 注意:使用接口可 ...

  6. 编写高质量代码改善C#程序的157个建议——建议25:谨慎集合属性的可写操作

    建议25:谨慎集合属性的可写操作 如果类型的属性中有集合属性,那么应该保证属性对象是由类型本身产生的.如果将属性设置为可写,则会增加抛出异常的几率.一般情况下,如果集合属性没有值,则它返回的Count ...

  7. 《PRC:更新项目汇总额》报错

    请求报红,日志如下: +---------------------------------------------------------------------------+ 项目: Version ...

  8. MVC 基本概念

    1. M(Model-模型): 数据模型. 负责定义信息格式与信息反问的界面,包括商业逻辑,数据反问.(可以理解成是三层模式中的 BLL+DAL ) 2. V(View-视图): 负责用户界面 UI ...

  9. [转]-webkit-overflow-scrolling:touch的应用

    -webkit-overflow-scrolling 用来控制元素在移动设备上是否使用滚动回弹效果. 在移动端上,在你用overflow-y:scorll属性的时候,你会发现滚动的效果很木,很慢,这时 ...

  10. asp.net mvc 中通过url字符串获取controller和action

    在项目中遇到需要通过url字符串获取controller和action的情况,百度了 一下找到了一个可以用的方法 ,在这里分享和记录一下 这个方法是在博客园的博问里看到的 原文地址是http://q. ...