博客
关于我
算法题_朋友圈
阅读量:534 次
发布时间:2019-03-08

本文共 913 字,大约阅读时间需要 3 分钟。

在这里插入图片描述

在这里插入图片描述

思路: 这是一个并查集问题。

接下来我们用C++进行编程:

#include 
using namespace std;const int MAX_N = 2e5 + 5;int par[MAX_N], cnt[MAX_N];unordered_map
umap;void init(int n){ for(int i = 0; i < n; i++) { par[i] = i; cnt[i] = 1; }}//查找根结点int find(int x){ if(par[x] == x) return x; else return par[x] = find(par[x]);}//合并void joint(int x, int y){ if(find(x) == find(y)) return; cnt[par[y]] += cnt[par[x]]; par[par[x]] = par[y];}int main(){ int T; cin >> T; while(T--) { int n; cin >> n; int x, y; init(2 * n + 5); umap.clear(); int cntv = 0; for(int i = 0; i < n; i++) { cin >> x >> y; if(umap.find(x) == umap.end()) { ++cntv; umap[x] = cntv; } if(umap.find(y) == umap.end()) { ++cntv; umap[y] = cntv; } joint(umap[x], umap[y]); } int result = 0; for(int i = 0; i <= cntv; i++) result = max(result, cnt[find(i)]); cout << result << endl; } return 0;}

转载地址:http://hsbiz.baihongyu.com/

你可能感兴趣的文章
mysql 存储过程每隔一段时间执行一次
查看>>
mysql 存在update不存在insert
查看>>
Mysql 学习总结(86)—— Mysql 的 JSON 数据类型正确使用姿势
查看>>
Mysql 学习总结(87)—— Mysql 执行计划(Explain)再总结
查看>>
Mysql 学习总结(88)—— Mysql 官方为什么不推荐用雪花 id 和 uuid 做 MySQL 主键
查看>>
Mysql 学习总结(89)—— Mysql 库表容量统计
查看>>
mysql 实现主从复制/主从同步
查看>>
mysql 审核_审核MySQL数据库上的登录
查看>>
mysql 导入 sql 文件时 ERROR 1046 (3D000) no database selected 错误的解决
查看>>
mysql 导入导出大文件
查看>>
mysql 将null转代为0
查看>>
mysql 常用
查看>>
MySQL 常用列类型
查看>>
mysql 常用命令
查看>>
Mysql 常见ALTER TABLE操作
查看>>
mysql 往字段后面加字符串
查看>>
mysql 快速自增假数据, 新增假数据,mysql自增假数据
查看>>
Mysql 报错 Field 'id' doesn't have a default value
查看>>
MySQL 报错:Duplicate entry 'xxx' for key 'UNIQ_XXXX'
查看>>
mysql 排序id_mysql如何按特定id排序
查看>>