#include #include using namespace std; const int MAXSIZE = 1000; struct treenode { string name; int nchild; treenode *firstc, *nexts; bool root; int ans; } nodes[MAXSIZE]; int nNodes, sort[MAXSIZE]; treenode *findNode(string name) { int i; for(i=0; i> name; treenode *node = findNode(name); cin >> node->nchild; cin >> name; node->firstc = findNode(name); node->firstc->root = false; treenode *temp = node->firstc; for(int j=1; jnchild; j++) { cin >> name; temp->nexts = findNode(name); temp->nexts->root = false; temp = temp->nexts; } } } int *processTree(treenode *node, int d) { //cout << "start node " << node->name << endl; int i; int *desc = new int[d], *temp; for(i=0; ians = 0; treenode *c = node->firstc; for(i=0; inchild; i++) { temp = processTree(c, d); //cout << " node " << node->name << ", return from child " << c->name; //for(int ii=0; iians += temp[d-1]; delete temp; c = c->nexts; } return desc; } treenode *findRoot() { for(int i=0; i nodes[j+1].name)) { treenode temp = nodes[j]; nodes[j] = nodes[j+1]; nodes[j+1] = temp; } } } int count = 1; for(i=0; i 3 && nodes[i].ans != nodes[i-1].ans)) break; cout << nodes[i].name << ' ' << nodes[i].ans << endl; count++; } } void main() { int ncases, n, d; cin >> ncases; for(int icase = 1; icase <= ncases; icase++) { cin >> n >> d; input(n); treenode *root = findRoot(); processTree(root, d); output(icase); if (icase < ncases) cout << endl; } }