哈理工oj 1725 Virus Outbreaking 【并查集】
来源:互联网 发布:上虞干部教育培训网络 编辑:程序博客网 时间:2024/05/07 23:49
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 20
简单并查集,重点是输出,注意查重及方法;
#include<bits/stdc++.h>using namespace std;int pre[1111];int a[1111];int n, m;int findx(int x){ return pre[x] == x ? x : pre[x] = findx(pre[x]);}void join(int a, int b){ int X = findx(a); int Y = findx(b); if(X != Y) pre[X] = Y;}void group(int c){ int ans[1111]; int num = 0; for(int i = 1; i <= n; i++) { if(findx(i) == findx(c)) { int flag = 1; ///查重 for(int j = 0; j < num; j++) if(ans[j] == a[i]) flag = 0; if(flag && a[i]) ans[num++] = a[i]; } } if(!num) printf("0\n"); else { sort(ans, ans + num); for(int i = 0; i < num - 1; i++) printf("%d ", ans[i]); printf("%d\n", ans[num - 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]); pre[i] = i; } while(m--) { char order[66]; scanf("%s", order); if(order[0] == 't') { int nx, ny; scanf("%d%d", &nx, &ny); join(nx, ny); } if(order[0] == 'q') { int nz; scanf("%d", &nz); group(nz); } } printf("\n"); } return 0;}
- 哈理工oj hrbust 1725 Virus Outbreaking【并查集】
- 哈理工oj 1725 Virus Outbreaking 【并查集】
- 哈理工OJ 1959 森林木(并查集)
- hrbust 哈理工oj 2026 势力较量【并查集】
- 哈理工oj 1490咒语【基础并查集】
- 哈理工OJ 2240 土豪的时代【带权并查集】
- hrbust 1658,哈理工oj 1658 一笔画【并查集过】
- hrbust 哈理工oj 1418 夏夜星空【带权并查集】
- hrbust 哈理工oj 1360 Leyni的国家III【多路径并查集】
- hrbust/哈理工oj 1507 水神的栅栏【计算几何+并查集】
- hrbust 哈理工oj 2016 势力较量 (并查集)
- ZOJ 3261 /哈理工OJ 1913 Connection in War(逆向并查集)(STL应用)
- 哈理工1311 并查集+map火影忍者之~忍者村
- hrbust 哈理工oj1490 咒语【基础并查集】
- 九度OJ 朋友圈 -- 并查集
- SDUT OJ 2801 并查集
- swjtu-oj 1569 离线并查集
- 山东大学oj 并查集 Ubiquitous Religions
- 6岁会编程14岁被麻省理工录取?“神童”报道反转不断
- 一个戏精程序员的内心独白……
- 趣图 | 当客户让程序员又双叒叕做「小」改动后
- 《挪威的森林》-(一)
- 荐书丨程序员的成长课(留言送书)
- 哈理工oj 1725 Virus Outbreaking 【并查集】
- 本周作业题解
- JavaFX之RadioButton (1)
- python中groupby()函数讲解与示例
- vsnprintf()函数 & vfprintf()函数
- mysqldump原理分析
- Coursera Machine Learning 第五周编程week5 ex4Neural Networks Learning编程全套满分题目+注释
- 趣图丨凭什么不让我写代码?7456!
- [LeetCode] 206. Reverse Linked List