If a tree falls in the forest, and there’s nobody there to hear, does it make a sound? This classic conundrum was coined by George Berkeley (1685-1753), the Bishop and influential Irish philosopher whose primary philosophical achievement is the advancement of what has come to be called subjective idealism. He wrote a number of works, of which the most widelyread are Treatise Concerning the Principles of Human Knowledge (1710) and Three Dialogues between Hylas and Philonous (1713) (Philonous, the “lover of the mind”, representing Berkeley himself).

    A forest contains T trees numbered from 1 to T and P people numbered from 1 to P.
Input
The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.
    Standard input consists of a line containing P and T followed by several lines, containing a pair of integers i and j, indicating that person i has heard tree j fall. People may have different opinions as to which trees, according to Berkeley, have made a sound.
Output
For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.
    How many different opinions are represented in the input? Two people hold the same opinion only if they hear exactly the same set of trees. You may assume that P < 100 and T < 100.
Sample Input
1

3 4
1 2
3 3
1 3
2 2
3 2
2 4
Sample Output
2

问题链接:UVA11559 Event Planning
问题简述:(略)
问题分析
    有P个人和T棵树。给定某人听到某棵树倒下声音的若干二元组,若两个人听到的树的集合相同则他们同属于一个集合,问人分成几个集合。
    有2种解法,一是用并查集,二是用STL的set。
程序说明
    POJ和ZOJ与UVA的题是一样的,输入输出格式不同。
参考链接:(略)
题记:(略)

AC的C++语言程序(POJ)如下:

/* POJ2419 Forests */#include <iostream>
#include <set>using namespace std;const int N = 100 + 1;int main()
{int p, t, a, b;cin >> p >> t;set<int> s[N];while(cin >> a >> b)s[a].insert(b);set<set<int> > s2(s + 1, s + p + 1);cout << s2.size() << endl;return 0;
}

AC的C++语言程序(UVA)如下:

/* UVA10227 Forests */#include <bits/stdc++.h>using namespace std;const int N = 100 + 1;int main()
{int n, p, t, a, b;string line;cin >> n;while(n--) {set<int> s[N];     // 观点cin >> p >> t;cin.ignore();while(getline(cin, line) && !line.empty()) {stringstream ss(line);ss >> a >> b;s[a].insert(b);}set<set<int> > s2;for(int i = 1; i <= p; i++)s2.insert(s[i]);cout << s2.size() << endl;if(n) cout << endl;}return 0;
}

AC的C++语言程序(UVA)如下:

/* UVA10227 Forests */#include <bits/stdc++.h>using namespace std;const int N = 100 + 1;
int f[N];void UFInit(int n)
{for(int i = 0; i < n; i++)f[i] = i;
}int Find(int a) {return a == f[a] ? a : f[a] = Find(f[a]);
}void Union(int a, int b)
{a = Find(a);b = Find(b);if (a != b) {f[a] = b;}
}const int M = 128;
char buf[128];
int p2t[N][N], vis[N];bool judge(int p1, int p2, int t)
{for(int i = 1; i <= t; i++)if(p2t[p1][i] != p2t[p2][i])return false;return true;
}int main()
{int n, p, t, a, b;scanf("%d", &n);while(n--) {UFInit(N);memset(vis, 0, sizeof(vis));scanf("%d%d", &p, &t);getchar();while(fgets(buf, M, stdin) && buf[0] != '\n') {sscanf(buf, "%d%d", &a, &b);vis[a] = 1;p2t[a][b] = 1;}for(int i = 1; i <= p; i++)for(int j = i; j <= p; j++)if(vis[i] && vis[j] && judge(i, j, t))Union(i, j);int cnt = 0;for(int i = 0; i <= p; i++)if(vis[i] && f[i] == i) cnt++;printf("%d\n", cnt);if(n) printf("\n");}return 0;
}

AC的C++语言程序(ZOJ,POJ出现WA)如下:

/* ZOJ1900 POJ2419 Forests */#include <iostream>
#include <stdio.h>
#include <string.h>using namespace std;const int N = 100;
int f[N];void UFInit(int n)
{for(int i = 0; i < n; i++)f[i] = i;
}int Find(int a) {return a == f[a] ? a : f[a] = Find(f[a]);
}void Union(int a, int b)
{a = Find(a);b = Find(b);if (a != b) {f[a] = b;}
}const int M = 128;
char buf[128];
int p2t[N][N], vis[N];bool judge(int p1, int p2, int t)
{for(int i = 1; i <= t; i++)if(p2t[p1][i] != p2t[p2][i])return false;return true;
}int main()
{int p, t, a, b;while(~scanf("%d%d", &p, &t)) {getchar();UFInit(N);memset(vis, 0, sizeof(vis));while(fgets(buf, M, stdin) && buf[0] != '\n') {sscanf(buf, "%d%d", &a, &b);vis[a] = 1;p2t[a][b] = 1;}for(int i = 1; i <= p; i++)for(int j = i; j <= p; j++)if(vis[i] && vis[j] && judge(i, j, t))Union(i, j);int cnt = 0;for(int i = 0; i <= p; i++)if(vis[i] && f[i] == i) cnt++;printf("%d\n", cnt);}return 0;
}

UVA10227 POJ2419 ZOJ1900 Forests【并查集+set】相关推荐

  1. 并查集c++代码_[Leetcode 每日精选](本周主题-并查集) 547. 朋友圈

    题目难度: 中等 原题链接 今天继续来做并查集的问题, 这道题仍然比较基础, 而且也是个比较接近现实的问题了. 大家在我的公众号"每日精选算法题"中的聊天框中回复 并查集 就能看到 ...

  2. HDU1811 Rank of Tetris 拓扑排序+并查集 OR 差分约束最短路+并查集

    题目链接 题意:就是给你一堆关系,看能不能排出个确定的顺序 做法: 1. 拓扑排序+并查集 应该很容易想到的一种思路,大于小于建立单向边.对于相等的呢,就把他们缩成一个点.就用并查集缩成一个点就行了 ...

  3. HDU 2586 How far away ? LCA ---tanjar+并查集 离线算法

    tanjar算法离线求LCA的思想主要是利用并查集的思想. 求距离的话就是d[start[i]]+end[en[i]]-2*d[lca[i]]; 首先从根节点dfs,在深度遍历的回溯的过程中不断的更新 ...

  4. POJ - 2513 Colored Sticks 欧拉通路+并查集+静态树

    一开始想用map来搞,但是感觉好复杂,然后想了一下看大佬们用trie做的,感觉十分合理就敲了一发. 一开始re,数组要开到550000 只会静态的字典树,在每个根节点看是否出现过改颜色,如果没有就把该 ...

  5. 关于 并查集(union find) 算法基本原理 以及 其 在分布式图场景的应用

    二月的最后一篇水文-想写一些有意思的东西. 文章目录 环检测在图数据结构中的应用 深度/广度优先 检测环 并查集数据结构 (Union-Find) 基本概念 初始化 合并 union 查找祖先 优化1 ...

  6. 【BZOJ1015】【JSOI2008】星球大战 并查集

    题目大意 给你一张\(n\)个点\(m\)条边的无向图,有\(q\)次操作,每次删掉一个点以及和这个点相邻的边,求最开始和每次删完点后的连通块个数. \(q\leq n\leq 400000,m\le ...

  7. 并查集 HDOJ 1232 畅通工程

    题目传送门 1 /* 2 并查集(Union-Find)裸题 3 并查集三个函数:初始化Init,寻找根节点Find,连通Union 4 考察:连通边数问题 5 */ 6 #include <c ...

  8. 1013 Battle Over Cities(并查集解法)

    关于背景的介绍见1013 Battle Over Cities(图的DFS解法) DFS就是不算特定结点后数连通子图的总数,再减一.我想着那么并查集就是数不算特定节点后,集合元素(根)的个数.但是我弄 ...

  9. 并查集专题练习:好朋友(未完待续)

    有空再把题目补上 输入样例1 4 2 1 4 2 3 样例输出1 2 输入样例2 7 5 1 2 2 3 3 1 1 4 5 6 输出样例2 3 解题思路: 1. 这题放在并查集的专题后面,有查找也有 ...

最新文章

  1. 贝叶斯机器学习:经典模型与代码实现!
  2. 在QTP中申明XPath
  3. Nat. Biotechnol. | 人工智能药物研发在中国蓬勃发展
  4. scala------------:: , +:, :+, :::, +++的区别
  5. SAP CRM WebClient UI和Hybris CommerceUI tag的渲染逻辑
  6. 华为nova5i计算机有计算记录吗,华为nova 5i正式发布,看完价格后:还是算了吧!...
  7. 【无码专区13】最小公倍数(线段树)
  8. TS DataType
  9. 湖南理工学院计算机专业课表,下学期课表“新鲜出炉”,你查了吗?
  10. 计算机组成原理实用教程第3版课后答案,计算机组成原理实用教程课后习题答案.docx...
  11. postgres数据库入门, python 操作postgres
  12. php opencv 人脸识别,基于OpenCV的PHP图像人脸识别技术
  13. T-SQL 之 DDL语法
  14. AutoJs学习-TTS抢语音红包
  15. ❤The Matrix黑客帝国屏保!!!❤HTML实现及其傻瓜安装你值得拥有
  16. 英语-- such that
  17. 【Jmeter+ant+Jenkins自动化持续集成】
  18. 说说域名、二级域名和主机名的联系区别
  19. 网站制作的流程包括哪几个步骤?
  20. 关于遍历,看这篇文章就足够了【find()、findIndex()、forEach()、splice()、slice()详解】

热门文章

  1. 【转载】Android加载大图片OOM异常解决
  2. C#图片处理之:亮度和对比度的校正
  3. python登录网页版易信_易信网页版下载|易信网页版登陆客户端官方最新版 2.1.1103.0 - 系统天堂...
  4. Scala初步学习(三)
  5. CDH页面中Oozie的调度告警邮箱设置
  6. Hadoop 之 Distcp官网介绍和注意事项
  7. Hive 中日志的存放位置
  8. html 图片 按钮,css按钮背景图片如何实现?(代码实例)
  9. 2字段添加注释_2w字长文给你讲透了配置类为什么要添加 @Configuration注解
  10. QT5之exe发布及dll打包