题目链接:
简述:
两道题均为基础的并查集问题,但是都要注意细节。
自环:例如 1 2 2 1 0 0 false
空树:例如 0 0 true
成环:当输入两个节点 a b。 分别寻找到他们的祖先,如果他们的祖先为同一个,那么连接a和b,将会出现环。
例如:1 2 1 3 2 3 0 0 false
性质:利用性质,如果有N个节点,如果成立,那么必须有N-1条边进行相连。
根节点: 只能有一个根节点,如果根节点不唯一,将会构成森林(森林不是树)。例如:1 2 3 4 0 0 false
HDU 1325/POJ 1308 Is It A Tree? :
HDU 1773 为无向图 ,HDU 1325 为有向图。解决HDU1325同样要注意上面五个细节,并且需要注意入度的问题。
入度:因为是有向图,因此只能有一个节点的入度为0,并且该点必定为根节点。除了根节点其余各个节点的入度必须为1.
方向:必须是父亲指向儿子,不能翻着来。例如 : 2 1 3 1 4 1 0 0 false
例如: 1 2 1 3 1 4 0 0 true
代码实现:
HDU 1773 小希的迷宫:
样例:
/*
0 0
1 1 0 0
1 2 2 1 0 0
1 2 2 3 4 5 0 0
1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 1 0 0
1 2 1 2 0 0
Yes
No
No
No
No
No
*/
代码:
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 1e5+10;
int parent[maxn],Rank[maxn],vis[maxn];
int mark,linecount,minnode,maxnode;
int find_root(int x) {
int x_root = x;
while(parent[x_root] != x_root) {
x_root = parent[x_root];
}
return x_root;
}
void merge(int x,int y) {
int x_root = find_root(x);
int y_root = find_root(y);
if(x_root != y_root) {
if(Rank[x_root] > Rank[y_root]) parent[y_root] = x_root; //压缩路径
else if(Rank[y_root] > Rank[x_root]) parent[x_root] = y_root;
else{
Rank[x_root]++;
} parent[y_root] = x_root;
linecount++; // 记录有多少有效的连接
} else { // 成环;
mark = 1;
}
}
int main() {
int a,b,k = 0,count = 0,p = 0;
minnode = 1e9;
for(int i = 0; i < maxn; i++) {
parent[i] = i;
}
while(~scanf("%d%d",&a,&b) && a >= 0 && b >= 0) {
if(a == 0 && b == 0) { // 空树。
printf("Yes\n");
continue;
}
while(true) {
minnode = min(minnode,min(a,b)); // 寻找最小下标
maxnode = max(maxnode,max(a,b)); // 寻找最大下标
if(!vis[a]) {//判断该节点是否存在
count++ ;// 记录节点数目
vis[a] = 1;
}
if(!vis[b]) { // 同上
count++;
vis[b] = 1;
}
merge(a,b);
scanf("%d%d",&a,&b);
if(a == 0 || b == 0) break;
}
for(int i = minnode; i <= maxnode; i++) {
if(vis[i]) {
if( parent[i] == i ) p++; // 根节点只能有一个;
parent[i] = i; //初始化
vis[i] = 0; //初始化
Rank[i] = 0;
}
}
if(mark) { // 有环;
printf("No\n");
} else {
if(linecount + 1 != count) { // n个节点,必然有n-1条边
printf("No\n");
} else {
if( p == 1) { // 根节点只能有一个;
printf("Yes\n");
} else {
printf("No\n");
}
}
}
minnode = 1e9; // 初始化。
maxnode = 0;
count = 0;
linecount = 0;
mark = 0;
p = 0;;
}
return 0;
}
HDU 1325/POJ 1308 Is It A Tree? :
样例:
/*
0 0
1 1 0 0
1 2 2 1 0 0
1 2 2 3 3 4 4 1 0 0
1 2 2 3 3 1 5 6 0 0
2 3 0 0
Case 1 is a tree.
Case 2 is not a tree.
Case 3 is not a tree.
Case 4 is not a tree.
Case 5 is not a tree.
Case 6 is a tree.
*/
代码:
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 1e5+10;
int parent[maxn],Rank[maxn],vis[maxn],father[maxn];
int mark,linecount,minnode,maxnode;
int find_root(int x) {
int x_root = x;
while(parent[x_root] != x_root) {
x_root = parent[x_root];
}
return x_root;
}
void merge(int x,int y) {
int x_root = find_root(x);
int y_root = find_root(y);
if(x_root != y_root) {
parent[y_root] = x_root;
linecount++; // 记录有多少有效的连接
} else { // 成环;
mark = 1;
}
}
int main() {
int a,b,k = 0,count = 0,p = 0,f = 0 , s = 0;
minnode = 1e9;
for(int i = 0; i < maxn; i++) {
parent[i] = i;
}
while(~scanf("%d%d",&a,&b) && a >= 0 && b >= 0) {
if(a == 0 && b == 0) { // 空树。
printf("Case %d is a tree.\n",++k);
continue;
}// 6 8
while(true) {
minnode = min(minnode,min(a,b)); // 寻找最小下标
maxnode = max(maxnode,max(a,b)); // 寻找最大下标
father[b]++; //入度。
if(!vis[a]) {//判断该节点是否存在
count++ ;// 记录节点数目
vis[a] = 1;
}
if(!vis[b]) { // 同上
count++;
vis[b] = 1;
}
merge(a,b);
scanf("%d%d",&a,&b);
if(a == 0 || b == 0) break;
}
for(int i = minnode; i <= maxnode; i++) {
if(vis[i]) {
if( parent[i] == i ) p++; // 根节点只能有一个;
if( father[i] == 0) f++; // 入度为 0 的只有一个
if(father[i] > 1) s++; // 每一个节点的入度不能超过一个
parent[i] = i; //初始化
father[i] = 0; // 初始化;
vis[i] = 0; //初始化
}
}
if(mark) { // 有环;
printf("Case %d is not a tree.\n",++k);
} else {
if(linecount + 1 != count) { // n个节点,必然有n-1条边
printf("Case %d is not a tree.\n",++k);
} else {
if(p == 1 && f == 1 && s == 0) {
printf("Case %d is a tree.\n",++k);
} else {
printf("Case %d is not a tree.\n",++k);
}
}
}
minnode = 1e9; // 初始化。
maxnode = 0;
count = 0;
linecount = 0;
mark = 0;
p = 0;
f = 0;
s = 0;
}
return 0;
}
完美撒花!