L2-031 Cronicas (25 points)

The famous spymaster 007 needs to perform a task, get the enemy's confidential information. Known intelligence hidden in an underground maze, a labyrinth has only one entrance, there are many pathways, every road leads to a door. Behind each door or a room, or there are lots of different paths, each route is also leading to a door ...... his hands with a table, other spies to help him collect intelligence, they write down the number of each door, and the number of each door a path behind the door reached. 007 found that there is no two roads leading to the same door.

Insider told him that intelligence is hidden in the depths of the maze. But the maze is too big, he needs your help - please help him find programming furthest from the entrance door.

Input formats:

Firstly, a positive integer in the input line  N ( <), the number of gates. The last  N lines, the  line i ( 1) is described in the following format numbered  door behind door i can lead to:

K D[1] D[2] ... D[K] 
 

Wherein  K is the number of channels, followed by a number each door.

Output formats:

Output furthest from the entrance door in a row number. Subject to ensure that such a result is unique.

Sample input:

13
3 2 3 4
2 5 6
1 7
1 8
1 9
0
2 11 10
1 13
0
0
1 12
0
0
 

Sample output:

12

Shows the relationship between nodes of a tree, to find the most required leaf node.

Code:
#include <iostream>
#include <cstdio>
#define MAX 100005
using namespace std;
int n,m,t;
int f[MAX],val[MAX];
void get(int k) {
    if(f[k] == 0) {
        val[k] = 1;
        return;
    }
    if(val[f[k]] == 0) get(f[k]);
    val[k] = val[f[k]] + 1;
}
int main() {
    int k,d;
    scanf("%d",&n);
    for(int i = 1;i <= n;i ++) {
        scanf("%d",&k);
        for(int j = 0;j < k;j ++) {
            scanf("%d",&d);
            f[d] = i;
        }
    }
    for(int i = 1;i <= n;i ++) {
        get(i);
        if(m < val[i]) m = val[i],t = i;
    }
    printf("%d",t);
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/8023spz/p/12165310.html