Description
When ALPC42 got to a panzer brigade, He was asked to build software to help them regroup the battalions or companies.
As the tradition of army, soldiers are rated according his or her abilities, taking the rate as an integer. The fighting capacity of a company is determined by the soldier in this company whose rate is lowest. Now the recruits those rated are coming and join to their companies according to the order form HQ.
With the coming of new recruits, a big regroup action reached, asking to merge some companies into one. The designation of a company, however, will not be canceled, but remain for memorialize what the company is done, means the designation of the company is still exist, but the company is gone, so it is unable to ask recruits to join this company, or merge the company into others.
A strange thing is, the orders sometimes get wrong, send newbie to a company which is already merged into another, or mentioned some only-designation-existed companies. Such order could be rejected.
The brigadier wants to know every change of each order, so the program should able to report the status of every order, telling whether it is accept, and can query the fighting capacity of specified company. (To simplify, companies are numbered from 0 to n-1
Input
There may be several test cases.
For each case, the integers in first line, n, k, m, telling that there are n companies, k soldiers already, and m orders needs be executed. (1<=n ,k ,m<=100000).
Then k lines with two integers R and C for each, telling a soldier with rate R is now in company C
Then m lines followed, containing 3 kinds of orders, in upper case:
AP x y
A recruit with ability rate x were asked to join company y. (0<=x<2^31, 0<=y
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
#define inf 0x3f3f3f3f
int n,k,m;
struct node
{
int cnt,low,pre;
} com[100009];
int fin(int i)
{
return i==com[i].pre?i:com[i].pre=fin(com[i].pre);
}
int main()
{
while(scanf("%d%d%d",&n,&k,&m)!=EOF)
{
for(int i=0; i<n; i++)
com[i].pre=i,com[i].cnt=0,com[i].low=inf;//初始化!!!!!!!!!
for(int i=0; i<k; i++)
{
int a,b;
scanf("%d%d",&a,&b);
com[b].cnt++;
com[b].low=min(com[b].low,a);
}
while(m--)
{
char s[10];
scanf("%s",s);
int x,y;
if(!strcmp(s,"GT"))
{
scanf("%d",&x);
if(com[x].pre!=x)
printf("Company %d is a part of company %d.\n",x,fin(x));
else if(com[x].cnt==0)
printf("Company %d is empty.\n",x);
else
printf("Lowest rate: %d.\n",com[x].low);
}
else if(!strcmp(s,"AP"))
{
scanf("%d%d",&x,&y);
if(com[y].pre!=y)
printf("Reject\n");
else
{
printf("Accept\n");
com[y].cnt++;
com[y].low=min(com[y].low,x);
}
}
else
{
scanf("%d%d",&x,&y);
if(com[x].pre!=x||com[y].pre!=y||x==y)//当合并的是同一个公司!!!
printf("Reject\n");
else
{
printf("Accept\n");
com[y].pre=x;
com[x].cnt+=com[y].cnt;
com[x].low=min(com[y].low,com[x].low);
}
}
}
printf("\n");
}
return 0;
}
1,并查集合并的是根!
2,初始化!