Problem:
Rooted Trees
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).
Fig. 1
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.
Fig. 2
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
c
1
c
2
...
c
k
where
id
is the node ID of
u
,
k
is the degree of
u
,
c
1
...
c
k
are node IDs of 1st, ...
k
th 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
, [
c
1
...
c
k
]
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
.
c
1
...
c
k
is the list of children as a ordered tree.
Please follow the format presented in a sample output below.
Constraints
1 ≤
n
≤ 100000
Sample Input 1
13
0 3 1 4 10
1 2 2 3
2 0
3 0
4 3 5 6 7
5 0
6 0
7 2 8 9
8 0
9 0
10 2 11 12
11 0
12 0
Sample Output 1
node 0: parent = -1, depth = 0, root, [1, 4, 10]
node 1: parent = 0, depth = 1, internal node, [2, 3]
node 2: parent = 1, depth = 2, leaf, []
node 3: parent = 1, depth = 2, leaf, []
node 4: parent = 0, depth = 1, internal node, [5, 6, 7]
node 5: parent = 4, depth = 2, leaf, []
node 6: parent = 4, depth = 2, leaf, []
node 7: parent = 4, depth = 2, internal node, [8, 9]
node 8: parent = 7, depth = 3, leaf, []
node 9: parent = 7, depth = 3, leaf, []
node 10: parent = 0, depth = 1, internal node, [11, 12]
node 11: parent = 10, depth = 2, leaf, []
node 12: parent = 10, depth = 2, leaf, []
Sample Input 2
4
1 3 3 2 0
0 0
3 0
2 0
Sample Output 2
node 0: parent = 1, depth = 1, leaf, []
node 1: 
...(truncated)

Submitted solutions:

code_snippet: #include<iostream>
using namespace std;
static const int MAX = 100005;
static const int NIL = -1;

struct Node {int p, l, r;};

Node T[MAX];
int n, D[MAX];

void print(int u){
    int i,c;
    cout << "node " << u << ": ";
    cout << "parent = " << T[u].p << ", ";
    cout << "depth = " << D[u] << ", ";

    if(T[u].p == NIL) cout << "root, ";
    else if(T[u].l == NIL) cout << "leaf, ";
    else cout << "internal node, ";

    cout << "[";

    for(int i = 0, c = T[u].l; c!= NIL ;i++, c = T[c].r){
        if(i) cout << ", ";
        cout << c;
    }
    cout << "]" << endl;
}

void setDepth(int u, int p){
    D[u] = p;
    if(T[u].r != NIL) setDepth(T[u].r,p);
    if(T[u].l != NIL) setDepth(T[u].l, p + 1);
}

int main(){
    int i, j, d, v, c, l, r;
    cin >> n;
    for(int i = 0; i < n; i++){
        T[i].p = T[i].l = T[i].r = NIL;
    }

    for(int i = 0 ; i < n; i++){
        cin >> v >> d;
        for(j = 0; j < d; j++){
            cin >> c;
            if(j == 0) T[v].l = c;
            else T[l].r = c;
            l = c;
            T[c].p = v;
        }
    }

    for(i = 0;i < n; i++){
        if(T[i].p == NIL) r = i;
    }

    setDepth(r,0);

    for(i = 0; i < n; i++) print(i);
}
accuracy: 1
time_ms: 130
memory_kb: 8120
final_rank: 14

code_snippet: #include<iostream>
using namespace std;
static const int MAX = 100005;
static const int NIL = -1;

struct Node {int p, l, r;};

Node T[MAX];
int n, D[MAX];

void print(int u){
    int i,c;
    cout << "node " << u << ": ";
    cout << "parent = " << T[u].p << ", ";
    cout << "depth = " << D[u] << ", ";

    if(T[u].p == NIL) cout << "root, ";
    else if(T[u].l == NIL) cout << "leaf, ";
    else cout << "internal node, ";

    cout << "[";

    for(int i = 0, c = T[u].l; c!= NIL ;i++, c = T[c].r){
        if(i) cout << ", ";
        cout << c;
    }
    cout << "]" << endl;
}

void setDepth(int u, int p){
    D[u] = p;
    if(T[u].r != NIL) setDepth(T[u].r,p);
    if(T[u].l != NIL) setDepth(T[u].l, p + 1);
}

int main(){
    int i, j, d, v, c, l, r;
    cin >> n;
    for(int i = 0; i < n; i++){
        T[i].p = T[i].l = T[i].r = NIL;
    }

    for(int i = 0 ; i < n; i++){
        cin >> v >> d;
        for(j = 0; j < d; j++){
            cin >> c;
            if(j == 0) T[v].l = c;
            else T[l].r = c;
            l = c;
            T[c].p = v;
        }
    }

    for(i = 0;i < n; i++){
        if(T[i].p == NIL) r = i;
    }

    setDepth(r,0);

    for(i = 0; i < n; i++) print(i);
}
accuracy: 1
time_ms: 130
memory_kb: 7736
final_rank: 11

code_snippet: #include <bits/stdc++.h>
using namespace std;

struct node
{
    int id;
    int degree;
    node* parent;
    vector<node*> children;

    void adopt(int d, const vector<node*>& c)
    {
        degree += d;
        children = c;
    }
    node(int i)
    {
        id = i;
        parent = nullptr;
    }
    node()
    {

    }
};

struct rooted_tree
{
    vector<node> nodes;
    int node_count;

    void assign_node(int id, int d, const vector<int>& c_ids)
    {
        vector<node*> c_nodes;
        for(int c_id : c_ids)
            c_nodes.push_back(&nodes[c_id]);

        node& n = nodes[id];
        n.adopt(d, c_nodes);
        for(node* c_node : c_nodes)
            c_node->parent = &n;
    }

    int check_depth(int id, int count)
    {
        if(nodes[id].parent == nullptr)
            return count;
        else
            return check_depth(nodes[id].parent->id, count + 1);
    }

    void show_info()
    {
        for(node& n : nodes)
        {
            cout << "node " << n.id << ": ";

            cout << "parent = ";
            if(n.parent == nullptr)
            {
                cout << "-1";
            }
            else
            {
                cout << n.parent->id;
            }
            cout << ", ";

            cout << "depth = " << check_depth(n.id, 0) << ", ";

            if(n.parent == nullptr)
            {
                cout << "root";
            }
            else if(n.children.size() == 0)
            {
                cout << "leaf";
            }
            else
            {
                cout << "internal node";
            }
            cout << ", ";

            cout << "[";
            for(int i = 0; i < (int)n.children.size(); i++)
            {
                cout << n.children[i]->id;
                if(i != (int)n.children.size()-1)
                    cout << ", ";
            }
            cout << "]" << endl;
        }
    }
    rooted_tree(int n)
    {
        node_count = n;
        for(int i = 0; i < n; i++)
            nodes.push_back(node(i));
    }
};

int main()
{
    int n;
    cin >> n;

    rooted_tree t = rooted_tree(n);

    for(int node = 0; node < n; node++)
    {
        int id, d;
        cin >> id >> d;
        vector<int> c_id(d);
        for(int i = 0; i < d; i++)
            cin >> c_id[i];
        
        t.assign_node(id, d, c_id);
    }

    t.show_info();
}
accuracy: 1
time_ms: 130
memory_kb: 10028
final_rank: 16

code_snippet: #include <bits/stdc++.h>
// #include <boost/multiprecision/cpp_int.hpp>
using namespace std;
using ll = long long;
using ld = long double;
ll INF = 2e18;
#define PI 3.14159265358979323846264338327950l
// namespace multip = boost::multiprecision;
// using lll = multip::int128_t;
using P = pair<ll, ll>;
template<typename T> using vc = vector<T>;
template<typename T> using vv = vc<vc<T>>;
using vl = vc<ll>; using vvl = vv<ll>; using vvvl = vv<vl>; using vvvvl = vv<vvl>;
using vs = vc<string>; using vvs = vv<string>;
#ifndef ONLINE_JUDGE
#define _GLIBCXX_DEBUG
#endif
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define drep(i,n) for (ll i = (n)-1; i >= 0; i--)
#define nfor(i, s, n) for (ll i = (s); i < (n); i++)
#define dfor(i, s, n) for (ll i = (s)-1; i>=n; i--)
#define nall(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
#define eb emplace_back
#define em emplace
#define pob pop_back
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
#define YN { cout << "Yes" << endl; } else { cout << "No" << endl; }
#define dame cout << -1 << endl
#define minval(vec) *min_element(nall(vec))
#define maxval(vec) *max_element(nall(vec))
#define binaryS(vec, x) *lower_bound(nall(vec), x)
#define yu_qurid(x, y) (x) * (x) + (y) * (y)
#define mannhattan(x1, x2, y1, y2) abs(x1-x2)+abs(y1-y2)
#define vc_cout(v){ll n= size(v); rep(i,n)cout<<v[i]<<" ";cout << endl;}
#define vvc_cout(v){ll n= size(v);rep(i,n){rep(j,size(v[i])){cout<<v[i][j]<<" ";}cout<<endl;}}
// 少数の出力
void print(ld x) { printf("%.20Lf\n", x); }

// グリット用
vector<ll> dx = {1, 0, -1, 0};
vector<ll> dy = {0, 1, 0, -1};
bool in_grid(ll i, ll j, ll h, ll w) {
    return 0 <= i and i < h and 0 <= j and j < w;
}

// 木の構造
struct tree {
    // ここのorderとは次数
    ll id=-1, order=-1, parent=-1, depth=-1;
    vector<ll> children;
    string statu;
};

ll dep(vector<tree> &root_tree, ll &id, ll cnt){
    if (root_tree[id].parent == -1) return cnt;
    cnt++;
    return dep(root_tree, root_tree[id].parent, cnt);
}

// main
void solve() {
    ll n;
    cin >> n;
    vector<tree> root_tree(n);
    nfor(i,0,n) {
        ll id, k, c;
        cin >> id >> k;
        root_tree[id].id = id;
        root_tree[id].order = k;
        nfor(i,0,k) {
            cin >> c;
            root_tree[id].children.push_back(c);
            root_tree[c].parent = id;
        }
    }
    // depth、状態を計算する
    // rootはparentが-1のとき
    // internarl nodeはparentが-1でなく,childrenが0でないとき
    // leafはchildrenが0のとき
    // 再帰関数で実現する
    nfor(i,0,n) {
        ll cnt = 0;
        ll d = dep(root_tree, i, cnt);
        root_tree[i].depth = d;
    }
    nfor(i,0,n) {
        if (root_tree[i].parent == -1) {
            root_tree[i].statu = "root";
        }
        else if (root_tree[i].parent != -1 and root_tree[i].children.size() != 0) {
            root_tree[i].statu = "internal node";
        }
        else {
            root_tree[i].statu = "leaf";
        }
    }
    // 結果を出力する
    nfor(i, 0, n) {
        cout << "node " << root_tree[i].id;
        cout << ": parent = " << root_tree[i].parent;
        cout << ", depth = " << root_tree[i].depth;
        cout << ", " << root_tree[i].statu;
        cout << ", [";
        nfor(j,0,root_tree[i].order) {
            cout << root_tree[i].children[j];
            if (j + 1 != root_tree[i].order) {
                cout << ", ";
            }
        }
        cout << "]" << endl;
    }


    return;
}

int main() {
    solve();
    return 0;
}
accuracy: 1
time_ms: 90
memory_kb: 13408
final_rank: 20

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

class Node {
public:
	int value;
	Node* parent;
	vector<Node*> children;

	Node(int value) {
		this->value = value;
		this->parent = NULL;
	}
};

void computeDepth(Node* node, int depth, vector<int>& depths) {
	depths[node->value] = depth;
	for (Node* child : node->children) {
		computeDepth(child, depth + 1, depths);
	}
}

int main() {
	int n;
	cin >> n;
	vector<Node*> nodes(n); // 建立的节点需要被保存
	for (int i = 0; i < n; i++) {
		nodes[i] = new Node(i); // 先把数字变成待连接的节点
	}
	// 连接
	for (int i = 0; i < n; i++) {
		int v, k;
		cin >> v >> k;
		for (int j = 0; j < k; j++) {
			int c;
			cin >> c;
			nodes[v]->children.push_back(nodes[c]);
			nodes[c]->parent = nodes[v];
		}
	}
	// 找到根节点
	Node* root = NULL;
	for (int i = 0; i < n; i++) {
		if (nodes[i]->parent == NULL) {
			root = nodes[i];
			break;
		}
	}
	// 计算深度
	vector<int> depths(n);
	computeDepth(root, 0, depths);
	// 输出结果
	for (int i = 0; i < n; i++) {
		cout << "node " << i << ": ";

		cout << "parent = ";
		if (nodes[i]->parent) {
			cout << nodes[i]->parent->value << ", ";
		}
		else {
			cout << "-1, ";
		}

		cout << "depth = " << depths[i] << ", ";

		if (nodes[i]->value == root->value) {
			cout << "root, ";
		}
		else if (nodes[i]->children.empty()) {
			cout << "leaf, ";
		}
		else {
			cout << "internal node, ";
		}

		cout << "[";
		for (int j = 0; j < nodes[i]->children.size(); j++) {
			if (j)
				cout << ", ";
			cout << nodes[i]->children[j]->value;
		}
		cout << "]" << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 130
memory_kb: 10304
final_rank: 17

code_snippet: #include <iostream>
#include <vector>
#include <optional>
using namespace std;

struct Node {
    optional<int> parent;
    optional<int> left;   // first child
    optional<int> right;  // next sibling
};

struct Tree {
    vector<Node> nodes;
    vector<int> depth;

    explicit Tree(int n) : nodes(n), depth(n, 0) {}

    void add_children(int v, const vector<int>& children) {
        if (children.empty()) return;
        nodes[v].left = children.front();
        for (size_t i = 0; i < children.size(); ++i) {
            int c = children[i];
            nodes[c].parent = v;
            if (i + 1 < children.size()) nodes[c].right = children[i + 1];
        }
    }

    int find_root() const {
        for (size_t i = 0; i < nodes.size(); ++i)
            if (!nodes[i].parent) return static_cast<int>(i);
        return -1; // should not happen
    }

    void assign_depth(int u, int d) {
        depth[u] = d;
        if (nodes[u].right) assign_depth(*nodes[u].right, d);
        if (nodes[u].left) assign_depth(*nodes[u].left, d + 1);
    }

    void print(int u) const {
        cout << "node " << u << ": "
             << "parent = " << (nodes[u].parent ? to_string(*nodes[u].parent) : string("-1")) << ", "
             << "depth = " << depth[u] << ", ";

        if (!nodes[u].parent) cout << "root, ";
        else if (!nodes[u].left) cout << "leaf, ";
        else cout << "internal node, ";

        cout << "[";
        bool first = true;
        for (auto c = nodes[u].left; c; c = nodes[*c].right) {
            if (!first) cout << ", ";
            cout << *c;
            first = false;
        }
        cout << "]\n";
    }
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    if (!(cin >> n)) return 0;
    Tree tree(n);

    for (int i = 0; i < n; ++i) {
        int v, d;
        cin >> v >> d;
        vector<int> children(d);
        for (int j = 0; j < d; ++j) cin >> children[j];
        tree.add_children(v, children);
    }

    int root = tree.find_root();
    if (root == -1) return 1;

    tree.assign_depth(root, 0);

    for (int i = 0; i < n; ++i) tree.print(i);
}
accuracy: 1
time_ms: 40
memory_kb: 8864
final_rank: 7

code_snippet: #include <iostream>
#include <list>
using namespace std;
int main()
{
    int n;
    cin >> n;

    int K[100000];
    list<int> C[100000];
    int P[100000];

    for(int id=0; id<n; id++){
        P[id] = -1;
    }
    for(int i=0; i<n; i++){
        int id, k;
        cin >> id >> k;
        K[id] = k;

        for(int j=0; j<k; j++){
            int c;
            cin >> c;

            C[id].push_back(c);
            P[c] = id;
        }
    }

    int D[100000];

    for(int id=0; id<n; id++){
        if(P[id]==-1){
            D[id] = 0;
        }
        else{
            D[id] = -1;
        }
    }

    for(int d=0; d<20; d++){
        for(int id=0; id<n; id++){
            if(D[id]!=d){
                continue;
            }
            for(list<int>::iterator it=C[id].begin(); it!=C[id].end(); it++){
                D[*it] = d+1;
            }
        }
    }
    for(int id=0; id<n; id++){
        cout << "node " << id << ": ";
        cout << "parent = " << P[id] << ", ";
        cout << "depth = " << D[id] << ", ";

        if(P[id]==-1){
            cout << "root, ";
        }
        else if(K[id]>0){
            cout << "internal node, ";
        }
        else{
            cout << "leaf, ";
        }

        cout << "[";
        for(list<int>::iterator it=C[id].begin(); it!=C[id].end(); it++){
            if(it!=C[id].begin()){
                cout << ", ";
            }
            cout << *it;
        }
        cout << "]" << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 130
memory_kb: 9952
final_rank: 15

code_snippet: #include <iostream>
#include <algorithm>
using namespace std;
 
const int MAX = 100000;
int n, pa[MAX], ch[MAX], br[MAX];
 
void solve(){
  for(int i=0;i<n;i++){
    cout << "node " << i << ": parent = " << pa[i] << ", depth = " << flush;
 
    int cnt = 0;
    for(int j=i; pa[j]!=-1; j=pa[j]) cnt++;
    cout << cnt << ", " << flush;
 
    if(pa[i] == -1) cout << "root, " << flush;
    else if(ch[i] == -1) cout << "leaf, " << flush;
    else cout << "internal node, " << flush;
 
    cout << "[";
    for(int j=ch[i]; j!=-1; j=br[j]){
      if(j != ch[i]) cout << ", ";
      cout << j;
    }
    cout << "]" << endl;
  }
}
 
int main(){
  fill(pa,pa+MAX,-1);
  fill(ch,ch+MAX,-1);
  fill(br,br+MAX,-1);
  cin >> n;
  for(int i=0;i<n;i++){
    int x,prev,y,c;
    cin >> x >> c;
    for(int j=0;j<c;j++){
      cin >> y;
      pa[y] = x;
      if(j == 0) ch[x] = y;
      else br[prev] = y;
      prev = y;
    }
  }
  solve();
}
accuracy: 1
time_ms: 330
memory_kb: 4544
final_rank: 18

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < n; ++i)

struct Node {
    int parent;
    vector<int> children;
};

vector<Node> tree;
vector<int> depth;

void setDepth(int u, int d) {
    depth[u] = d;
    for(auto c : tree[u].children) setDepth(c, d+1);
}


int main() {
    int n;
    cin >> n;
    tree.resize(n);
    depth.resize(n);

    rep(i,n) tree[i].parent = -1;
    rep(i,n) {
        int id,k;
        cin >> id >> k;
        rep(j,k) {
            int c;
            cin >> c;
            tree[id].children.push_back(c);
            tree[c].parent = id;
        }
    }

    int root = -1;
    rep(i,n) if(tree[i].parent == -1) root = i;

    setDepth(root, 0);

    rep(i,n) {
        cout << "node " << i << ": ";
        cout << "parent = " << tree[i].parent << ", ";
        cout << "depth = " << depth[i] << ", ";

        if(tree[i].parent == -1) cout << "root, ";
        else if(tree[i].children.empty()) cout << "leaf, ";
        else cout << "internal node, ";

        cout << "[";
        rep(j,(int)tree[i].children.size()) {
            if(j) cout << ", ";
            cout << tree[i].children[j];
        }
        cout << "]" << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 130
memory_kb: 7096
final_rank: 10

code_snippet: #include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cmath>
#include <numeric>
#include <iomanip>
#include <map>
#include <queue>
#include <set>
#include <bitset>
#include <stack>
#include <list>
#include <unordered_set>
#include <unordered_map>
#define rep(i, a, b) for (int i = a; i < b; ++i)
using namespace std;

const int MOD = 998244353;
const int mod = 1000000007;

const long long INF = 1LL << 60;
constexpr double pi = 3.141592653589793238462643383279502884L;

struct Edge {
    int to;
    long long w;
    Edge(int to, long long w) : to(to), w(w) {}
};

using Graph = vector<vector<Edge>>;
using P = pair<int, int>;
using Pll = pair<long long, int>;
using ll = long long;

template<class T> bool chmin(T& a, T b) { if (a > b) { a = b; return true; } else return false; }
template<class T> bool chmax(T& a, T b) { if (a < b) { a = b; return true; } else return false; }

struct Node {
    int id;
    Node* parent;
    Node* first_child;
    Node* next_brother;

    Node() : id(-1), parent(nullptr), first_child(nullptr), next_brother(nullptr) {}
};

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int n; cin >> n;
    Node** nodes = new Node*[n];

    for (int i = 0; i < n; ++i) {
        nodes[i] = new Node;
    }

    for (int i = 0; i < n; ++i) {
        int id, k;
        cin >> id >> k;
        nodes[id]->id = id;
        int prev = -1;
        
        for (int j = 0; j < k; ++j) {
            int c; cin >> c;
            if (nodes[id]->first_child == nullptr) {
                nodes[id]->first_child = nodes[c];
            } else {
                if (prev != -1) {
                    nodes[prev]->next_brother = nodes[c];
                }
            }
            nodes[c]->parent = nodes[id];
            prev = c;
        }
    }

    for (int i = 0; i < n; ++i) {
        int parent = nodes[i]->parent == nullptr ? -1 : nodes[i]->parent->id;
        int depth = 0;
        Node* cur = nodes[i];
        while (true) {
            cur = cur->parent;
            if (cur == nullptr) {
                break;
            }
            ++depth;
        }

        string type = "";
        if (parent == -1) {
            type = "root";
        } else if (nodes[i]->first_child == nullptr) {
            type = "leaf";
        } else {
            type = "internal node";
        }

        string children = "[";
        cur = nodes[i]->first_child;
        while (cur) {
            if (cur != nodes[i]->first_child) {
                children += ", ";
            }
            int child = cur->id;
            children += to_string(child);
            cur = cur->next_brother;
        }
        children += "]";

        cout << "node " << i << ": " << flush;
        cout << "parent = " << parent << ", " << flush;
        cout << "depth = " << depth << ", " << flush;
        cout << type << ", " << flush;
        cout << children << endl;
    }

    for (int i = 0; i < n; ++i) {
        delete nodes[i];
    }

    delete[] nodes;
}
accuracy: 1
time_ms: 200
memory_kb: 9912
final_rank: 19

code_snippet: #include <iostream>
using namespace std;
#define MAX 100005
#define NIL -1

struct Node { int p, l, r; };

Node T[MAX];
int n, D[MAX];

void print(int u) {
    int i, c;
    cout << "node " << u << ": ";
    cout << "parent = " << T[u].p << ", ";
    cout << "depth = " << D[u] << ", ";

    if (T[u].p == NIL) cout << "root, ";
    else if (T[u].l == NIL) cout << "leaf, ";
    else cout << "internal node, ";

    cout << "[";

    for (int i=0, c = T[u].l; c != NIL ; i++, c = T[c].r) {
        if (i) cout << ", ";
        cout << c;
    }
    cout << "]" << endl;
}

// 再帰の深さを求める
void rec(int u, int p) {
    D[u] = p;
    if (T[u].r != NIL) rec(T[u].r, p); // 右の兄弟にも同じ深さを設定
    if (T[u].l != NIL) rec(T[u].l, p+1); // 最も左の子に自分の深さ+1を設定
}

int main() {
    int i, j, d, v, c, l, r;
    cin >> n;
    for (i = 0; i<n; i++) T[i].p = T[i].l = T[i].r = NIL;

    for (i = 0; i<n; i++) {
        cin >> v >> d;
        for (j = 0; j<d; j++) {
            cin >> c;
            if (j==0) T[v].l = c;
            else T[l].r = c;
            l = c;
            T[c].p = v;
        }
    }
    for (int i = 0; i<n; i++) {
        if (T[i].p == NIL) r=i;
    }

    rec(r,0);

    for (i = 0; i<n; i++) print(i);

    return 0;
}
accuracy: 1
time_ms: 130
memory_kb: 8024
final_rank: 12

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, s, n) for (int i = (s); i < (n); i++)


// vectorを出力する
void vecprint(vector<int> vec) {
    rep(i, vec.size()) {
        cout << vec[i] << endl;
    }
}
void newvecprint(vector<int> vec) {
    rep(i, vec.size()) {
        cout << vec[i] << " ";
    }
    cout << endl;
}

// 最大か最小を求める
int minval(vector<int> vec) {
    return *min_element(begin(vec), end(vec));
}
int maxval(vector<int> vec) {
    return *max_element(begin(vec), begin(vec));
}

void solve() {
    struct Node {
        int parent = -1;
        vector<int> child;
        int depth = 0;

        void add_child(int num) {
            child.push_back(num);
        }
    };

    ll n;
    cin >> n;
    vector<Node> tree(n);

    // 入力を受け取って、treeに格納する
    ll id, k, num;
    rep(i, n) {
        cin >> id >> k;
        rep(j, k) {
            cin >> num;
            tree[num].parent = id;
            tree[id].add_child(num);
        }
    }

    rep(i, n) {
        // 節点の深さを計算する
        ll d = 0;
        ll u = i;
        while (tree[u].parent != -1) {
            u = tree[u].parent;
            d++;
        }

        string type;
        if (tree[i].parent == -1) {
            type = "root";
        }
        else if (tree[i].child.size() == 0) {
            type = "leaf";
        }
        else {
            type = "internal node";
        }

        // 結果の出力
        cout << "node " << i << ": parent = " << tree[i].parent << ", depth = " << d << ", " << type << ", ";
        cout << "[";
        rep(j, tree[i].child.size()) {
            if (j+1 == tree[i].child.size()) {
                cout << tree[i].child[j];
            }
            else {
                cout << tree[i].child[j] << ", ";
            }
        }
        cout << "]" << endl;
    }
    return;
}

int main() {
    solve();
    return 0;
}
accuracy: 1
time_ms: 90
memory_kb: 8080
final_rank: 9

code_snippet: #define _USE_MATH_DEFINES
#include<iostream>
#include<string>
#include<sstream>
#include<vector>
#include <algorithm>
#include<cmath>
#include <cassert>
#include<bitset>
#include<queue>
#include<functional>
#include<utility>
#include<chrono>
#include<random>
#include <climits>
#include<map>
#include<set>
#include<list>
#include<stack>
#include <iomanip>
#include<stdio.h>
#include<math.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define MAX 200000
#define INFTY (1<<30)
typedef long long ll;

struct Node {
	int p, l, r;
};

Node T[MAX];

int n, D[MAX];

void depth(int u,int d) {
	D[u] = d;
	if (T[u].l != -1) {
		depth(T[u].l, d + 1);
	}

	if (T[u].r != -1) {
		depth(T[u].r, d);
	}
}

void print(int i) {
	cout << "node " << i << ": parent = " << T[i].p << ", depth = " << D[i] << ", ";
	if (T[i].p == -1) {
		cout << "root, [";

	}
	else if (T[i].l != -1) {
		cout << "internal node, [";
	}
	else {
		cout << "leaf, [";
	}
	int c;
	for (int j = 0, c = T[i].l; c != -1; j++, c = T[c].r) {
		if (j)cout << ", ";
		cout << c;
	}
	cout << "]";
}

int main() {
	cin >> n;
	int num,size;
	rep(i, n) {
		T[i].p = T[i].l = T[i].r = -1;
	}
	rep(i, n) {
		cin >> num >> size;
		if (size == 0)continue;
		int sum, presum;
		rep(j, size) {
			cin >> sum;
			T[sum].p = num;
			if (j == 0) {
				T[num].l = sum;
			}
			else {
				T[presum].r = sum;
			}
			presum = sum;
		}
	}
	int root;
	rep(i, n) {
		if (T[i].p == -1)root = i;
	}

	depth(root, 0);

	rep(i, n) {
		print(i);
		cout << endl;
	}

}
accuracy: 1
time_ms: 130
memory_kb: 4740
final_rank: 5

code_snippet: #include<iostream>
#include<string>
using namespace std;
#define MAX 100000
#define NIL -1

struct Node {
    int p, l, r;
};

int D[MAX];
Node T[MAX];

void depth(int u, int p) {
    D[u] = p;
    if (T[u].r != NIL) depth(T[u].r, p);
    if (T[u].l != NIL) depth(T[u].l, p+1);
}

void printChildren(int u) {
    int c = T[u].l;
    cout << "[";
    int i = 0;
    while (c != NIL) {
        if (i) cout << ", ";
        cout << c;
        c = T[c].r;
        i++;
    }
    cout << "]" << endl;
}

int main() {
    int n, id, k, c, old_c, root;
    root = NIL;
    cin >> n;
    for (int i = 0; i < n; i++) {
        T[i].l = T[i].r = T[i].p = NIL;
    }
    for (int i = 0; i < n; i++) {
        cin >> id >> k;
        for (int j = 0; j < k; j++) {
            cin >> c;
            if (j == 0) T[id].l = c;
            else T[old_c].r = c;
            old_c = c;
            T[c].p = id;
        }
    }

    for (int i = 0; i < n; i++) {
        if (T[i].p == NIL) root = i;
    }
    depth(root, 0);

    string type;
    for (int i = 0; i < n; i++) {
        if (T[i].p == NIL) type = "root";
        else if (T[i].l == NIL) type = "leaf";
        else type = "internal node";
        cout << "node " << i << ": parent = " << T[i].p << ", depth = " << D[i] << ", " << type << ", ";
        printChildren(i);
    }
    return 0;
}
accuracy: 1
time_ms: 130
memory_kb: 8024
final_rank: 12

code_snippet: #include <iostream>
#include <string>
using namespace std;

#define MAX 100000
#define NIL -1

struct Node {
    int parent;
    int left;
    int right;
};

Node T[MAX];
int D[MAX]; // depth

void setDepth(int u, int d) {
    D[u] = d;
    if (T[u].left != NIL) setDepth(T[u].left, d + 1); // 子は深さ+1
    if (T[u].right != NIL) setDepth(T[u].right, d);   // 兄弟は同じ深さ
}

void printChildren(int u) {
    int c = T[u].left;
    cout << "[";
    bool first = true;
    while (c != NIL) {
        if (!first) cout << ", ";
        cout << c;
        first = false;
        c = T[c].right;
    }
    cout << "]";
}

int main() {
    int n;
    cin >> n;

    // 初期化
    for (int i = 0; i < n; i++) {
        T[i].parent = NIL;
        T[i].left = NIL;
        T[i].right = NIL;
    }

    // 入力の読み込み
    for (int i = 0; i < n; i++) {
        int id, k, c, prev;
        cin >> id >> k;
        for (int j = 0; j < k; j++) {
            cin >> c;
            if (j == 0)
                T[id].left = c;
            else
                T[prev].right = c;
            prev = c;
            T[c].parent = id;
        }
    }

    // 根の探索
    int root = -1;
    for (int i = 0; i < n; i++) {
        if (T[i].parent == NIL) {
            root = i;
            break;
        }
    }

    // 深さの計算
    setDepth(root, 0);

    // 出力
    for (int i = 0; i < n; i++) {
        cout << "node " << i << ": parent = " << T[i].parent << ", depth = " << D[i] << ", ";

        if (T[i].parent == NIL)
            cout << "root, ";
        else if (T[i].left == NIL)
            cout << "leaf, ";
        else
            cout << "internal node, ";

        printChildren(i);
        cout << endl;
    }

    return 0;
}
accuracy: 1
time_ms: 130
memory_kb: 4880
final_rank: 6

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
typedef pair<int,int> PII;
const int N=1e5+5;
vector<int>E[N];
int n,pres[N],dep[N],cnts[N];
void dfs(int u=0,int pre=-1,int d=0){
    pres[u]=pre;
    dep[u]=d;
    for(auto v:E[u]){
        dfs(v,u,d+1);
    }
}
void solve(){
    cin>>n;
    for(int i=0;i<n;i++) cnts[i]=0;
    for(int i=0;i<n;i++){
        int id;cin>>id;
        int k;cin>>k;
        for(int j=0;j<k;j++){
            int v;cin>>v;
            cnts[v]++;
            E[id].push_back(v);
        }
    }
    int root=-1;
    for(int i=0;i<n;i++) if(cnts[i]==0) root=i;
    dfs(root);
    for(int i=0;i<n;i++){
        cout<<"node "<<i<<": parent = "<<pres[i]<<", depth = "<<dep[i]<<", ";
        if(i==root) cout<<"root";
        else if(E[i].empty()) cout<<"leaf";
        else cout<<"internal node";
        cout<<", [";
        bool first=true;
        for(auto v:E[i]){
            if(first) first=false;
            else cout<<", ";
            cout<<v;
        }
        cout<<"]"<<endl;
    }
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    int T=1;
    // cin>>T;
    for(int t=1;t<=T;t++){
        solve();
    }
    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 9248
final_rank: 8

code_snippet: #define _USE_MATH_DEFINES
#include<iostream>
#include<string>
#include<sstream>
#include<vector>
#include <algorithm>
#include<cmath>
#include <cassert>
#include<bitset>
#include<queue>
#include<functional>
#include<utility>
#include<chrono>
#include<random>
#include <climits>
#include<map>
#include<set>
#include<list>
#include<stack>
#include <iomanip>
#include<stdio.h>
#include<math.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define MAX 200000
#define INFTY (1<<30)
#define NIL -1
typedef long long ll;

struct Node {
	int parent, l, r;
};

Node T[MAX];

int n, D[MAX];

void rec(int u, int p) {
	D[u] = p;
	if (T[u].l != NIL)rec(T[u].l, p + 1);
	if (T[u].r != NIL)rec(T[u].r, p);
}

void print(int num) {
	cout << "node " << num << ": parent = ";
	if (T[num].parent == NIL) {
		cout << T[num].parent << ", depth = " << D[num] << ", root, [";
	}
	else {
		cout << T[num].parent << ", depth = " << D[num] << ", ";
		if (T[num].l == NIL)cout << "leaf, [";
		else cout << "internal node, [";
	}

	int c = T[num].l;
	for (int i = 0; c != NIL; i++, c = T[c].r) {
		if (i)cout << ", ";
		cout << c;
	}
	cout << "]" << endl;
}

int main() {
	cin >> n;

	rep(i, n) {
		T[i].parent = T[i].l = T[i].r = NIL;
	}
	int node, size;
	rep(i, n) {
		cin >> node >> size;
		if (size == 0)continue;
		int sum, presum;
		rep(j, size) {
			cin >> sum;
			if (j == 0) {
				T[node].l = sum;
				presum = sum;
			}
			else {
				T[presum].r = sum;
				presum = sum;
			}

			T[sum].parent = node;
		}
	}
	int root;
	rep(i, n) {
		if (T[i].parent == NIL)root = i;
	}

	rec(root, 0);

	rep(i, n)print(i);

	return 0;
}
accuracy: 1
time_ms: 120
memory_kb: 5008
final_rank: 4

code_snippet: #include <bits/stdc++.h>
using namespace std;

struct node
{
    int left = -1, parent = -1, right = -1;
};

int main()
{
    int n;
    cin >> n;

    node tree[n];

    int k, id;

    for (int i = 0; i < n; i++)
    {
        cin >> id >> k;

        vector<int> c(k);
        for (auto &i : c) cin >> i;

        if (k > 0)
            tree[id].left = c[0];
        for (int j = 0; j < k; j++)
        {
            tree[c[j]].parent = id;
            if (j+1 < k)
                tree[c[j]].right = c[j+1];
        }
    }

    for (int i = 0; i < n; i++)
    {
        cout << "node " << i << ": ";
        cout << "parent = " << tree[i].parent << ", ";

        int depth = 0;
        for (int p = tree[i].parent; p != -1;)
        {
            depth++;
            p = tree[p].parent;
        }
        cout << "depth = " << depth << ", ";

        if (tree[i].parent == -1) cout << "root, ";
        else if (tree[i].left == -1) cout << "leaf, ";
        else cout << "internal node, ";

        cout << "[";
        if (tree[i].left != -1)
        {
            int c = tree[i].left;
            cout << c;
            c = tree[c].right;
            while (c != -1)
            {
                cout << ", " << c;
                c = tree[c].right;
            }
        }
        cout << "]" << endl;

    }
}
accuracy: 1
time_ms: 130
memory_kb: 4492
final_rank: 3

code_snippet: #include<iostream>

using namespace std;

#define N_MAX 100000 // ノード数の最大
#define NIL -1

// ノードの種類
#define IS_ROOT 1
#define IS_INTERNAL_NODE 2
#define IS_LEAF 3


typedef struct {
    int parent; // 親ノード番号
    int children; // 子ノード
    int sibling; // 兄弟ノード
} Node;

Node nodes[N_MAX];


int depth(int i) {
    // ノードiの深さを算出
    if(nodes[i].parent == NIL){
        return 0;
    }else{
        return 1 + depth(nodes[i].parent);
    }
}

int find_type(int i){
    // ノードiが
    // root, internal node, leafのどれに該当するか
    if(nodes[i].parent == NIL){
        return IS_ROOT;
    }else if(nodes[i].children == NIL) {
        return IS_LEAF;
    }else
        return IS_INTERNAL_NODE;
}

int main(){

    int n;
    scanf("%d", &n);

    int id, k, child;

    for(int i=0;i<n;i++){
        nodes[i].parent = NIL;
        nodes[i].children = NIL;
        nodes[i].sibling = NIL;
    }

    for(int i=0;i<n;i++){
        scanf("%d %d", &id, &k);
        // cout << "ID=" << id << ", degree=" << k << endl;
        
        int current_node = id;
        for(int j=0; j<k; j++) {
            scanf("%d", &child);
            // cout << "child=" << child << endl;
            // 小ノードの親
            nodes[child].parent = id;

            if(j == 0) // 最初に入力された子である場合
                nodes[current_node].children = child;
            else
                nodes[current_node].sibling = child;
            current_node = child;
        }
    }

    for(int i=0; i<n; i++) {
        cout << "node " << i << ": ";
        cout << "parent = " << nodes[i].parent << ", ";
        cout << "depth = " << depth(i) << ", ";
        switch (find_type(i)) { 
            case IS_ROOT:
                cout << "root";
                break;
            case IS_INTERNAL_NODE:
                cout << "internal node";
                break;
            case IS_LEAF:
                cout << "leaf";
                break;
            default:
                break;
        }
        cout << ", [";
        if(nodes[i].children != NIL){
            cout << nodes[i].children;
            int current_node = nodes[i].children;
            while(nodes[current_node].sibling != NIL){
                cout << ", " << nodes[current_node].sibling;
                current_node = nodes[current_node].sibling;
            }
        }
        cout << "]";
        cout << endl;

    }

    return 0;
}
accuracy: 1
time_ms: 110
memory_kb: 4528
final_rank: 2

code_snippet: // 8.2 根付き木
// 2025.06.28

#include <iostream>
using namespace std;
static const int MAX = 100005;

struct node{int parent, left_child, right_sibling;};

node tree[MAX];     // 根っこ付き木
int depths[MAX];    // 深さリスト


// 深さを計算する再帰関数 (引数: 節点番号, 深さ)
void culcDepth(int id, int d){
    int child = tree[id].left_child, bro = tree[id].right_sibling;

    depths[id] = d;                         // 深さを記録
    if(child != -1) culcDepth(child, d+1);  // 子供の深さは+1
    if(bro != -1) culcDepth(bro, d);        // 兄弟の深さは同じ
}

// 兄弟を出力する関数
void showBros(int id){
    cout << id;
    int bro = tree[id].right_sibling;
    if(tree[id].right_sibling != -1){
        cout << ", ";
        showBros(bro);
    }
}

// メイン関数
int main() {
    // 初期化
    int n;
    cin >> n;
    for(int i=0; i<n; ++i) {
        tree[i].parent = -1;        // .parent 初期化
        tree[i].left_child = -1;    // .light_child 初期化
        tree[i].right_sibling = -1; // .right_sibling 初期化
    }

    // 入力
    for(int i=0, id, k; i<n; ++i){
        cin >> id >> k;
        for(int j=0, c, c_bro; j<k; ++j){       // 子供がいる場合
            cin >> c;
            tree[c].parent = id;                // c の parent は id (全子供共通)
            if(j==0){                           // c が長男の場合は
                tree[id].left_child = c;        // id の left_child に長男を追加
            }else{                              // c が次男以下の場合は
                tree[c_bro].right_sibling = c;  // c_bro(兄貴) の right_sibling に自分を追加
            }
            c_bro = c;                          // c も兄貴になる
        }
    }

    // 深さを計算(根を探して再帰)
    for(int i=0; i<n; ++i){
        if(tree[i].parent == -1){
            culcDepth(i, 0);
            break;
        }
    }

    // 出力
    for(int i=0; i<n; ++i){
        cout << "node " << i << ": parent = " << tree[i].parent << ", depth = " << depths[i] << ", ";
        if(tree[i].parent == -1) {
            cout << "root, [";
        }else if(tree[i].left_child == -1){
            cout << "leaf, [";
        }else {
            cout << "internal node, [";
        }
        if(tree[i].left_child != -1) showBros(tree[i].left_child);
        cout << "]" << endl;
    }

    return 0;
}
accuracy: 1
time_ms: 80
memory_kb: 5120
final_rank: 1