本文主要是介绍【并查集】 HDU 1272 小希的迷宫,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
HDU 1272 小希的迷宫
需要判断是否是连通图。
#include <iostream>
#include <string>
#include <algorithm>
#include <math.h>
#include <stdio.h>
#include <cstring>
#include <stdlib.h>
using namespace std;int father[100001], vis[100001];int get_fa(int x)
{if(x != father[x]) father[x] = get_fa(father[x]);return father[x];
}int main()
{for(int i = 0;i <= 100001; i++)father[i] = i;int flag = 0, cnt = 0;int maxx = -0x3f3f3f3f;memset(vis, 0, sizeof(vis));while(1){int a, b;scanf("%d%d", &a, &b);if(a == 0 && b == 0){for(int i = 1; i <= maxx; i ++)if(get_fa(i) == i && vis[i] == 1) cnt++;if(flag == 0 && cnt <= 1) puts("Yes");else puts("No");flag = 0;cnt = 0;maxx = -0x3f3f3f3f;for(int i = 0;i <= 100001; i++)father[i] = i, vis[i] = 0;continue;}if(a == -1 && b == -1) break;maxx = max(maxx, a);maxx = max(maxx, b);vis[a] = vis[b] = 1;a = get_fa(a), b = get_fa(b);if(a != b) father[b] = a;else if(a == b) flag = 1;}return 0;
}
这篇关于【并查集】 HDU 1272 小希的迷宫的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!