转载一个并查集的讲解:
http://blog.csdn.net/pure_life/article/details/2922118
A:Fool Game
B:Is It A Tree?(poj1308)
题目:
A tree is a well-known data structure that is either empty (null, void, nothing) or is a set of one or more nodes connected by directed edges between nodes satisfying the following properties.
There is exactly one node, called the root, to which no directed edges point.
Every node except the root has exactly one edge pointing to it.
There is a unique sequence of directed edges from the root to each node.
For example, consider the illustrations below, in which nodes are represented by circles and edges are represented by lines with arrowheads. The first two of these are trees, but the last is not.
In this problem you will be given several descriptions of collections of nodes connected by directed edges. For each of these you are to determine if the collection satisfies the definition of a tree or not.
Input
The input will consist of a sequence of descriptions (test cases) followed by a pair of negative integers. Each test case will consist of a sequence of edge descriptions followed by a pair of zeroes Each edge description will consist of a pair of integers; the first integer identifies the node from which the edge begins, and the second integer identifies the node to which the edge is directed. Node numbers will always be greater than zero.
Output
For each test case display the line “Case k is a tree.” or the line “Case k is not a tree.”, where k corresponds to the test case number (they are sequentially numbered starting with 1).
Sample Input
6 8 5 3 5 2 6 4
5 6 0 0
8 1 7 3 6 2 8 9 7 5
7 4 7 8 7 6 0 0
3 8 6 8 6 4
5 3 5 6 5 2 0 0
-1 -1
Sample Output
Case 1 is a tree.
Case 2 is a tree.
Case 3 is not a tree.
思路:(copy)
非树:
1.多根
2.多个入边
3.环
4.重边
5.自己不能指向自己
注意:空树是树
我的代码
#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 double eps = 1e-6;
const int INF = 1000000000;
const int maxn = 100000;
int T,n,m;
bool flag;
int root_num;
//num表示该节点数量(是否存在,0或1)
//in表示指向该节点的节点数
struct node{
int num;
int root,in;
};
node a[maxn];
int find_root(int i){
if (a[i].root != i) {
return a[i].root = find_root(a[i].root);
}
return a[i].root;
}
int union_set(int i, int j){
int x = find_root(i);
int y = find_root(j);
if (x!=y) {
return a[j].root = x;
}
return x;
}
void init(){
for (int i = 0; i < maxn; i++) {
a[i].num = 0;
a[i].root = i;
a[i].in = 0;
}
}
int main() {
// freopen("input.txt", "r", stdin);
T = 0;
init();
flag = true;
root_num = 0;
while(scanf("%d %d",&m,&n)!=EOF && m>=0 && n>=0) {
if ((m == 0 && n == 0) ||flag == false) {
if (flag == false) {
T++;
printf("Case %d is not a tree.\n", T);
}else{
for (int i = 0; i <maxn; i++) {
if (a[i].num == 1 && find_root(i) == i) {
root_num++;
if (root_num > 1) {
flag =false;
}
}
if (a[i].in > 1) {
flag = false;
break;
}
}
T++;
if (flag == false) {
printf("Case %d is not a tree.\n", T);
}else{
printf("Case %d is a tree.\n",T);
}
}
init();
root_num = 0;
flag = true;
}else{
//无环
if ((m != n && find_root(m) == find_root(n)) || m ==n) {
flag = false;
}else{
a[m].num = a[n].num = 1;
a[n].in++;
union_set(m, n);
}
}
}
return 0;
}
B:Cube Stacking(POJ1988)
参考博客:
http://blog.csdn.net/ascii991/article/details/7536826
题目
Farmer John and Betsy are playing a game with N (1 <= N <= 30,000)identical cubes labeled 1 through N. They start with N stacks, each containing a single cube. Farmer John asks Betsy to perform P (1<= P <= 100,000) operation. There are two types of operations:
moves and counts.
* In a move operation, Farmer John asks Bessie to move the stack containing cube X on top of the stack containing cube Y.
* In a count operation, Farmer John asks Bessie to count the number of cubes on the stack with cube X that are under the cube X and report that value.
Write a program that can verify the results of the game.
Input
Line 1: A single integer, P
Lines 2..P+1: Each of these lines describes a legal operation. Line 2 describes the first operation, etc. Each line begins with a ‘M’ for a move operation or a ‘C’ for a count operation. For move operations, the line also contains two integers: X and Y.For count operations, the line also contains a single integer: X.
Note that the value for N does not appear in the input file. No move operation will request a move a stack onto itself.
Output
Print the output from each of the count operations in the same order as the input file.
Sample Input
6
M 1 6
C 1
M 2 4
M 2 6
C 3
C 4
Sample Output
1
0
2
题意
有几个stack,初始里面有一个cube。支持两种操作:1.move x y: 将x所在的stack移动到y所在stack的顶部。2.count x:数在x所在stack中,在x之下的cube的个数。
我的代码
#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 double eps = 1e-6;
const int INF = 1000000000;
const int maxn = 30005;
int T,n,m,N;
int fa[maxn],height[maxn],ret[maxn];
char ope;
void make_set(int n){
for (int i = 1; i <= n; i ++) {
fa[i] = i;
height[i] = 1;
ret[i] = 0;
}
}
int find_set(int x){
if (fa[x] == x) {
return x;
}else{
int temp = fa[x];
fa[x] = find_set(fa[x]);
ret[x] += ret[temp];
height[x] = 0;
}
return fa[x];
}
void union_set(int x, int y){
int a = find_set(x);
int b = find_set(y);
if (a == b) {
return;
}
fa[a] = b;
ret[a] += height[b];
height[b] += height[a];
height[a] = 0;
}
int main() {
freopen("input.txt", "r", stdin);
int a,b;
scanf("%d\n",&N);
//cin >> N;
make_set(maxn);
for (int i = 0; i < N; i++) {
// cin >> ope;
scanf("%c", &ope);
if (ope == 'M') {
scanf("%d %d\n",&a,&b );
// cin >> a >> b;
union_set(a,b);
}else{
scanf("%d\n",&a);
// cin >> a;
find_set(a);
printf("%d\n",ret[a]);
}
}
return 0;
}