题目描述
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. 《Cracking the Coding Interview》——第18章:难题——题目7

    2014-04-29 03:05 题目:给定一个词典,其中某些词可能能够通过词典里其他的词拼接而成.找出这样的组合词里最长的一个. 解法:Leetcode上有Word Break这道题,和这题基本思路 ...

  2. 数据库——pymysql模块的使用(13)

    1.基本用法——建立链接,获取游标,执行sql语句,关闭 建立远程链接账号和权限 mysql> grant all on *.* to '; Query OK, rows affected, w ...

  3. Lua2

    1. 迭代器与Closure 在Lua中,迭代器通常为函数,每调用一次函数,即返回集合中的“下一个”元素.每个迭代器都需要在每次成功调用之间保持一些状态,这样才能知道它所在的位置和下一次遍历时的位置. ...

  4. quagga源码学习--BGP协议中的routemap

    路由策略的基础知识 定义 路由策略(Routing Policy)作用于路由,主要实现了路由过滤和路由属性设置等功能,它通过改变路由属性(包括可达性)来改变网络流量所经过的路径. 目的 路由器在发布. ...

  5. Oracle设置用户密码永不过期

    1.查看用户的profile是那个,一般是default: select username, profile from dba_users; 2.查看指定概要文件(如default)的密码有效期设置: ...

  6. synflood 模拟工具

    synflood 模拟工具 来源 https://blog.csdn.net/wuzhimang/article/details/54581117 因项目需要,要对主流的几家抗DDoS设备做测评,当然 ...

  7. [洛谷P3978][TJOI2015]概率论

    题目大意:对于一棵随机生成的$n$个结点的有根二叉树,所有不同构的形态等概率出现(这里同构当且仅当两棵二叉树根相同,并且相同节点的左儿子和右儿子都相同),求叶子节点个数的期望是多少? 题解:令$f_n ...

  8. ZOJ 1081 Points Within | 判断点在多边形内

    题目: 给个n个点的多边形,n个点按顺序给出,给个点m,判断m在不在多边形内部 题解: 网上有两种方法,这里写一种:射线法 大体的思想是:以这个点为端点,做一条平行与x轴的射线(代码中射线指向x轴正方 ...

  9. bzoj 4621 Tc605 思想+dp

    4621: Tc605 Time Limit: 15 Sec  Memory Limit: 128 MBSubmit: 328  Solved: 183[Submit][Status][Discuss ...

  10. MFC 加载资源文件里的png

    static bool LoadImageFromResource(IN CImage* pImage, IN UINT nResID, IN LPCWSTR lpTyp) { if ( pImage ...