1、POJ 2524 Ubiquitous Religions
最基础的并查集。
#include <iostream>
using namespace std;
int s[50005], hight[50005];
void init(int n)
{
for (int i = 1; i <= n + 5; i++)
{
s[i] = i;
hight[i] = 1;
}
}
int getroot(int x)
{
if (s[x] != x)
x = getroot(s[x]);
return s[x];
}
void merge(int x, int y)
{
x = getroot(x);
y = getroot(y);
if (hight[x] == hight[y])
{
hight[x]++;
s[y] = x;
}
else
{
if (hight[x] > hight[y])
s[y] = x;
else
s[x] = y;
}
}
int main()
{
ios::sync_with_stdio(false);
int n, m, x, y;
int tmp = 0;
while (cin >> n >> m)
{
if (!n && !m)
break;
int ans = 0;
init(n);
while (m--)
{
cin >> x >> y;
merge(x, y);
}
for (int i = 1; i <= n; i++)
{
if (i == s[i])
ans++;
}
cout << "Case " << ++tmp << ": ";
cout << ans << endl;
}
return 0;
}
2、POJ1611 The Suspects
类似于并查集的连通问题。
#include <iostream>
using namespace std;
int s[30005], sum[30005]; //sum用来记录当前集合的元素个数,s为并查集
void init(int n) //初始话
{
for (int i = 0; i < n; i++)
{
s[i] = i;
sum[i] = 1;
}
}
int getroot(int x) //压缩状态
{
if (x != s[x])
s[x] = getroot(s[x]);
return s[x];
}
int merge(int x, int y) //集合的交
{
x = getroot(x);
y = getroot(y);
if (x != y)
{
s[y] = x;
sum[x] += sum[y]; //加到x上
}
}
int main()
{
ios::sync_with_stdio(false);
int n, m, t, x, y;
while (cin >> n >> m)
{
if (n == 0 && m == 0)
break;
init(n);
while (m--)
{
cin >> t;
cin >> x; //多组数据的处理
t--;
while (t--)
{
cin >> y;
merge(x, y);
}
}
cout << sum[getroot(0)] << endl;
}
return 0;
}
3、POJ 1703 Find them, Catch them
并查集提高题,并查集维护不同集合的关系。
#include <iostream>
using namespace std;
const int MAXN = (1e5 + 5) * 2;
int s[MAXN], high[MAXN]; //存根
void itin(int n) //初始化
{
for (int i = 1; i <= 2 * n; i++)
{
s[i] = i;
high[i] = 1;
}
}
int getroot(int x) //压缩状态
{
if (x != s[x])
s[x] = getroot(s[x]);
return s[x];
}
void merge(int x, int y) //合并优化
{
x = getroot(x);
y = getroot(y);
if (x == y)
return;
if (high[x] == high[y])
{
s[y] = x;
high[x]++;
}
else
{
if (high[x] > high[y])
s[y] = x;
else
s[x] = y;
}
}
bool same(int x, int y) //判断函数
{
x = getroot(x);
y = getroot(y);
return x == y;
}
int main()
{
int t, n, m, x, y;
char ch;
cin >> t;
while (t--)
{
cin >> n >> m;
itin(n);
while (m--)
{
cin >> ch;
cin >> x >> y;
if (ch == 'A') //put
{
if (same(x, y) || same(x + n, y + n))
cout << "In the same gang." << endl;
else if (same(x + n, y) || same(x, y + n))
cout << "In different gangs." << endl;
else
cout << "Not sure yet." << endl;
}
else //merge
{
merge(x + n, y);
merge(x, y + n);
}
}
}
return 0;
}
4、POJ 2236 Wireless Network
与坐标有关的并查集。
处理方法,结构体存放坐标,其他与普通并查集相同。
#include <iostream>
#include <math.h>
using namespace std;
int s[1005], high[1005];
struct zb
{
int x, y, flag; //flag:1代表修好,0代表没修好
} num[1005];
double fx(int x1, int y1, int x2, int y2)
{
return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
int find_set(int x)
{
if (x != s[x])
s[x] = find_set(s[x]);
return s[x];
}
bool some(int x, int y) //check
{
return find_set(x) == find_set(y);
}
void union_set(int x, int y) //merge
{
x = find_set(x);
y = find_set(y);
if (x == y)
return;
if (high[x] == high[y])
{
s[y] = x;
high[x]++;
}
else if (high[x] > high[y])
s[y] = x;
else
s[x] = y;
}
int main()
{
ios::sync_with_stdio(false);
int n, D;
char ch;
cin >> n >> D;
for (int i = 1; i <= n; i++) //在存入坐标的同时进行初始化
{
cin >> num[i].x >> num[i].y;
num[i].flag = 0;
s[i] = i;
high[i] = 0;
}
int t1, t2;
while (cin >> ch)
{
if (ch == 'O') //合并结构体
{
cin >> t1;
num[t1].flag = 1; //只有修好的情况下才能合并
for (int i = 1; i <= n; i++)
{
if (t1 == i)
continue;
if (fx(num[i].x, num[i].y, num[t1].x, num[t1].y) <= D && num[i].flag)
union_set(i, t1);
}
}
else //检查结构体
{
cin >> t1 >> t2;
if (some(t1, t2))
cout << "SUCCESS" << endl;
else
cout << "FAIL" << endl;
}
}
return 0;
}