题目描述
It’s universally acknowledged that there’re innumerable trees in the campus of HUST. Thus a professional tree manager is needed. Your task is to write a program to help manage the trees.
Initially, there are n forests and for the i-th forest there is only the i-th tree in it. Given four kinds of operations.
1 u v, merge the forest containing the u-th tree and the forest containing the v-th tree; 2 u, separate the u-th tree from its forest; 3 u, query the size of the forest which contains the u-th tree;
4 u v, query whether the u-th tree and the v-th tree are in the same forest.
输入描述:
The first line contains an integer T, indicating the number of testcases. In each test case: The first line contains two integers N and Q, indicating the number of initial forests and the number of operations. Then Q lines follow, and each line describes an operation. 输出描述:
For each test cases, the first line should be "Case #i:", where i indicate the test case i.
For each query 3, print a integer in a single line.
For each query 4, print "YES" or "NO" in a single line.
示例1
输入
1
10 8
3 1
4 1 2
1 1 2
3 1
4 1 2
2 1
3 1
4 1 2
输出
Case #1:
1
NO
2
YES
1
NO

题意:对trees有四种操作。

1 u v,将包含u-th树的森林和含有v-th树的森林合并在一起;

u,将u-th树与森林分开;

3 u,查询包含u-th树的森林的大小;

4 u v,查询u-th树和v-th树是否在同一林中。

【分析】:给每个点都给他们造一个编号w,删除点,就相当于该点的编号w改变就可以了,也是相当于构造新点。

【出处】:UVA 11987 (白书267)

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<string>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<set>
#define LL long long
#define INF 0xffffff
using namespace std;
const double pi = 2 * acos (0.0);
const int maxn = 2e5+10;
int n,m;
int p[maxn];
int s[maxn];
int w[maxn]; void init()
{
for(int i=1;i<=maxn;i++) //这里必须是maxn,其他的好像不行
{
p[i] = i;
w[i] = i;
s[i] = 1;
}
}
int Find(int x)
{
return x==p[x]?x:p[x]=Find(p[x]);
}
void Union(int x,int y)
{
int fx = Find(x);
int fy = Find(y);
if(fx!=fy)
{
p[fx] = fy;
s[fy] += s[fx];
//printf("size=%d\n",s[fy]);
}
} int main()
{
int T;
cin >> T;
int k =1;
while(T--)
{
printf("Case #%d:\n",k++);
init(); cin >> n >> m;
int cnt = n;
while(m--)
{
int op,u,v;
scanf("%d",&op);
if(op==1)
{
scanf("%d%d",&u,&v);
Union(w[u],w[v]);
}
else if(op==2)
{
scanf("%d",&u);
s[Find(w[u])]--;
w[u] = ++cnt;
}
else if(op==3)
{
scanf("%d",&u);
printf("%d\n",s[Find(w[u])]);
}
else{
scanf("%d%d",&u,&v);
if(Find(w[u])==Find(w[v]))
printf("YES\n");
else printf("NO\n");
}
}
}
return 0;
}

第十四届华中科技大学程序设计竞赛 C Professional Manager【并查集删除/虚点】的更多相关文章

  1. 第十四届华中科技大学程序设计竞赛决赛同步赛 A - Beauty of Trees

    A - Beauty of Trees 题意: 链接:https://www.nowcoder.com/acm/contest/119/A来源:牛客网 Beauty of Trees 时间限制:C/C ...

  2. 第十四届华中科技大学程序设计竞赛决赛同步赛 F Beautiful Land(01背包,背包体积超大时)

    链接:https://www.nowcoder.com/acm/contest/119/F来源:牛客网 Beautiful Land 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 1 ...

  3. 第十四届华中科技大学程序设计竞赛 K Walking in the Forest【二分答案/最小化最大值】

    链接:https://www.nowcoder.com/acm/contest/106/K 来源:牛客网 题目描述 It's universally acknowledged that there'r ...

  4. 第十四届华中科技大学程序设计竞赛 J Various Tree【数值型一维BFS/最小步数】

    链接:https://www.nowcoder.com/acm/contest/106/J 来源:牛客网 题目描述 It's universally acknowledged that there'r ...

  5. 第十四届华中科技大学程序设计竞赛 B Beautiful Trees Cutting【组合数学/费马小定理求逆元/快速幂】

    链接:https://www.nowcoder.com/acm/contest/106/B 来源:牛客网 题目描述 It's universally acknowledged that there'r ...

  6. 第十四届华中科技大学程序设计竞赛决赛同步赛 Beautiful Land

    It’s universally acknowledged that there’re innumerable trees in the campus of HUST.Now HUST got a b ...

  7. 第十四届华中科技大学程序设计竞赛 K--Walking in the Forest

    链接:https://www.nowcoder.com/acm/contest/106/K来源:牛客网 题目描述 It’s universally acknowledged that there’re ...

  8. 第十四届华中科技大学程序设计竞赛--J Various Tree

    链接:https://www.nowcoder.com/acm/contest/106/J来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536 ...

  9. Minieye杯第十五届华中科技大学程序设计邀请赛现场同步赛 I Matrix Again

    Minieye杯第十五届华中科技大学程序设计邀请赛现场同步赛 I Matrix Again https://ac.nowcoder.com/acm/contest/700/I 时间限制:C/C++ 1 ...

随机推荐

  1. 无限小数转分数POJ1930分析

    将无限小数化为分数,有一套简单的公式.使其轻松表示出来. 循环节 例如:0.121212…… 循循环节为12.   公式 这个公式必须将循环节的开头放在十分位.若不是可将原数乘10^x(x为正整数) ...

  2. 1、shader简介、渲染管线

    vs对于shader的插件:http://blog.shuiguzi.com/shaderlabvs-release-page.html 计算机有一块重要的组成部分,就是“显卡”,大家玩游戏的话,肯定 ...

  3. [译]在Linux中清空或删除大文件内容的5种方法

    原文来源: https://www.tecmint.com/empty-delete-file-content-linux/ 有时,在处理Linux终端中的文件时,您可能希望清除文件的内容,而无需使用 ...

  4. Dijkstra算法_最短路径_L2-001. 紧急救援

    作为一个城市的应急救援队伍的负责人,你有一张特殊的全国地图.在地图上显示有多个分散的城市和一些连接城市的快速道路.每个城市的救援队数量和每一条连接两个城市的快速道路长度都标在地图上.当其他城市有紧急求 ...

  5. Oracle常用数据库系统表单以及SQL的整理

    因为最近涉及到了一些数据库的归档,备份等工作,所以一部分的重心放在了数据库上,毕竟之前对数据库的了解也只停留在了一般的建表,查询,最多最多再写一写触发器之类的东西. 通常都是自己瞎搞搞,也就懂一点皮毛 ...

  6. Struts2+DAO层实现实例01——搭建Struts2基本框架

    实例内容 利用Strust2实现一个登陆+注册功能的登陆系统. 实现基础流程:

  7. 用树莓派做3G无线路由器

    第一篇博客献给我做了很长时间的课程设计,也就是题目所说的3G无线路由器.本次开发所使用的开发平台为树莓派开发板,下面进入正题..... 目标:将树莓派设置成为一个3G无线路由器,通过华为的E261拨号 ...

  8. ionic2.x 手动搭建开发环境教程分享(nodejs,jdk,ant,androidsdk)

    1.ionic简介 为什么选用ionic: 1.     彻底开源且免费 2.     性能优异 3.     基于红的发紫的AngularJs 4.     漂亮的UI 5.     强大的命令行( ...

  9. JavaScript如何读写cookie

    今天把javascript如何用来创建及存储cookie复习了一下,其中的一点体会拿出来和大家讨论,首先看一下基础知识: 什么是cookie cookie 是存储于访问者的计算机中的变量.每当同一台计 ...

  10. 浅谈Visitor Pattern

    第一步:   在介绍Visitor Pattern (访问者模式)之前,先简要介绍一下:双重分派. 在Visitor Pattern中双重分派是指:数据结构的每一个节点都可以接受一个访问者的调用(这句 ...