[PTA] Cronicas

Title repeat

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, input a positive integer N (<10 in the line 5 ), a number of gates. The last N lines, line i (1≤i≤N) number in the following format described as the door leading to the back door i can:

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

Where 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

answer

First find the entrance (entry point is not present), carried out from this junction to traverse the level, if better solution is found, updates the global variables.

100 ++ AND

#include <iostream>
#include <bits/stdc++.h>
#define MAX 100010
using namespace std;
vector<int> vs[MAX];
bool vis[MAX];
int max_value=-1;
int max_id;
void bfs(int id,int depth)
{
    if(depth>max_value)
    {
        max_value=depth;
        max_id=id;
    }
    for(int i=0;i<vs[id].size();i++)
    {
        bfs(vs[id][i],depth+1);
    }
}
int main()
{
    ios::sync_with_stdio(false);
    int n,k,tmp;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>k;
        for(int j=0;j<k;j++)
        {
            cin>>tmp;
            vis[tmp]=true;
            vs[i].push_back(tmp);
        }
    }
    for(int i=1;i<=n;i++)
    {
        if(!vis[i])
        {
             bfs(i,0);
        }
    }
    cout<<max_id;
    return 0;
}

Published 243 original articles · won praise 106 · views 50000 +

Guess you like

Origin blog.csdn.net/weixin_43889841/article/details/104333880