hdoj3926-Hand in Hand(同构图的判断)
来源:互联网 发布:正新鸡排 知乎 编辑:程序博客网 时间:2024/05/17 02:17
题目链接
思路
1.这个图就是同构图的判断,对于题目中的孩子的牵手方式,因为每个人只有两只手,所以可以看成图中每个节点的读书不大于2,因此连接方式只能是环或者链;
2.然后我们只用并查集来判断每个图含有多少环,多少链,然后对于环和链,用其中的一个节点来记录环或者链所含有节点个数;
3.然后将所有节点按是否成环和节点的数目来排序,最后再遍历判断一次结果。
code
#include <iostream> #include <algorithm> #include <cstring> #include <fstream>using namespace std;const int MAX = 10005;class Node{public: int isCircle, count;//是否成环,以及节点的个数};Node node1[MAX], node2[MAX];int f[MAX];int cmp(Node a, Node b) {if(a.isCircle == b.isCircle) {return a.count > b.count;} return a.isCircle > b.isCircle;}void init() {for(int i = 0; i < MAX; ++ i) {f[i] = i;}}int find(int x) {if(x != f[x]) {f[x] = find(f[x]);}return f[x];}int main() { //ifstream cin("data.in"); int t, cnt = 1; cin >> t; while(t --) { int n1, n2, m1, m2; bool ok = true; for(int i = 0; i < MAX; ++ i) { node1[i].isCircle = 0;//初始化每个孩子为单独个体,且不成环,看做节点数为1的链 node2[i].isCircle = 0; node1[i].count = 1; node2[i].count = 1; }cin >> n1 >> m1; init(); for(int i = 0; i < m1; i ++) { int x, y; cin >> x >> y; x = find(x); y = find(y); if(x != y) { //当两条链不成环时,连接两条链 f[y] = x; node1[x].count += node1[y].count; node1[y].count = 0; } else { node1[y].isCircle = 1;//成环时进行标记 } } cin >> n2 >> m2; init(); for(int i = 0; i < m2; i ++) { int x, y; cin >> x >> y; x = find(x); y = find(y); if(x != y) { f[y] = x; node2[x].count += node2[y].count; node2[y].count = 0; } else { node2[y].isCircle = 1; } } if(n1 != n2 || m1 != m2) { ok = false; } else { sort(node1+1, node1+n1+1, cmp); sort(node2+1, node2+n2+1, cmp); for(int i = 1; i < n1; i ++) { if(node1[i].isCircle != node2[i].isCircle || node1[i].count != node2[i].count){ ok = false; break; } } } cout << "Case #" << cnt ++ << ": "; if(ok) { cout << "YES" << endl; } else { cout << "NO" << endl; } } return 0;}
0 0
- hdoj3926-Hand in Hand(同构图的判断)
- 简单同构图 hdoj3926 Hand in Hand
- hdu3926 Hand in Hand 同构图★
- hdu 3926 Hand in Hand (同构图)
- Hdu-3926 Hand in Hand(同构图)
- 【HDU】3926 Hand in Hand 同构图
- HDU 3926 Hand in Hand 同构图
- HDU 3926 Hand in Hand (同构图)
- hdu 3926 hand in hand 同构图
- HDU 3926 Hand in Hand(同构图)
- hdu 3926 Hand in Hand(同构图)
- Hand in Hand 并查集 同构图
- HDU 3926 Hand in Hand 同构图★
- hdu 3826 Hand in Hand 同构图★
- HDU3926 Hand in Hand, 图形同构判断
- Hand in hand lyrics
- HDU3926 - Hand in Hand(判断入度最大为2的无向图的同构)
- HDU 3926 Hand in Hand(判断同构)
- 整数和数组转化为字符串
- Fragment 碎片
- 人工智能重大进展!全球首个光电子神经网络问世
- 变量a进行定义
- 个人记录-LeetCode 33. Search in Rotated Sorted Array
- hdoj3926-Hand in Hand(同构图的判断)
- tp5:为什么find()出来的数据有时候可以用toArray() 有时候会报错?
- MyEclipse2015 json文件报错
- 散列表
- 排序算法总结
- iozone与mdtest使用简介
- Spring Boot中使用RabbitMQ
- TensorBoard--TensorFlow可视化
- LightOJ-1085-树状数组,离散化,dp