题目描述

DIDIDI and WNJXYK are good friends. One day, they go to the zoo. The monkeys are playing happily. There are n monkeys named 1-n. At first, the first one hangs its tail on the tree. The other n-1 monkeys, either are caught by other monkeys, or catch other monkeys, or both. In the next m seconds, every second, there will be a monkey releasing its left hand or right hand. And then some monkeys will drop on the floor. Given the relationship between monkeys, calculate the landing time of every monkey.

输入

The first line of input contains a positive integer T, telling you there are T test cases followed.
Each test case, first line includes two integers n, m. n means number of monkeys, and m as mentioned in the description.
Then following n lines, the k-th line has two integers, describe the k-th monkey’s information. The first integer is the index of monkey in its left hand, the second integer is the index of monkey in its right hand. -1 indicates there is nothing in its hand.
Then following m lines, the i-th line gives the information at time i-1, every line has two numbers, the first is the index of the monkey, the second is the hand that it releases, 1 is left hand, 2 is right hand.
It’s guaranteed that all the monkeys aren’t on the floor at the beginning.

输出

For each test case, print a line “Case #x: ”, where x is the case number (starting from 1) and print n integers. The i-th integer is the time monkey i drop in the floor. If monkey i don’t land on the floor, print -1;

样例输入
1
3 2
-1 3
3 -1
1 2
1 2
3 1
样例输出
Case #1: -1 1 1
提示

Tips:1≤T≤10,1≤n≤200,000,0≤m≤400,000
Initially, the first monkey’s tail hangs on the tree. The first monkey’s right hand catches the third monkey, the second monkey’s left hand catches the third monkey, and the third’s left hand catches the first monkey while the right hand catches the second. On time 0, the first monkey releases its right hand, no monkey lands on the floor. On time 1, the third monkey releases its left hand, then the second monkey and the third land on the floor.

题意

n 只猴子( 标号 1 - n ),m 个时刻 , 一开始给出 n 只猴子左右手牵的是哪只猴子( - 1 就是没牵),然后接下来 m 行给出每个时刻哪只猴子放开哪只手,最后让你给出 m 个时刻后,每只猴子在哪个时刻掉下去( - 1 代表没掉下去)(一开始只有 1 号猴子 尾巴 挂在树上)

思路

看猴子是不是掉下去就是判断和 1 是不是联通,可以倒着考虑这个题,把松手倒过来,看做从最后一次开始抓,用并查集启发式合并集合的那些元素,然后看看和 1 是不是连通,如果联通,就把答案时刻存下来。(还是看代码吧(((φ(◎ロ◎;)φ))))

代码
#include<iostream>
#include<string>
#include<map>
#include<set>
//#include<unordered_map>
#include<queue>
#include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<fstream>
#define X first
#define Y second
#define best 131
#define INF 0x3f3f3f3f
#define P pair<int,int>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const double eps=1e-7;
const double pai=acos(-1.0);
const int N=2e4+10;
const int maxn=4e5+10;
const int mod=1e9+7;
//const int d[4][2]={0,1,1,0,-1,0,0,-1};
int pre[maxn],ans[maxn];
vector<int>deep[maxn];
pair<int,int>p[maxn];
int vis[maxn][3],to[maxn][3];
int find(int x)
{if(pre[x]==x) return x;else return pre[x]=find(pre[x]);
}
void unions1(int x,int y)
{int fx=find(x);int fy=find(y);if(fx==fy) return;if(deep[fx].size()>deep[fy].size()) swap(fx,fy);pre[fx]=fy;deep[fy].insert(deep[fy].end(),deep[fx].begin(),deep[fx].end());
}
void unions2(int x,int y,int t)
{if(y==-1) return;int fx=find(x);int fy=find(y);if(fx==fy) return;int f1=find(1);if(fx==f1){for(int i=0;i<deep[fy].size();i++)ans[deep[fy][i]]=t;}if(fy==f1){for(int i=0;i<deep[fx].size();i++)ans[deep[fx][i]]=t;}if(deep[fx].size()>deep[fy].size()) swap(fx,fy);pre[fx]=fy;deep[fy].insert(deep[fy].end(),deep[fx].begin(),deep[fx].end());
}
void init(int n)
{for(int i=1;i<=n;i++) {deep[i].clear();pre[i]=i;ans[i]=-1;deep[i].push_back(i);vis[i][1]=0;vis[i][2]=0;}
}
int main()
{//  ios::sync_with_stdio(false);//memset(mp,-1,sizeof(mp));int t,n,m,x,y,tot=0;scanf("%d",&t);while(t--){scanf("%d%d",&n,&m);init(n);for(int i=1;i<=n;i++) {scanf("%d%d",&x,&y);to[i][1]=x;to[i][2]=y;}for(int i=1;i<=m;i++){scanf("%d%d",&p[i].first,&p[i].second);vis[p[i].first][p[i].second]=1;} for(int i=1;i<=n;i++) {for(int j=1;j<=2;j++){if(to[i][j]==-1||vis[i][j]) continue;unions1(i,to[i][j]);} }for(int i=m;i>=1;i--) {unions2(p[i].first,to[p[i].first][p[i].second],i-1);}printf("Case #%d: ",++tot);for(int i=1;i<n;i++) printf("%d ",ans[i]);printf("%d\n",ans[n]);}return 0;
}

upc-WNJXYK and DIDIDI and monkey(并查集启发式合并)相关推荐

  1. codeforces1559 D2. Mocha and Diana (Hard Version)(并查集+启发式合并+随机化)

    D2. Mocha and Diana (Hard Version) RunningBeef题解 首先将图1的点与1号点所在的连通块相连,图2类似. 然后就是在图1和图2中选择没有和1号点在同一个连通 ...

  2. 2018.08.21 bzoj4668: 冷战(并查集+启发式合并)

    传送门 可以发现需要维护连通性和两点连通时间. 前者显然是并查集的常规操作,关键就在于如何维护两点的连通时间. 然后会想到这个时候不能用路径压缩了,因为它会破坏原本树形集合的结构,因此可以启发式按si ...

  3. codeforces#1166F. Vicky's Delivery (Service并查集+启发式合并)

    题目链接: https://codeforces.com/contest/1166/problem/F 题意: 给出节点数为$n$,边数为$m$的图,保证每个点对都是互连的 定义彩虹路:这条路经过$k ...

  4. 51nod 1515 明辨是非 2017百度之星初赛第一场第二题(并查集+启发式合并)

    题目: 原题链接 给n组操作,每组操作形式为x y p. 当p为1时,如果第x变量和第y个变量可以相等,则输出YES,并限制他们相等:否则输出NO,并忽略此次操作. 当p为0时,如果第x变量和第y个变 ...

  5. P7323-[WC2021]括号路径【并查集,启发式合并】

    正题 题目链接:https://www.luogu.com.cn/problem/P7323 题目大意 给出nnn个点的一张有向图.每个边(u,v,w)(u,v,w)(u,v,w)表示u−>vu ...

  6. Jumping Monkey 并查集,反向思维

    题意 : n点的树,每个点的点权distinct,求分别从每个点出发能到达的点数(被到达的点必须是路径上点权最大的点) 思路 : 考虑点权最大的那个结点(假设为u),显然不管从哪个结点开始跳一次,都可 ...

  7. POJ1456贪心(set或者并查集区间合并)

    题意:       给你n商品,每个商品有自己的价值还有保质期,一天最多只能卖出去一个商品,问最大收益是多少? 思路:       比较好想的贪心,思路是这样,每一次我们肯定拿价值最大的,至于在那天拿 ...

  8. 牛客多校3 - Operating on a Graph(并查集+链表合并)

    题目链接:点击查看 题目大意:给出一个由 n 个点和 m 条边组成的无向图,每个点初始时都有颜色 i ,i ∈ [ 0 , n - 1 ] ,接下来有 q 次操作,每次操作会给出一种颜色 x ,分两种 ...

  9. 采用Kruskal算法生成最小生成树,并采用并查集的合并优化和查询优化。

    文章目录 最小生成树 1.什么是图的最小生成树(MST)? 2.最小生成树用来解决什么问题? Kruskal(克鲁斯卡尔)算法 算法描述 图解 最小生成树 1.什么是图的最小生成树(MST)? 用N- ...

最新文章

  1. 每位开发人员都应铭记的10句编程谚语 (我超喜欢,转载了)
  2. php 函数 变量,PHP函数中变量的说明
  3. 多重背包 (poj 1014)
  4. spring security CSRF 问题 Invalid CSRF Token 'null' was found on ......
  5. Istio的网络API解释了
  6. web.config中的globalization 标签在将几个不同服务器上的网闸内容以统一面貌集成在自己网站上时的要考虑的问题
  7. Detected both log4j-over-slf4j.jar AND bound slf4j-log4j12.jar on the class
  8. excel文件打不开怎么办_电脑设备管理器打不开怎么办
  9. 服务器测试文档格式,服务器测试文档格式
  10. 用面向对象思想设计奥赛罗游戏
  11. 对路径的访问被拒绝怎么办_工作组计算机无法访问,教您无法访问工作组计算机的解决技巧...
  12. 转载PHP的静态变量介绍
  13. 量子计算和量子加密的基础问答
  14. 【Linux系列文章】网络配置
  15. 表的列被set unused的机制
  16. PHP论坛开发技术总结
  17. tf2常用数据类型与常用函数汇总
  18. 【Kafka】第三篇-Kafka的集群及Canal介绍
  19. Android 基础知识+app测试权限问题
  20. Python pycharm环境 飞机大战游戏代码 以及打包成exe教程

热门文章

  1. bae index.php,使用百度Bae云平台服务器安装原版dedec_php
  2. flutter实现条形码二维码扫描
  3. Compaq笔记本的新功能
  4. 常说的220V与380V有什么区别?
  5. IIS 7.0: 使用集成的 ASP.NET 管道增强应用程序
  6. RDBMS和HDFS的区别
  7. 搭建直播平台过程中Android端直播APP源码是如何实现连麦功能的?
  8. JS 指定元素滚动到屏幕中间
  9. CSS 学习成长笔记(3)
  10. 《人月神话》阅读记录