One way that the police finds the head of a gang is to check people's phone calls. If there is a phone call between A and B, we say that A and B is related. The weight of a relation is defined to be the total time length of all the phone calls made between the two persons. A "Gang" is a cluster of more than 2 persons who are related to each other with total relation weight being greater than a given threthold K. In each gang, the one with maximum total weight is the head. Now given a list of phone calls, you are supposed to find the gangs and the heads.
Input:
For each case, the first line contains two positive Numbers N and K (both less than or equal to 1000), the number of phone calls and the weight threthold, respectively, Then N lines follow, each in the following format:
Name1 Name2 Time
Where Name1 and Name2 are the names of people at the two ends of the call, and Time is the length of the call. A name is A string of three capital letters feature from A to Z, A Time length is A positive integer which is no more than 1000 minutes.
Output:
For each test case, first print in a line the total number of gangs. Then For each gang, print a line in the name of the head and the total number of the members. It is guaranteed that the head is unique For each gang. The output must be sorted "according to the alphabetical order of the names of the heads.
The sample input:
8 59
AAA and BBB 10
BBB AAA 20
AAA CCC 40
DDD EEE 5
EEE DDD 70
FFF GGG 30
GGG HHH 20
HHH FFF 10
8 70
AAA and BBB 10
BBB AAA 20
AAA CCC 40
DDD EEE 5
EEE DDD 70
FFF GGG 30
GGG HHH 20
HHH FFF 10
The sample output:
2
AAA 3
GGG 3
0
On the PAT test for "segmentation fault"
I put the recursion into general circulation, or tip segment error?
Pray god to help see where possible period of mistake?
this is PAT test shots, has been showing on the nine degrees is "Wrong Answer"
#include
#include
#include
#include
#include
#include
using namespace std;
# define MaxSize 1010
Typedef struct Node {
Char name [4].
int time;
Int the parent;
} the Node;
Typedef struct Head {
Char name [4].
Int numb;
} the Head;
Int compare1.c (const void * a, const void * b)
{
Node * p=(*) a;
The Node b * q=(*);
//if the point to the same root node, it is sorted by size of duration
If (p - & gt; The parent==q - & gt; The parent) return (p - & gt; Time & lt; Q - & gt; Time)? 1:1;
//do not point to the same root node, according to the root node order
Return (p - & gt; The parent & gt; Q - & gt; The parent)? 1:1;
}
Int compare2 (const void * a, const void * b)
{
The Head * p=(Head *) a;
The Head * q=b (Head *);
Return STRCMP (p - & gt; The name of the q - & gt; Name);
}
Int the Find (int loc);//look for the root, using a recursive ultimately all point to the root node
Void the Union (int first, int second);//merge collection
//global variable
The Node call [MaxSize];//save phone bills
The Head gang [MaxSize];//save leader
Int main ()
{
Int count, kval;
Name2 char name1 [4], [4].
int time;//each person's individual duration
While (the scanf (" % d % d ", & amp; The count, & amp; Kval)!=(EOF)
{
//initialization, to become a collection, each node individually single duration initialized to 0
For (int index=0; The index & lt; The count. + + index) call [index]. Parent=index;
//input data, each data, first check to see if in the collection, if in the update time information, if not, then added to the collection
Int numscan=0, loca=0, pre=0;//the number of elements in the instructions have been added, the location of the input array, the first name corresponding to the node number of
While (numscan & lt; The count)
{
The scanf (" % d % s % s ", name1, name2, & amp; Time);
+ + numscan;
int j;//traverse indicator
//processing first name
For (j=0; J & lt; Loca. + + j)
If (STRCMP (call [j]. Name, name1)==0)//if already in the collection
{
Call [j]. Journal of time +=time;
The pre=j;//save a number of the current node
break;
}
If (j==loca)//the current name does not exist in the collection of
{
Strcpy (call [loca]. Name, name1);
The call (loca). Time=the time;
The pre=loca; No.//recording the current
+ + loca;//update to the next position
}
//handle the second name
For (j=0; J & lt; Loca. + + j)
If (STRCMP (call [j]. Name, name2)==0)//if already in the collection
{
Call [j]. Journal of time +=time;
break;
}
If (j==loca)//the current name does not exist in the collection of
{
Strcpy (call [loca]. Name, name2);
The call (loca). Time=the time;
+ + loca;//update to the next position
}
The Union (pre, j);//merge two sets, the pre indicating the position of the first name, j indicating the position of the second name
}
The count=loca;//record the current call the number of elements in the array
//path compression, and according to the root node number order (point to the same root node according to the duration of the sorting, from big to small)
For (int index=0; The index & lt; The count. + + index), Find (index);//a roots function called for each element, make its all point to the same root
Tree (call, count, sizeof (Node), compare1.c);//root node size sorting, from small to large
//to find the number of leaders and organizations
Int root, j=0, I=0;//store the root node number, gang array item number
Int sumtime, numb;//gangs call total duration
Loca=0;
While (loca & lt; The count)
{
Sumtime=0;
Numb=0;
Root=call (loca). The parent;
For (; i{
+ + numb;//to the gangs to count
Sumtime +=call [I] time;
}
Sumtime=sumtime/2;//because each element again, this time sumtime added twice, so half
If (numb & gt; nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull