HDU-3172-Virtual Friends
来源:互联网 发布:淘宝代金券怎么使用 编辑:程序博客网 时间:2024/06/04 18:49
ACM模版
描述
题解
带权并查集,这里需要用到字典树来搞一个映射,把名字映射成ID即可。
注意啦,注意啦,这里有一个坑,绝对坑死人,有多组数据,这里说的是T外还要套一层while
,真是个暗坑!!!
代码
#include <iostream>#include <string>#include <map>using namespace std;const int MAXN = 2e5 + 10;int pre[MAXN];int sum[MAXN];int total;map<string, int> people; // 用map来处理人名与数字下标之间的对应关系int find(int x){ if (x != pre[x]) { pre[x] = find(pre[x]); } return pre[x];}int join(int x, int y){ int fx = find(x); int fy = find(y); if (fx != fy) { pre[fx] = fy; sum[fy] += sum[fx]; } return sum[fy];}int main(){ int T, n; string a, b; while (cin >> T) { while (T--) { total = 0; people.clear(); scanf("%d", &n); while (n--) { cin >> a >> b; if (people.find(a) == people.end()) { total++; people[a] = total; pre[total] = total; sum[total] = 1; } if (people.find(b) == people.end()) { total++; people[b] = total; pre[total] = total; sum[total] = 1; } int ans = join(people[a], people[b]); printf("%d\n", ans); } } } return 0;}
0 0
- hdu 3172 Virtual Friends
- hdu 3172 Virtual Friends
- hdu 3172:Virtual Friends
- hdu 3172 Virtual Friends
- hdu 3172 Virtual Friends
- HDU 3172 Virtual Friends
- hdu 3172 Virtual Friends
- hdu 3172 Virtual Friends
- HDU 3172 Virtual Friends
- HDU 3172 Virtual Friends
- HDU-3172-Virtual Friends
- HDU 3172Virtual Friends()
- hdu 3172 Virtual Friends
- hdu 3172 virtual friends 并查集
- HDU 3172 Virtual Friends (并查集)
- 【并查集】 HDU 3172 Virtual Friends
- hdu 3172Virtual Friends (map水过)
- hdu 3172 Virtual Friends(并查集)
- 1001. A+B Format
- HW2171是一款低成本高性能高集成度2.4GHz无线SOC芯片
- HDU 2243 考研路茫茫——单词情结 AC自动机+DP+快速幂
- 最长回文子串
- Thread Dump与Analyzer
- HDU-3172-Virtual Friends
- Chrome,FireFox开发者工具无法找到js?
- postman post/get 方法
- C#中datagridview使用tooltip控件显示单元格内容与datagridview自带的tooltip显示单元格内容的方法
- CodeForces 606B Testing Robots【读题题QAQ】
- 安卓低功耗蓝牙开发
- Easy 191题 Number of 1 Bits
- This app has been built with an incorrect configuration. Please configure your build for VectorDrawa
- HTML 18.3 表单元素input的输入属性type