聲明一下:POJ的cin,cout很難用,有時候會TLE,解除同步也不行。推薦使用scanf,printf。
1、Find them, Catch them (POJ 1703)
方法一:帶權並查集
#include <iostream>
#include <stdio.h>
using namespace std;
const int MAXN = 1e5 + 5;
int s[MAXN], d[MAXN]; //d 0 同派,1不同派別
void init(int n)//初始化
{
for (int i = 1; i <= n; i++)
{
s[i] = i;
d[i] = 0;
}
}
int find_set(int x)//壓縮狀態
{
if (x != s[x])
{
int t = s[x];
s[x] = find_set(s[x]);
d[x] = (d[x] + d[t]) % 2;//更新關係,動手畫下圖
}
return s[x];
}
void union_set(int x, int y)//合併
{
int rx = find_set(x), ry = find_set(y);
if (rx != ry)
{
s[rx] = ry;
d[rx] = (d[y] + 1 - d[x]) % 2;//更新狀態
}
}
bool some_set(int x, int y)//檢查是否入過並查集,沒如果就是不確定
{
return find_set(x) == find_set(y);
}
int main()
{
int t;
scanf("%d",&t);
while (t--)
{
int n, m, x, y;
char ch;
scanf("%d%d",&n,&m);
init(n);
while (m--)
{
scanf(" %c %d%d",&ch,&x,&y);
if (ch == 'D') //union
union_set(x, y);
else
{
bool tmp = some_set(x, y);
if (tmp && (d[x] == d[y]))
printf("In the same gang.\n");
else if (tmp && (d[x] != d[y]))
printf("In different gangs.\n");
else
printf("Not sure yet.\n");
}
}
}
return 0;
}
方法二:種類並查集
#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;
}
2、A Bug’s Life POJ 2492
跟上一題的更新關係是一樣的。
#include <iostream>
#include <stdio.h>
using namespace std;
const int MAXN = 2010;
int s[MAXN], sex[MAXN];
bool flag;
void init(int n)
{
flag = 0;
for (int i = 1; i <= n; i++)
{
s[i] = i;
sex[i] = 0;
}
}
int find_set(int x)
{
if (x != s[x])
{
int t = s[x];
s[x] = find_set(s[x]);
sex[x] = (sex[t] + sex[x]) % 2;
}
return s[x];
}
void union_set(int x, int y)
{
int rootx = find_set(x), rooty = find_set(y);
if (rootx == rooty)
{
if (sex[x] == sex[y])
flag = 1;
}
else
{
s[rootx] = rooty;
sex[rootx] = (sex[x] - sex[y] + 1) % 2;
}
}
int main()
{
//ios::sync_with_stdio(false);
int t, m, n;
int k = 0, x, y;
scanf("%d", &t);
while (t--)
{
scanf("%d%d", &n, &m);
init(n);
while (m--)
{
scanf("%d%d", &x, &y);
if (flag)
continue;
union_set(x, y);
}
cout << "Scenario #" << ++k << ":\n";
if (flag)
cout << "Suspicious bugs found!\n\n";
else
cout << "No suspicious bugs found!\n\n";
}
return 0;
}
3、Cube Stacking POJ 1988
#include <iostream>
using namespace std;
const int MAXN = 30005;
int s[MAXN], fro[MAXN], sum[MAXN];
void init()
{
for (int i = 1; i <= MAXN; i++)
{
s[i] = i;
sum[i] = 1;
}
}
int find_set(int x) //路徑壓縮
{
if (x != s[x])
{
int t = s[x];
s[x] = find_set(s[x]);
fro[x] += fro[t]; //當前點到根節點距離
}
return s[x];
}
void union_set(int x, int y)
{
int rootx = find_set(x), rooty = find_set(y);
if (rootx != rooty)
{
s[rooty] = rootx;
fro[rooty] = sum[rootx];
sum[rootx] += sum[rooty]; //總樹的長度
}
}
int main()
{
ios::sync_with_stdio(false);
int p, x, y;
char ch;
init();
cin >> p;
while (p--)
{
cin >> ch;
if (ch == 'M') //並
{
cin >> x >> y;
union_set(x, y);
}
else //統計
{
cin >> x;
int t = find_set(x);
cout << (sum[t] - fro[x] - 1) << endl;
}
}
return 0;
}
4、食物鏈 POJ 1182
#include <iostream>
#include <stdio.h>
using namespace std;
const int MAXN = 50005;
int s[MAXN], rea[MAXN]; //0同級,1 吃 2 被吃
int ans;
void init(int n)
{
for (int i = 1; i <= n; i++)
{
s[i] = i;
rea[i] = 0;
}
}
int find_set(int x)
{
if (x != s[x])
{
int rx = s[x];
s[x] = find_set(s[x]);
rea[x] = (rea[x] + rea[rx]) % 3; //更新關係
}
return s[x];
}
void union_set(int x, int y, int jud)
{
int rootx = find_set(x), rooty = find_set(y);
if (rootx == rooty)
{
if ((jud - 1) != (rea[x] - rea[y] + 3) % 3)
ans++;
}
else //union
{
s[rootx] = rooty;
rea[rootx] = (rea[y] - rea[x] + (jud - 1)) % 3;
}
}
int main()
{
int n, k;
scanf("%d%d", &n, &k);
init(n);
while (k--)
{
int tmp, x, y;
scanf("%d%d%d", &tmp, &x, &y);
if (x > n || y > n || (tmp == 2 && x == y))
{
ans++;
continue;
}
union_set(x, y, tmp);
}
printf("%d\n", ans);
return 0;
}
5、Dragon Balls HDU 3635
#include <iostream>
using namespace std;
const int MAXN = 10005;
int s[MAXN], sum[MAXN], move1[MAXN];
void init(int n)
{
for (int i = 1; i <= n; i++)
{
s[i] = i;
sum[i] = 1;
move1[i] = 0;
}
}
int find_set(int x)
{
if (x != s[x])
{
int t = s[x];
s[x] = find_set(s[x]);
move1[x] += move1[t];
}
return s[x];
}
void union_set(int x, int y)
{
int rx = find_set(x), ry = find_set(y);
if (rx != ry)
{
s[rx] = ry;
sum[ry] += sum[rx];
sum[rx] = 0;
move1[rx] = 1;
}
}
int main()
{
ios::sync_with_stdio(false);
int t, tmp = 0, n, q;
cin >> t;
while (t--)
{
char ch;
int x, y; //x->y
cin >> n >> q;
init(n);
cout << "Case " << ++tmp << ":\n";
while (q--)
{
cin >> ch;
if (ch == 'T') //move
{
cin >> x >> y;
union_set(x, y);
}
else
{
cin >> x;
int k = find_set(x);
cout << s[k] << " " << sum[k] << " " << move1[x] << endl;
}
}
}
return 0;
}
6、More is better HDU 1856
求樹的最大子的個數。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <bitset>
#include <cstdlib>
#include <cmath>
#include <set>
#include <list>
#include <deque>
#include <map>
#include <queue>
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const int MAXN = 10000000;
int s[MAXN], sum[MAXN], ans = 1;
void init()
{
for (int i = 1; i <= MAXN; i++)
{
s[i] = i;
sum[i] = 1;
}
}
int find_set(int x)
{
if (x != s[x])
s[x] = find_set(s[x]);
return s[x];
}
void union_set(int x, int y)
{
int rx = find_set(x), ry = find_set(y);
if (rx != ry)
{
s[rx] = ry;
sum[ry] += sum[rx];//樹的子的個數
ans = (sum[ry] > ans ? sum[ry] : ans);
}
}
int main()
{
int n;
while (~scanf("%d", &n))
{
init();
int x, y;
ans = 1;
while (n--)
{
scanf("%d%d", &x, &y);
union_set(x, y);
}
cout << ans << endl;
}
return 0;
}
7、Is It A Tree? HUD 1325
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <bitset>
#include <cstdlib>
#include <cmath>
#include <set>
#include <list>
#include <deque>
#include <map>
#include <queue>
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const int MAXN = 5005;
int s[MAXN], vis[MAXN], maxx = 0, tp;
bool flag;
void init()
{
for (int i = 1; i <= MAXN; i++)
{
s[i] = i;
vis[i] = 0;
}
maxx = 0;
flag = 0;
tp = 0;
}
int find_set(int x)
{
if (x != s[x])
s[x] = find_set(s[x]);
return s[x];
}
void union_set(int x, int y)
{
int rx = find_set(x), ry = find_set(y);
if (rx == ry || ry != y) //有向邊,子節點時根節點,否則會樹會亂
{
flag = 1;
return;
}
if (rx != ry)
s[ry] = rx;
}
int main()
{
int x, y, tmp = 0, x1, y1;
while (~scanf("%d%d", &x, &y))
{
if (x < 0 && y < 0)
break;
if (x == 0 && y == 0)
printf("Case %d is a tree.\n", ++tmp);
else
{
init();
union_set(x, y);
maxx = max(maxx, max(x, y));
vis[x] = vis[y] = 1;
while (~scanf("%d%d", &x1, &y1) && (x1 + y1))
{
vis[x1] = vis[y1] = 1;
maxx = max(maxx, max(x1, y1));
union_set(x1, y1);
}
for (int i = 1; i <= maxx; i++)//求樹的個數,只能是1或0棵
if (vis[i] && s[i] == i)
++tp;
if (!flag && tp <= 1)
printf("Case %d is a tree.\n", ++tmp);
else
printf("Case %d is not a tree.\n", ++tmp);
}
}
return 0;
}