A graph G = (V, E) is a data structure where V is a finite set of vertices and E is a binary relation on V represented by a set of edges. Fig. 1 illustrates an example of a graph (or graphs).
A free tree is a connnected, acyclic, undirected graph. A rooted tree is a free tree in which one of the vertices is distinguished from the others. A vertex of a rooted tree is called "node."
Your task is to write a program which reports the following information for each node u of a given rooted tree T:
node ID of u
parent of u
depth of u
node type (root, internal node or leaf)
a list of chidlren of u
If the last edge on the path from the root r of a tree T to a node x is (p, x), then p is the parent of x, and x is a child of p. The root is the only node in T with no parent.
A node with no children is an external node or leaf. A nonleaf node is an internal node
The number of children of a node x in a rooted tree T is called the degree of x.
The length of the path from the root r to a node x is the depth of x in T.
Here, the given tree consists of n nodes and evey node has a unique ID from 0 to n-1.
Fig. 2 shows an example of rooted trees where ID of each node is indicated by a number in a circle (node). The example corresponds to the first sample
input.
Note
You can use a left-child, right-sibling representation to implement a tree which has the following data:
the parent of u
the leftmost child of u
the immediate right sibling of u
Constraints
1 ≤ n ≤ 100000
Input
The first line of the input includes an integer n, the number of nodes of the tree.
In the next n lines, the information of each node u is given in the following format:
id k c1 c2 ... ck
where id is the node ID of u, k is the degree of u, c1 ... ck are node IDs of 1st, ... kth child of u. If the node does not have a child, the k is 0.
Output
Print the information of each node in the following format ordered by IDs:
node id: parent = p, depth = d, type, [c1...ck]
p is ID of its parent. If the node does not have a parent, print -1.
d is depth of the node.
type is a type of nodes represented by a string (root, internal node or leaf). If the root can be considered as a leaf or an internal node, print root.
c1...ck is the list of children as a ordered tree.
Please follow the format presented in a sample output below.
We need to implement a program to read a rooted tree, compute parent, depth, type, and children list for each node, and output in specified format. The nodes are given with ID and children list. Since nodes can be given in any order, we need to reconstruct the tree. Constraints up to 100k nodes, so we need efficient data structures.
The input format: first line n. Then n lines each: id k c1 ... ck. It's typical of a rooted tree representation where the root is not explicitly identified but we can find it by the node that never appears as a child (or whose parent is -1). So we need to record parent for each node.
We can create arrays of size n: parent (initialize to -1), degree (number of children), children list (maybe store as vector of ints). We read each line, for id, we record its children list. For each child c, we set parent[c] = id (provided no conflicting assignments; but there should be only one parent). We also store children list for each id. After reading all lines, we find root as node with parent == -1.
Then we compute depth for each node via DFS or BFS from root, using the children adjacency list. Depth can be computed recursively: depth[root] = 0; for each child, depth[child] = depth[parent] + 1. We'll use iterative stack/queue to avoid recursion depth issues (n up to 100k, recursion might be okay but we'll use BFS to be safe).
Then we determine type: root if parent == -1; leaf if children list empty (and not root? but root could also have no children, in which case they say: "If the root can be considered as a leaf or an internal node, print root." So if node is root, type = "root" regardless of children count. Else if children list empty: "leaf", else "internal node".
Finally output node id from 0 to n-1: "node id: parent = p, depth = d, type, [c1...ck]". Note: children list should be printed as a comma-separated list in the order given in input? It says: "a list of children of u" and "c1...ck is the list of children as a ordered tree." The sample output shows children list exactly as input order. So we should preserve order. We stored children list as given.
Edge cases: nodes may not be contiguous? The problem says "the given tree consists of n nodes and every node has a unique ID from 0 to n-1." So IDs are 0..n-1 inclusive. Input may not be sorted by id? But they give lines each with id; could be in any order? The example is in order but not necessarily; but we should handle arbitrary order. We'll store arrays indexed by id.
For i in 0..n-1: read id, k; if k>0, read children and push into children[id]; for each child c: set parent[c] = id (if already set, it should be -1 initially). Since it's a tree, each node has at most one parent.
After reading, find root: For i from 0 to n-1 if parent[i] == -1: root = i; (there should be exactly one).
Compute depth: BFS/DFS from root. Use stack/queue. For root, depth=0. Then for each child, depth = depth[parent] + 1.
For each node id from 0 to n-1:
p = parent[id] (if -1)
d = depth[id]
type: if parent == -1: "root"; else if children[id].empty(): "leaf"; else "internal node".
children list: print as e.g., [1, 2, 3] etc.
Format: "node {id}: parent = {p}, depth = {d}, {type}, [{children comma-separated}]". Note: for children list, if empty, just [].
Use iostream with sync or printf? We can use printf for formatted but children list as vector; easiest: use printf for static parts and loops. Or use cout; but we need to output many lines, performance okay.
Potential pitfalls: Input size large; use fast IO (ios::sync_with_stdio(false); cin.tie(0)). Use vector to store children; total children sum = n-1 (since tree with n nodes has n-1 edges). So memory O(n).
Compute depth using BFS queue:
queue q; q.push(root); depth[root]=0;
while(!q.empty()) {
int u = q.front(); q.pop();
for(int v : children[u]) {
depth[v] = depth[u] + 1;
q.push(v);
}
}
That's fine.
We also need to handle root detection: There might be case where parent array remains -1 for multiple nodes if tree not properly rooted? But problem says it's a rooted tree so exactly one root.
Now test with sample.
Implementation details:
Use vector parent(n, -1);
For each id line, we read children and assign parent for each child. However, note that a child might appear before its own line as parent? That's okay; we set parent now. But careful: For root, we might set parent for root if it appears as a child? But root shouldn't be child of any node. But input might list a node as child and later as its own line; it's fine. The assignment for parent should only happen once; if there's a duplicate, it would be an error, but we can assert if parent[child] != -1 and parent[child] != id? But tree ensures unique parent. We can ignore.
We need to read lines: the number of lines is n; each line begins with id and k. But ids might not be in order; we can read all lines and store children accordingly. However, we need to process all. We'll just do:
for(int i=0;i<n;i++){
int id, k; cin>>id>>k;
children[id].reserve(k);
for(int j=0;j<k;j++) {
int c; cin>>c;
children[id].push_back(c);
parent[c] = id;
}
}
That's fine.
Now, root might be determined as node with parent == -1 after processing all. Since we set parent for each child, only root remains -1.
Potential issue: If a node has no children and never appears as child (so parent stays -1), that node would be root. But if multiple nodes have parent -1, then it's not a tree? However, the problem guarantees rooted tree, so exactly one.
Now compute depth and output.
Time complexity: O(n). Memory: O(n).
Now we need to ensure correct output formatting. Children list printed as comma separated without trailing comma. Use:
Now, note: In sample output, there is a space after colon? "node 0: parent = -1, ..." So colon then space. Our code: "node " << id << ": parent = ..." yields "node 0: parent = ..." That is fine.
One more nuance: The problem statement says: "node id: parent = p, depth = d, type, [c1...ck]" No comma after type? Actually sample: "node 0: parent = -1, depth = 0, root, [1, 4, 10]" So after type there is a comma before the list. So we output: ", " before the list? We have: type << ", ["; So yes.
Now, confirm that type string: "root", "internal node", "leaf". We need to ensure correct spelling: internal node (two words) not "internal_node". In sample they have "internal node". So we will use exactly: if root -> "root"; else if children empty -> "leaf"; else -> "internal node".
Now, verify with input sample 2:
4
1 3 3 2 0
0 0
3 0
2 0
Let's simulate: Process line1: id=1, k=3, children:3,2,0. So children[1] = [3,2,0]. Set parent[3]=1, parent[2]=1, parent[0]=1.
Line2: id=0, k=0 -> children[0] empty, no parent changes.
Line3: id=3, k=0 -> children[3] empty.
Line4: id=2, k=0 -> children[2] empty.
Now parent array: index0: parent[0]=1, index1: parent[1] not set (still -1), index2: parent[2]=1, index3: parent[3]=1. So root is 1. Depth: depth[1]=0; children:3,2,0 -> depth=1 for each. Types: node0: child list empty, parent=1 => leaf; node1: root; nodes2,3: leaf. Output order by id: node0: parent=1, depth=1, leaf, []; node1: parent=-1, depth=0, root, [3,2,0]; node2: parent=1, depth=1, leaf, []; node3: parent=1, depth=1, leaf, []. That matches sample output, except the children list for node1 is [3,2,0] as input order? sample output: node 1: ... [3, 2, 0] yes.
Thus correct.
Now implement.
Potential pitfalls: Input may be large, so use fast I/O. Use vector<vector> children(n); but n up to 100k, each child list variable. That's fine.
Memory: vector of vectors overhead; each inner vector has small overhead. But total edges n-1, so total memory O(n). Acceptable.
Now we just implement main() accordingly.
But there is a note in problem: "You can use a left-child, right-sibling representation to implement a tree which has the following data: the parent of u, the leftmost child of u, the immediate right sibling of u." That's just an alternative suggestion; we are not required to use that. Our approach is simpler.
One more nuance: The problem might expect we also use left-child right-sibling representation because of memory constraints? But n=100k, our adjacency list is fine. Typically such problems in AOJ (maybe this is AOJ ALDS1_7_A) allow adjacency list.
But there is a nuance: The input lines may not be sorted by ID, but we are storing children in the order given, which is correct. However, we must be careful: The order of nodes in output should be by ID (0..n-1). But the children list for each node should be as given in input order. Since we read lines in some order, but each line gives children list for that node. So we store exactly as read. That's fine.
Now code.
Edge Cases:
n=1: root only. Input: 0 0. Then parent[0] remains -1, root=0, depth=0, type root, children empty. Output: node 0: parent = -1, depth = 0, root, [].
Node with many children: works.
Now ensure BFS/DFS recursion depth not too high; BFS using queue is fine.
But one subtle: The tree is rooted, and we have adjacency list for children; it's a directed tree from root downwards. BFS will traverse without cycles.
Now we should also compute depth via recursion but BFS fine.
Let's write code.
Pseudo:
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<vector> children(n);
vector parent(n, -1);
for (int i = 0; i < n; ++i) {
int id, k;
cin >> id >> k;
children[id].reserve(k);
for (int j = 0; j < k; ++j) {
int c;
cin >> c;
children[id].push_back(c);
parent[c] = id;
}
}
int root = -1;
for (int i = 0; i < n; ++i) {
if (parent[i] == -1) {
root = i;
break;
}
}
vector depth(n, 0);
// BFS from root
queue q;
depth[root] = 0;
q.push(root);
while (!q.empty()) {
int u = q.front(); q.pop();
for (int v : children[u]) {
depth[v] = depth[u] + 1;
q.push(v);
}
}
for (int i = 0; i < n; ++i) {
cout << "node " << i << ": parent = " << parent[i] << ", depth = " << depth[i] << ", ";
if (parent[i] == -1) {
cout << "root";
} else if (children[i].empty()) {
cout << "leaf";
} else {
cout << "internal node";
}
cout << ", [";
for (size_t j = 0; j < children[i].size(); ++j) {
if (j > 0) cout << ", ";
cout << children[i][j];
}
cout << "]\n";
}
return 0;
}
Now this solves the problem.
But careful: In the input, the lines might not be given in order of ID, but we are using children[id] assuming id is within range; but we also are iterating i from 0 to n-1 in the input loop, reading each line and using id from that line to index into children. That's fine; however, we need to ensure that we correctly read all n lines. Since we are not sorting the input, the for loop with i from 0 to n-1 just reads n lines, but id could be any order. But after reading all, children arrays are filled correctly.
Potential issue: When reading lines, if there are duplicate IDs? Should not happen. But if we read line for id=5 and later again line for id=5? No, each node appears exactly once.
Thus okay.
But note: In the input reading loop, we are using children[id].reserve(k) and then pushing children. But what if k=0? Then we do nothing. That's fine.
Now we have to consider that parent array is initially -1, and we set parent[c]=id each time we see a child. However, if a node appears as a child multiple times? That would violate tree, but not our concern. But if it appears multiple times as child, we would set parent multiple times, which could overwrite. That's not supposed to happen.
Now test with sample 1: Works.
Now about performance: For n=100000, total edges = 99999, BFS O(n). Output lines also O(n). Fine.
Now ensure that the format exactly matches sample: There is a space after colon? In our output: "node 0: parent = -1..." yes. The sample shows "node 0: parent = -1, ..." So we have colon then space. Good.
Now possible pitfalls: Some judges require using endl or newline. Using "\n" is fine.
Now we should check whether depth of root is 0. Yes.
Now note: In the problem description they talk about left-child, right-sibling representation but we don't need.
Now final code: We'll produce a full solution.
But I want to think about alternative constraints: In the constraint they mention n up to 100000, but input size moderate. Our solution is linear.
Now final answer: provide the code.
But since the user asked for "Your task is to write a program which reports..." we just output the code.
We'll write in C++. Provide final answer.
Potential improvements: We can compute depth using recursion, but BFS is safe.
Edge Cases: If root is not 0? we find correctly.
Now let's confirm with second sample:
Input:
4
1 3 3 2 0
0 0
3 0
2 0
Our algorithm:
Loop i=0: id=1, k=3, children:3,2,0 => children[1]=[3,2,0]; parent[3]=1, parent[2]=1, parent[0]=1.
i=1: id=0, k=0 => children[0]=[]; parent unchanged.
i=2: id=3, k=0 => children[3]=[].
i=3: id=2, k=0 => children[2]=[].
Parent: 0->1,1->-1,2->1,3->1. Root=1.
BFS: q start 1, depth[1]=0; process children: 3 depth=1, 2 depth=1, 0 depth=1; order of push? children[1] is [3,2,0] so push 3,2,0; but BFS order doesn't matter because depth is set correctly regardless. All children will be set to depth=1. Then for each child, they have no children. BFS done.
Output:
node0: parent=1, depth=1, leaf, [].
node1: parent=-1, depth=0, root, [3,2,0] -> but note: children[1] = [3,2,0] so output as "3, 2, 0"? Our loop prints comma separated: first prints "3", then ", 2", then ", 0". So output: [3, 2, 0] with spaces after commas? In sample: [3, 2, 0] (with spaces). Our code prints: "[" then if j>0 print ", " then value. So for j=0: prints "3"; j=1: prints ", 3"? Actually we need to ensure no space before comma? We do: if (j>0) cout << ", "; then cout << children[i][j]; So for j=1, we output ", " then "2" -> that yields ", 2" which has a space after comma, exactly like sample. So output is [3, 2, 0]. Good.
node2: parent=1, depth=1, leaf, [].
node3: same.
Matches sample.
Now everything fine.
Now we should consider if any node's parent is not set because it appears as root and also appears as child? No.
Now also need to consider that some nodes may have no children and parent set: leaf.
Now implement final code.
But there is a subtle nuance: In the input, the number of lines is n, but the order of lines is arbitrary. In our reading loop, we are using i from 0 to n-1, which expects exactly n lines and we read them sequentially. That's fine; we don't need to map lines to IDs; we simply read each line and use the id from it. So we can read any order.
However, one potential problem: We used children[id] before ensuring it's allocated? children is vector of vectors of size n; it's fine.
Now we also need to ensure we handle reading of ids that are out of range? No.
Now final code.
I'll include necessary headers: iostream, vector, queue.
Now answer. ```cpp
#include
#include
#include
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
code
int n;
cin >> n;
vector<vector<int>> children(n);
vector<int> parent(n, -1);
// read all nodes
for (int i = 0; i < n; ++i) {
int id, k;
cin >> id >> k;
children[id].reserve(k);
for (int j = 0; j < k; ++j) {
int c;
cin >> c;
children[id].push_back(c);
parent[c] = id;
}
}
// find root (the only node with parent = -1)
int root = -1;
for (int i = 0; i < n; ++i) {
if (parent[i] == -1) {
root = i;
break;
}
}
// compute depth by BFS
vector<int> depth(n, 0);
queue<int> q;
q.push(root);
depth[root] = 0;
while (!q.empty()) {
int u = q.front();
q.pop();
for (int v : children[u]) {
depth[v] = depth[u] + 1;
q.push(v);
}
}
// output
for (int i = 0; i < n; ++i) {
cout << "node " << i << ": parent = " << parent[i]
<< ", depth = " << depth[i] << ", ";
if (parent[i] == -1) {
cout << "root";
} else if (children[i].empty()) {
cout << "leaf";
} else {
cout << "internal node";
}
cout << ", [";
for (size_t j = 0; j < children[i].size(); ++j) {
if (j > 0) cout << ", ";
cout << children[i][j];
}
cout << "]\n";
}
return 0;