We build a social network from people numbered . Some pairs of people in the network will be friends. If person becomes a friend of person , then person also becomes a friend of person .
The people are added to the network in stages, which are also numbered from to . Person is added in stage . In stage 0, person 0 is added as the only person of the network. In each of the next stages, a person is added to the network by a host, who may be any person already in the network. At stage (), the host for that stage can add the incoming person into the network by one of the following three protocols:
IAmYourFriendmakes person a friend of the host only.
MyFriendsAreYourFriendsmakes person a friend of each person, who is a friend of the host at this moment. Note that this protocol does not make person a friend of the host.
WeAreYourFriendsmakes person a friend of the host, and also a friend of each person, who is a friend of the host at this moment.
After we build the network we would like to pick a sample for a survey, that is, choose a group of people from the network. Since friends usually have similar interests, the sample should not include any pair of people who are friends with each other. Each person has a survey confidence, expressed as a positive integer, and we would like to find a sample with the maximum total confidence.
|stage||host||protocol||friend relations added|
|3||1||(3, 1), (3, 0), (3, 2)|
|4||2||(4, 1), (4, 3)|
Initially the network contains only person 0. The host of stage 1 (person 0) invites the new person 1 through the
IAmYourFriend protocol, hence they become friends. The host of stage 2 (person 0 again) invites person 2 by
MyFriendsAreYourFriends, which makes person 1 (the only friend of the host) the only friend of person 2. The host of stage 3 (person 1) adds person 3 through
WeAreYourFriends, which makes person 3 a friend of person 1 (the host) and people 0 and 2 (the friends of the host). Stages 4 and 5 are also shown in the table above. The final network is shown in the following figure, in which the numbers inside the circles show the labels of people, and the numbers next to the circles show the survey confidence. The sample consisting of people 3 and 5 has total survey confidence equal to , which is the maximum possible total confidence.
Given the description of each stage and the confidence value of each person, find a sample with the maximum total confidence. You only need to implement the function
findSample(n, confidence, host, protocol)
n: the number of people.
confidence: array of length ;
confidence[i]gives the confidence value of person .
host: array of length ;
host[i]gives the host of stage .
protocol: array of length ;
protocol[i]gives the protocol code used in stage ):
- Since there is no host in stage 0,
protocolare undefined and should not be accessed by your program.
- The function should return the maximum possible total confidence of a sample.
|1||11||All three protocols|
|5||23||All confidence values are 1||Both |
|6||31||All three protocols|
Your submission should implement the subprogram described above, using the following signatures.
int findSample(int n, int confidence, int host, int protocol);
function findSample(n: longint, confidence: array of longint, host: array of longint; protocol: array of longint): longint;