哈理工oj hrbust 1725 Virus Outbreaking【并查集】
来源:互联网 发布:淘宝商品代理 编辑:程序博客网 时间:2024/05/18 02:50
Village H is suffering a virus outbreaking.
There are n persons in the village, some of them is infected by at most one virus.
There are multiple test cases. The first line is an integer T indicating the number of test cases.
The first line of each case is two positive integers n and q. n is the number of persons in the village and q is the times of event happened in village H.
The second line of each case is n numbers a1, a2, ..., an separated by space. ai stands for the kind of virus the ith person infected. 0 means not infected.
Then q lines following, each is an event happened in the village, consist of touching or query ordered by happening:
touch A B: a touched b and they infected each other. They will be together until the end.
query A: ask how many kind of virus person A infected. If A is not infected, output 0.
There will be a blank line after each case.
Note: 1 <= n <= 1000, 1 <= q <= 1000, 0 <= ai <= 32.
For each query, output one line consist of all kinds of virus the person A infected, Output them by the increasing order, separated by space.
Output a blank line after each test case.
Sample Input24 620 14 24 30query 1query 4query 4query 4touch 1 4query 24 67 4 28 20touch 4 1query 2query 1query 3query 3query 1Sample Output
2030303014
47 2028287 20Hint Source哈理工2013春季校赛 - 网络预选赛Author黄李龙@HRBUST
写完这最后一篇题解,就准备收拾收拾东西上火车了,寒假的最后一发、做的还算开心。
一道并查集的题目,英语好的,读懂题意马上就能AC;
题目大意:【自己看输出猜的】有n个人,m个提问,n个数代表n个人的权值,touch操作表示连接两个人,query表示查询这个人所在集合所有人的权值。【从小到大,并且不要重复】、
AC代码:
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;int a[1024];int f[1024];int n,m;int find(int x){ return f[x] == x ? x : (f[x] = find(f[x]));}void merge(int a,int b){ int A,B; A=find(a); B=find(b); if(A!=B) f[B]=A;}int findans(int b){ int ans[1024]; int cont=0; for(int i=1;i<=n;i++) { if(find(i)==find(b)) { int ok=1; for(int j=0;j<cont;j++) { if(ans[j]==a[i]) { ok=0; } } if(ok&&a[i]) { ans[cont]=a[i]; cont++; } } } if(cont==0) { printf("0\n"); } else { sort(ans,ans+cont); for(int i=0;i<cont-1;i++) { printf("%d ",ans[i]); } printf("%d\n",ans[cont-1]); }}int main(){ int t; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) { scanf("%d",&a[i]); f[i]=i; } while(m--) { char str[50]; scanf("%s",str); if(str[0]=='t') { int x,y; scanf("%d%d",&x,&y); merge(x,y); } if(str[0]=='q') { int x; scanf("%d",&x); findans(x); } } printf("\n"); }}
- 哈理工oj hrbust 1725 Virus Outbreaking【并查集】
- 哈理工oj 1725 Virus Outbreaking 【并查集】
- hrbust 哈理工oj 2026 势力较量【并查集】
- hrbust 1658,哈理工oj 1658 一笔画【并查集过】
- hrbust 哈理工oj 1418 夏夜星空【带权并查集】
- hrbust 哈理工oj 1360 Leyni的国家III【多路径并查集】
- hrbust/哈理工oj 1507 水神的栅栏【计算几何+并查集】
- hrbust 哈理工oj 2016 势力较量 (并查集)
- hrbust 哈理工oj1490 咒语【基础并查集】
- 哈理工OJ 1959 森林木(并查集)
- 哈理工oj 1490咒语【基础并查集】
- 并查集 hrbust 1725
- hrbust 哈理工oj 1588 神医【贪心】
- hrbust 哈理工oj 网线【MST+Prim】
- 哈理工oj/hrbust 1790 武林【DP】
- hrbust哈理工oj 1674 充电【贪心】
- hrbust 哈理工oj 1330 邂逅【模拟】
- hrbust/哈理工oj 1877 区间【水题】
- 第三方SDK:RongCloud IMKit实现简单的单人会话
- iTerm 常用命令
- USACO-Section 3.2 Factorials(模拟)
- centos 6.5 搭建php
- WebView中网页点击还在WebView中跳转以及在屏幕上完整显示
- 哈理工oj hrbust 1725 Virus Outbreaking【并查集】
- Python中导入其它模块中的函数
- php简单爬虫使用登录账号密码爬取列表(爬虫二篇)
- 看看图像处理
- 【BZOJ2818】Gcd,数论练习之欧拉筛
- IOS开发学习之OC的消息机制。
- 面试题--两个字符串数字相加,不能用jdk自带的类型转换方法
- POJ2456 二分
- HTTPS原理