PAT A1094 The Largest Generation (25point(s))

Scroll Down

A family hierarchy is usually presented by a pedigree tree where all the nodes on the same level belong to the same generation. Your task is to find the generation with the largest population.

Input Specification:

Each input file contains one test case. Each case starts with two positive integers N (<100) which is the total number of family members in the tree (and hence assume that all the members are numbered from 01 to N), and M (<N) which is the number of family members who have children. Then M lines follow, each contains the information of a family member in the following format:

ID K ID[1] ID[2] ... ID[K]
where ID is a two-digit number representing a family member, K (>0) is the number of his/her children, followed by a sequence of two-digit ID's of his/her children. For the sake of simplicity, let us fix the root ID to be 01. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print in one line the largest population number and the level of the corresponding generation. It is assumed that such a generation is unique, and the root level is defined to be 1.

Sample Input:

23 13
21 1 23
01 4 03 02 04 05
03 3 06 07 08
06 2 12 13
13 1 21
08 2 15 16
02 2 09 10
11 2 19 20
17 1 22
05 1 11
07 1 14
09 1 17
10 1 18

Sample Output:

9 4

解答

#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<map>

using namespace std;

struct record { string id; int time, flag; };

int main(){
	int n, k, h, m, s, maxTime = 0;
	string flagstr;
	scanf("%d %d\n", &n, &k);
	vector<record> records(n), validRecords;
	for (int i = 0; i < n; i++) {
		cin >> records[i].id;
		scanf("%d:%d:%d", &h, &m, &s);
		cin >> flagstr;
		records[i].time = 3600 * h + 60 * m + s;
		records[i].flag = flagstr == "in" ? 1 : -1;
	}
	sort(records.begin(), records.end(), [](record a, record b) {
		return a.id == b.id ? a.time < b.time : a.id < b.id; });
	map<string, int> totalTime;
	for (int i = 0; i < n - 1; i++) {
		if (records[i].id == records[i + 1].id && records[i].flag == 1 && records[i + 1].flag == -1) {
			validRecords.push_back(records[i]);
			validRecords.push_back(records[i + 1]);
			totalTime[records[i].id] += records[i + 1].time - records[i].time;
			if (totalTime[records[i].id] > maxTime) maxTime = totalTime[records[i].id];
		}
	}
	sort(validRecords.begin(), validRecords.end(), [](record a, record b) {
		return a.time < b.time; });
	vector<int> cnt(n);
	for (int i = 0; i < validRecords.size(); i++) {
		if (i == 0) cnt[i] += validRecords[i].flag;
		else cnt[i] = cnt[i - 1] + validRecords[i].flag;
	}
	int tempIndex = 0, j;
	for (int i = 0; i < k; i++) {
		int h, m, s, t;
		scanf("%d:%d:%d", &h, &m, &s);
		t = 3600 * h + 60 * m + s;
		for (j = tempIndex; j < validRecords.size(); j++) {
			if (validRecords[j].time > t) {
				printf("%d\n", j > 0 ? cnt[j - 1] : 0);
				break;
			}
			else if (j == validRecords.size() - 1) printf("%d\n", cnt[j]);
		}
		tempIndex = j;
	}
	for (auto it = totalTime.begin(); it != totalTime.end(); it++) {
		if (it->second == maxTime) printf("%s ", it->first.c_str());
	}
	printf("%02d:%02d:%02d", maxTime / 3600, maxTime % 3600 / 60, maxTime % 60);
	
	return 0;
}