### The Suspects

Description:
Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized as a global threat in mid-March 2003. To minimize transmission to others, the best strategy is to separate the suspects from others.
In the Not-Spreading-Your-Sickness University (NSYSU), there are many student groups. Students in the same group intercommunicate with each other frequently, and a student may join several groups. To prevent the possible transmissions of SARS, the NSYSU collects the member lists of all student groups, and makes the following rule in their standard operation procedure (SOP).
Once a member in a group is a suspect, all members in the group are suspects.
However, they find that it is not easy to identify all the suspects when a student is recognized as a suspect. Your job is to write a program which finds all the suspects.
Input

The input file contains several cases. Each test case begins with two integers n and m in a line, where n is the number of students, and m is the number of groups. You may assume that 0 < n <= 30000 and 0 <= m <= 500. Every student is numbered by a unique integer between 0 and n−1, and initially student 0 is recognized as a suspect in all the cases. This line is followed by m member lists of the groups, one line per group. Each line begins with an integer k by itself representing the number of members in the group. Following the number of members, there are k integers representing the students in this group. All the integers in a line are separated by at least one space.
A case with n = 0 and m = 0 indicates the end of the input, and need not be processed.

Output

For each case, output the number of suspects in one line.

Sample Input

100 4
2 1 2
5 10 13 11 12 14
2 0 1
2 99 2
200 2
1 5
5 1 2 3 4 5
1 0
0 0

Sample Output

4
1
1

Problem solving:

Code:

#include<iostream>
#include<cstdio>
using namespace std;
const int sijiaxiaozhu=3e4+10;
int p[sijiaxiaozhu],n,m,k,mi,mii;
int find(int x)
{
return p[x]!=x?p[x]=find(p[x]):x;
}
void join(int x,int y)
{
x=find(x),y=find(y);
if(x<y)    p[y]=x;
else    p[x]=y;
}
int main()
{
while(~scanf("%d %d",&n,&m))
{
if(n==0&&m==0)  break;
for(int i=0;i<n;i++)    p[i]=i;
for(int i=0;i<m;i++)
{
scanf("%d %d",&k,&mi);
for(int i=0;i<k-1;i++)
{
cin>>mii;
join(mi,mii);
}
}
int ans=1;
for(int i=1;i<n;i++)
{
find(i);
// cout<<p[i]<<" ";
if(p[i]==0) ans++;
}
cout<<ans<<endl;
}

}


### 畅通工程

Description:

Input

3 3
1 2
1 2
2 1

Output

Sample Input

4 2
1 3
4 3
3 3
1 2
1 3
2 3
5 2
1 2
3 5
999 0
0

Sample Output

1
0
2
998

Huge input, scanf is recommended.

Problem solving:

Code:

#include<bits/stdc++.h>
using namespace std;
const int sijiaxiaozhu=1e5;
int p[sijiaxiaozhu];
int find(int x)
{
return p[x]!=x?p[x]=find(p[x]):x;
}
void join(int x,int y)
{
x=find(x),y=find(y);
if(x!=y)    p[x]=y;
}
int main()
{
int n,m,a,b;
while(scanf("%d %d",&n,&m)&&n)
{
int ans=0;
for(int i=1;i<=n;i++)   p[i]=i;
for(int i=0;i<m;i++)
{
cin>>a>>b;
join(a,b);
}
for(int i=1;i<=n;i++)   if(p[i]==i) ans++;
cout<<ans-1<<endl;
}
}


### 还是畅通工程

Description:

Input

Output

Sample Input

3
1 2 1
1 3 2
2 3 4
4
1 2 1
1 3 4
1 4 1
2 3 3
2 4 2
3 4 5
0

Sample Output

3
5

Huge input, scanf is recommended.
Problem solving:

Code:

#include<bits/stdc++.h>
using namespace std;
const int maxn = 10005;
struct node{
int a,b,c;
}r[maxn];
int p[maxn];
bool cmp(node a,node b)
{
return a.c<b.c;
}
int find(int x)
{
return p[x]!=x?p[x]=find(p[x]):x;
}
void join(int x,int y)
{
x=find(x),y=find(y);
if(x!=y)    p[x]=y;
}
int main()
{
int n;
while(scanf("%d",&n)&&n)
{
int sum=0;
for(int i=1;i<=n;i++)   p[i]=i;
for(int i=0;i<n*(n-1)/2;i++)
scanf("%d %d %d",&r[i].a,&r[i].b,&r[i].c);
sort(r,r+n*(n-1)/2,cmp);
for(int i=0;i<n*(n-1)/2;i++)
{
if(find(r[i].a)!=find(r[i].b))
{
sum+=r[i].c;
join(r[i].a,r[i].b);
}
}
cout<<sum<<endl;
}
}


### Ubiquitous Religions

Description:
There are so many different religions in the world today that it is difficult to keep track of them all. You are interested in finding out how many different religions students in your university believe in.

You know that there are n students in your university (0 < n <= 50000). It is infeasible for you to ask every student their religious beliefs. Furthermore, many students are not comfortable expressing their beliefs. One way to avoid these problems is to ask m (0 <= m <= n(n-1)/2) pairs of students and ask them whether they believe in the same religion (e.g. they may know if they both attend the same church). From this data, you may not know what each person believes in, but you can get an idea of the upper bound of how many different religions can be possibly represented on campus. You may assume that each student subscribes to at most one religion.
Input

The input consists of a number of cases. Each case starts with a line specifying the integers n and m. The next m lines each consists of two integers i and j, specifying that students i and j believe in the same religion. The students are numbered 1 to n. The end of input is specified by a line in which n = m = 0.

Output

For each test case, print on a single line the case number (starting with 1) followed by the maximum number of different religions that the students in the university believe in.

Sample Input

10 9
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
10 4
2 3
4 5
4 8
5 8
0 0

Sample Output

Case 1: 1
Case 2: 7

Hint
Huge input, scanf is recommended.

Problem solving:

Code:

#include<cstdio>
using namespace std;
int n,m,a,b;
const int sijiaxiaozhu=5e4+10;
int p[sijiaxiaozhu];
int find(int x)
{
return p[x]!=x?p[x]=find(p[x]):x;
}
void join(int x,int y)
{
x=find(x),y=find(y);
if(x!=y)    p[x]=y;
}
int main()
{
int flag=1;
while(scanf("%d %d",&n,&m))
{
if(n==0&&m==0)  break;
for(int i=1;i<=n;i++)   p[i]=i;
for(int i=0;i<m;i++)
{
scanf("%d %d",&a,&b);
join(a,b);
}
int ans=0;
for(int i=1;i<=n;i++)
{
find(i);
if(p[i]==i) ans++;
}
printf("Case %d: %d\n",flag++,ans);
}
}


### Cube Stacking

Description:
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

Problem solving:

Code:

#include<iostream>
#include<cstdio>
using namespace std;
const int sijiaxiaozhu=31314;
int p[sijiaxiaozhu],ans[sijiaxiaozhu],now[sijiaxiaozhu];
int find(int x)
{
if(p[x]==x) return p[x];
int mid=p[x];
p[x]=find(p[x]);
ans[x]+=ans[mid];
return p[x];
}
void join(int x,int y)
{
int fx=find(x),fy=find(y);
if(fx==fy)  return ;
p[fx]=fy;
ans[fx]+=now[fy];
now[fy]+=now[fx];
now[fx]=0;
}
int main()
{
int n;
cin>>n;
for(int i=1;i<=sijiaxiaozhu;i++)
{
now[i]=1;
p[i]=i;
ans[i]=0;
}
for(int i=0;i<n;i++)
{
char s;
int a,b;
cin>>s;
if(s=='M')
{
scanf("%d %d",&a,&b);
join(a,b);
}
else
{
scanf("%d",&a);
find(a);
printf("%d\n",ans[a]);
}
}
}


### Dragon Balls

Description:
Five hundred years later, the number of dragon balls will increase unexpectedly, so it's too difficult for Monkey King(WuKong) to gather all of the dragon balls together.
His country has N cities and there are exactly N dragon balls in the world. At first, for the ith dragon ball, the sacred dragon will puts it in the ith city. Through long years, some cities' dragon ball(s) would be transported to other cities. To save physical strength WuKong plans to take Flying Nimbus Cloud, a magical flying cloud to gather dragon balls.
Every time WuKong will collect the information of one dragon ball, he will ask you the information of that ball. You must tell him which city the ball is located and how many dragon balls are there in that city, you also need to tell him how many times the ball has been transported so far.
Input

The first line of the input is a single positive integer T(0 < T <= 100).
For each case, the first line contains two integers: N and Q (2 < N <= 10000 , 2 < Q <= 10000).
Each of the following Q lines contains either a fact or a question as the follow format:
T A B : All the dragon balls which are in the same city with A have been transported to the city the Bth ball in. You can assume that the two cities are different.
Q A : WuKong want to know X (the id of the city Ath ball is in), Y (the count of balls in Xth city) and Z (the tranporting times of the Ath ball). (1 <= A, B <= N)

Output

For each test case, output the test case number formated as sample output. Then for each query, output a line with three integers X Y Z saparated by a blank space.

Sample Input

2
3 3
T 1 2
T 3 2
Q 2
3 4
T 1 2
Q 1
T 1 3
Q 1

Sample Output

Case 1:
2 3 0
Case 2:
2 2 1
3 3 2

Problem solving:

（还有一点坑了我就是，不要路径压缩。。。我记得板子是压缩了路径的，然后一直WA，一直WA。。。你需要一个查找距离的过程，如果路径压缩了，那每个移动距离都是1了，会破坏原本的构图。这毒瘤样例也没体现这个坑点。太坑了！！！

Code:

#include<bits/stdc++.h>
using namespace std;
const int sijiaxiaozhu = 1e5;
int p[sijiaxiaozhu],sum[sijiaxiaozhu];
int find(int x)
{
return p[x]!=x?find(p[x]):x;
}
int xiaozhu(int x)//这个就是那个查找移动次数的函数
{
int ans=0;
while(p[x]!=x)//其实跟并查集的find是差不多的的
{
x=p[x];
ans++;//没查找答案一次加一
}
return ans;
}
void join(int x,int y)
{
int fx=find(x),fy=find(y);
if(fx!=fy)
{
p[fx]=fy;
sum[fy]+=sum[fx];//这个就是龙珠转移的过程
sum[fx]=0;
}
}
int main()
{
int t,nu=0;
scanf("%d",&t);
while(t--)
{
int n,m;
scanf("%d %d",&n,&m);
for(int i=1;i<=n;i++)
{
p[i]=i;sum[i]=1;
}
printf("Case %d:\n",++nu);
while(m--)
{
char s[2];
scanf("%s",s);
if(s[0]=='T')
{
int a,b;
scanf("%d %d",&a,&b);
join(a,b);
}
if(s[0]=='Q')
{
int a;
scanf("%d",&a);
printf("%d %d %d\n",find(a),sum[find(a)],xiaozhu(a));
}
}
}
}


Description:
In 12th Zhejiang College Students Games 2007, there was a new stadium built in Zhejiang Normal University. It was a modern stadium which could hold thousands of people. The audience Seats made a circle. The total number of columns were 300 numbered 1--300, counted clockwise, we assume the number of rows were infinite.
These days, Busoniya want to hold a large-scale theatrical performance in this stadium. There will be N people go there numbered 1--N. Busoniya has Reserved several seats. To make it funny, he makes M requests for these seats: A B X, which means people numbered B must seat clockwise X distance from people numbered A. For example: A is in column 4th and X is 2, then B must in column 6th (6=4+2).
Now your task is to judge weather the request is correct or not. The rule of your judgement is easy: when a new request has conflicts against the foregoing ones then we define it as incorrect, otherwise it is correct. Please find out all the incorrect requests and count them as R.
Input

There are many test cases:
For every case:
The first line has two integer N(1<=N<=50,000), M(0<=M<=100,000),separated by a space.
Then M lines follow, each line has 3 integer A(1<=A<=N), B(1<=B<=N), X(0<=X\<300) (A!=B), separated by a space.

Output

For every case:
Output R, represents the number of incorrect request.

Sample Input

10 10
1 2 150
3 4 200
1 5 270
2 6 200
6 5 80
4 7 150
8 9 100
4 8 50
1 7 100
9 2 100

Sample Output

2

Hint
Hint:
（PS： the 5th and 10th requests are incorrect）

Problem solving:

Code:

#include<bits/stdc++.h>
using namespace std;
const int sijia=52113;
int f[sijia],sijiaxiaozhu[sijia],n,m;
int find(int x)
{
if(x==f[x])    return f[x];
int t=f[x];
f[x]=find(f[x]);
sijiaxiaozhu[x] += sijiaxiaozhu[t];
return f[x];
}
bool Union(int x,int y,int m)
{
int a=find(x),b=find(y);
if(a==b)
{
if(sijiaxiaozhu[x]+m!=sijiaxiaozhu[y])
return 0;
return 1;
}
f[b]=a;
sijiaxiaozhu[b]=sijiaxiaozhu[x]+m-sijiaxiaozhu[y];
return 1;
}
int main()
{
int a,b,x;
while(~scanf("%d %d",&n,&m))
{
for(int i=0;i<=n;i++)
f[i]=i,sijiaxiaozhu[i]=0;
int cnt=0;
for(int i=0;i<m;i++)
{
scanf("%d %d %d",&a,&b,&x);
if(!Union(a,b,x))   cnt++;
}
printf("%d\n",cnt);
}
}


### How Many Tables

Description:
Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Ignatius wants to know how many tables he needs at least. You have to notice that not all the friends know each other, and all the friends do not want to stay with strangers.

One important rule for this problem is that if I tell you A knows B, and B knows C, that means A, B, C know each other, so they can stay in one table.

For example: If I tell you A knows B, B knows C, and D knows E, so A, B, C can stay in one table, and D, E have to stay in the other one. So Ignatius needs 2 tables at least.
Input

The input starts with an integer T(1<=T<=25) which indicate the number of test cases. Then T test cases follow. Each test case starts with two integers N and M(1<=N,M<=1000). N indicates the number of friends, the friends are marked from 1 to N. Then M lines follow. Each line consists of two integers A and B(A!=B), that means friend A and friend B know each other. There will be a blank line between two cases.

Output

For each test case, just output how many tables Ignatius needs at least. Do NOT print any blanks.

Sample Input

2
5 3
1 2
2 3
4 5
5 1
2 5

Sample Output

2
4

Problem solving:

Code:

#include<bits/stdc++.h>
using namespace std;
const int sijiaxiaozhu=1e4;
int p[sijiaxiaozhu];
int find(int x)
{
return p[x]!=x?p[x]=find(p[x]):x;
}
void join(int x,int y)
{
x=find(x),y=find(y);
if(x!=y) p[x]=y;
}
int main()
{
int t,n,m,a,b;
scanf("%d",&t);
while(t--)
{
int ans=0;
scanf("%d %d",&n,&m);
for(int i=1;i<=n;i++)   p[i]=i;
for(int i=0;i<m;i++)
{
scanf("%d %d",&a,&b);
join(a,b);
}
for(int i=1;i<=n;i++)
{
if(p[i]==i) ans++;
}
printf("%d\n",ans);
}
}