考虑离线,将删边操作倒过来变成加边,等价于询问每个点什么时候与1连通

使用并查集维护,每次合并时如果有一边是1所在连通块,就把另一边的所有点的答案更新

#include<cstdio>
#define N 200010
int n,m,i,j,x,y,son[N][2],del[N][2],q[N<<1][2],f[N],ans[N],g[N],nxt[N<<1],v[N<<1],ed;
inline void read(int&a){
char c;
while(!((((c=getchar())>='0')&&(c<='9'))||(c=='-')));
if(c!='-')a=c-'0';else{a=-1;getchar();return;}
while(((c=getchar())>='0')&&(c<='9'))(a*=10)+=c-'0';
}
inline void add(int x,int y){v[++ed]=y;nxt[ed]=g[x];g[x]=ed;}
void dfs(int x,int pre,int y){
ans[x]=y;
for(int i=g[x];i;i=nxt[i])if(v[i]!=pre)dfs(v[i],x,y);
}
int F(int x){return f[x]==x?x:f[x]=F(f[x]);}
inline void merge(int x,int y,int z){
if(y<0)return;
if(F(x)==F(y))return;
if(f[x]==F(1))dfs(y,0,z);else if(f[y]==F(1))dfs(x,0,z);else add(x,y),add(y,x);
f[f[x]]=f[y];
}
int main(){
read(n),read(m);
for(i=1;i<=n;i++)read(son[i][0]),read(son[i][1]),f[i]=i;
for(i=0;i<m;i++)read(x),read(y),del[q[i][0]=x][q[i][1]=y-1]=1;
for(i=1;i<=n;i++)for(j=0;j<2;j++)if(!del[i][j])merge(i,son[i][j],-1);
for(i=m-1;~i;i--)merge(q[i][0],son[q[i][0]][q[i][1]],i);
for(puts("-1"),i=2;i<=n;i++)printf("%d\n",ans[i]);
return 0;
}

  

BZOJ2610 : [Poi2003]Monkeys的更多相关文章

  1. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

  2. 【POI2003/2004 stage I】

    [原题在此] Let us consider a game on a rectangular board m x 1 consisting of m elementary squares number ...

  3. Twelves Monkeys (multiset解法 141 - ZOJ Monthly, July 2015 - H)

    Twelves Monkeys Time Limit: 5 Seconds      Memory Limit: 32768 KB James Cole is a convicted criminal ...

  4. HDU - 6178:Monkeys (贪心&树上最大匹配输&输入优化)

    There is a tree having N vertices. In the tree there are K monkeys (K <= N). A vertex can be occu ...

  5. zoj 3888 Twelves Monkeys 二分+线段树维护次小值

    链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do? problemCode=3888 Twelves Monkeys Time Limit: 5 ...

  6. 思维+multiset ZOJ Monthly, July 2015 - H Twelves Monkeys

    题目传送门 /* 题意:n个时刻点,m次时光穿梭,告诉的起点和终点,q次询问,每次询问t时刻t之前有多少时刻点是可以通过两种不同的路径到达 思维:对于当前p时间,从现在到未来穿越到过去的是有效的值,排 ...

  7. ZOJ 3888 Twelves Monkeys (预处理+优先队列)

    题目链接:ZOJ 3888 Twelves Monkeys 题意:题目描写叙述起来比較绕,直接讲案例 9 3 3 9 1 6 1 4 1 6 7 2 输入n,m,q.n限制了你询问的年份,m台时光机, ...

  8. ZOJ 3888 Twelves Monkeys

    Twelves Monkeys Time Limit: 5000ms Memory Limit: 32768KB This problem will be judged on ZJU. Origina ...

  9. UVa11167 Monkeys in the Emei Mountain(最大流)

    题目大概说有n只猴子,猴子们在某个时间段需要喝vi时间的水,各个单位时间段最多允许m只猴子同时喝水,问猴子们能否成功喝水并输出一个可行的方案,输出方案的时间段区间要从小到大排序并且合并连续的区间. 首 ...

随机推荐

  1. nginx(五)nginx与php的安装配置

    经过前面学习,对nginx有个大概的了解,来配置LNMP;只要是在系统安装过程中选择安装比较齐全的包,基本上系统都能满足安装要求,下面是我一个一个测试的,基本上全部安装所需的库文件,放心安装: [ro ...

  2. MySQL5.5 RPM安装的默认安装路径

    MySQL5.5 RPM安装的默认安装路径 2011-06-20 10:34:32|  分类: MySQL|举报|字号 订阅   下载LOFTER客户端     由于一客户要求安装mysql- 5.5 ...

  3. 又一款linux提权辅助工具

    又一款linux提权辅助工具 – Linux_Exploit_Suggester 2013-09-06 10:34 1455人阅读 评论(0) 收藏 举报 https://github.com/Pen ...

  4. 通过JS语句判断WEB网站的访问端是电脑还是手机

    通过JS语句判断WEB网站的访问端是电脑还是手机,以显示不同的页面! 目录腾讯网的适配代码如何判断访问网站的机器类型-如何判断ipadJS 判断浏览器客户端类型(ipad,iphone,android ...

  5. C++类编程(一)const的使用

    设计类时,考虑以下五点 1.构造函数初始化列表 2.函数该不该加const 3.参数传递尽量考虑用引用传递,考虑加不加const 4.返回用不用引用 5.数据尽量放在private,函数尽量放在pub ...

  6. jquery unbind bind

    $(selector).unbind(); --unbind() 方法会删除指定元素的所有事件处理程序 $(selector).unbind("click"); --unbind( ...

  7. iOS UITableView 的beginUpdates和endUpdates

    在官方文档中是这样介绍beginUpdates的 Call this method if you want subsequent insertions, deletion, and selection ...

  8. Java for LeetCode 151 Reverse Words in a String

    Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...

  9. Java for LeetCode 046 Permutations

    Given a collection of numbers, return all possible permutations. For example, [1,2,3] have the follo ...

  10. JDK安装和配置

    一.Windows下的JDK环境变量配置 在java 中需要设置三个环境变量(1.5之后不用再设置classpath了,但个人强烈建议继续设置以保证向下兼用问题) JDK安装完成之后我们来设置环境变量 ...