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

根据一条边被几个环覆盖来判断能不能删、有几种情况等;

用树上差分,终点 s++,LCA s-=2,统计时计算子树s值的和即可;

用ST表做LCA,不知为何WA了:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int const MAXN=1e5+;
int n,m,head[MAXN],ct,dep[MAXN],pre[MAXN][],s[MAXN];
long long ans;
struct N{
int to,next;
N(int t=,int n=):to(t),next(n) {}
}edge[MAXN<<];
void dfs(int x,int f)
{
dep[x]=dep[f]+;
pre[x][]=f;
for(int i=head[x];i;i=edge[i].next)
if(edge[i].to!=f)dfs(edge[i].to,x);
}
void init()
{
dfs(,);
for(int k=;k<=;k++)
for(int i=;i<=n;i++)
pre[i][k]=pre[pre[i][k-]][k-];
}
int lca(int x,int y)
{
if(dep[x]>dep[y])swap(x,y);
int d=dep[y]-dep[x];
for(int i=;i<=;i++)
if(/*d&(1<<i)*/(d>>i)&)y=pre[y][i];
for(int i=;i>=;i--)
if(pre[x][i]!=pre[y][i])
{
x=pre[x][i];
y=pre[y][i];
}
return pre[x][];
}
long long dfs2(int x,int f)
{
long long sum=s[x];
for(int i=head[x],u;i;i=edge[i].next)
{
if((u=edge[i].to)==f)continue;
long long k=dfs2(u,x);
if(k==)ans+=m;
if(k==)ans++;
sum+=k;
}
return sum;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=,x,y;i<n;i++)
{
scanf("%d%d",&x,&y);
edge[++ct]=N(y,head[x]);head[x]=ct;
edge[++ct]=N(x,head[y]);head[y]=ct;
}
init();
for(int i=,x,y;i<=m;i++)
{
scanf("%d%d",&x,&y);
if(x==y)continue;
s[x]++;s[y]++;
s[lca(x,y)]-=;
}
dfs2(,);
printf("%lld",ans);
return ;
}

ST表为什么WA

于是改成了tarjan,过程中求答案;

注意非树边加边时判掉x=y的情况。

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int const MAXN=1e5+;
int n,m,head[MAXN],ct,s[MAXN],fa[MAXN],ct2,head2[MAXN];
long long ans;
bool vis[MAXN];
struct N{
int to,next;
N(int t=,int n=):to(t),next(n) {}
}edge[MAXN<<],ed[MAXN<<];
int find(int x)
{
if(fa[x]==x)return x;
return fa[x]=find(fa[x]);
}
void tarjan(int x)
{
fa[x]=x;vis[x]=;
for(int i=head2[x],u;i;i=ed[i].next)
if(vis[u=ed[i].to])s[find(u)]-=;
for(int i=head[x],u;i;i=edge[i].next)
{
if(vis[u=edge[i].to])continue;//fa
tarjan(u);fa[u]=x;s[x]+=s[u];
if(s[u]==)ans+=m;
if(s[u]==)ans++;
}
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=,x,y;i<n;i++)
{
scanf("%d%d",&x,&y);
edge[++ct]=N(y,head[x]);head[x]=ct;
edge[++ct]=N(x,head[y]);head[y]=ct;
}
for(int i=,x,y;i<=m;i++)
{
scanf("%d%d",&x,&y);
if(x==y)continue;//!
s[x]++;s[y]++;
ed[++ct2]=N(y,head2[x]);head2[x]=ct2;
ed[++ct2]=N(x,head2[y]);head2[y]=ct2;
}
tarjan();
printf("%d",ans);
return ;
}

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

  1. [BZOJ3307]:雨天的尾巴(LCA+树上差分+权值线段树)

    题目传送门 题目描述: N个点,形成一个树状结构.有M次发放,每次选择两个点x,y对于x到y的路径上(含x,y)每个点发一袋Z类型的物品.完成所有发放后,每个点存放最多的是哪种物品. 输入格式: 第一 ...

  2. [luogu P3128][USACO15DEC]Max Flow [LCA][树上差分]

    题目描述 Farmer John has installed a new system of  pipes to transport milk between the  stalls in his b ...

  3. bzoj4326 树链剖分 + 线段树 // 二分 lca + 树上差分

    https://www.lydsy.com/JudgeOnline/problem.php?id=4326 题意:N个点的树上给M条树链,问去掉一条边的权值之后所有树链长度和的最大值最小是多少. 首先 ...

  4. 2018.08.22 codves2370 小机房的树(lca+树上差分)

    传送门 一道板子题. 直接树链剖分维护树上lca然后差分就行了. 代码: #include<bits/stdc++.h> #define N 50005 #define lc (p< ...

  5. 【洛谷】【lca+树上差分】P3258 [JLOI2014]松鼠的新家

    [题目描述:] 松鼠的新家是一棵树,前几天刚刚装修了新家,新家有n(2 ≤ n ≤ 300000)个房间,并且有n-1根树枝连接,每个房间都可以相互到达,且俩个房间之间的路线都是唯一的.天哪,他居然真 ...

  6. [JLOI2014] 松鼠的新家 (lca/树上差分)

    [JLOI2014]松鼠的新家 题目描述 松鼠的新家是一棵树,前几天刚刚装修了新家,新家有n个房间,并且有n-1根树枝连接,每个房间都可以相互到达,且俩个房间之间的路线都是唯一的.天哪,他居然真的住在 ...

  7. LOJ2425 NOIP2015 运输计划 【二分+LCA+树上差分】*

    LOJ2425 NOIP2015 运输计划 LINK 题意:给你一颗树,可以将任意一条边的权值变成0,然后求m条路径的长度的最小值 思路: 先二分最后的距离ans,然后我们把路程大于ans的所有路径拿 ...

  8. [BZOJ3631]:[JLOI2014]松鼠的新家(LCA+树上差分)

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

  9. bzoj 3307: 雨天的尾巴【树剖lca+树上差分+线段树合并】

    这居然是我第一次写线段树合并--所以我居然在合并的时候加点结果WAWAWAMLEMLEMLE--!ro的时候居然直接指到la就行-- 树上差分,每个点建一棵动态开点线段树,然后统计答案的时候合并即可 ...

随机推荐

  1. Resolving 'Root Partition Is Filling Up' Issue on Sophos UTM Firewall

    from: https://wandersick.blogspot.com/2016/06/resolving-root-partition-is-filling-up.html This is a ...

  2. 百科知识 华为手机P7如何更换电池

    参考下面 教程 https://item.jd.com/3265516.html  

  3. Linux基础(3)- 正文处理命令及tar命令、vi编辑器、硬盘分区、格式化及文件系统的管理和软连接、硬连接

    一.正文处理命令及tar命令 1)  将用户信息数据库文件和组信息数据库文件纵向合并为一个文件1.txt(覆盖) 2)  将用户信息数据库文件和用户密码数据库文件纵向合并为一个文件2.txt(追加) ...

  4. nightwatch.js - scroll until element is visible

    .getLocationInView() Determine an element's location on the screen once it has been scrolled into vi ...

  5. mysql查询结果自动生成序列号

  6. 命令行查看memcached的运行状态(转载)

    很多时候需要监控服务器上的Memcached运行情况,比如缓存的查询次数,命中率之类的.但找到的那个memcached-tool是linux下用perl写的,我也没试过windows能不能用.后来发现 ...

  7. Python之Numpy库常用函数大全(含注释)

    前言:最近学习Python,才发现原来python里的各种库才是大头! 于是乎找了学习资料对Numpy库常用的函数进行总结,并带了注释.在这里分享给大家,对于库的学习,还是用到时候再查,没必要死记硬背 ...

  8. HUD 2031: 进制转换

    进制转换 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  9. android android:duplicateParentState=&quot;true&quot; &quot;false&quot;

    今天要做一个效果.组件RelativeLayout上有两个TextView.这两个TextView具有不同的颜色值,如今要的效果是,当RelativeLayout被点击时,整个item有高亮背景. 同 ...

  10. ZooKeeper学习笔记(一)

    引导 刚开始学习ZooKeepter,看的书是[O'Reilly Media] ZooKeeper.下面的内容基本上是该书的翻译,很多语句也存在问题,大致读还是没问题的,自己在学习中记录. 第一章 Z ...