Question 222. 2022 Winter Holiday Ladder Competition Training-7-11 Going Deep into the Tiger's Den (25 points)


Question 222. 2022 Winter Holiday Ladder Competition Training-7-11 Going Deep into the Tiger's Den (25 points)


1. The topic

insert image description here

2. Problem solving

The door you want to find the farthest is bfs to the end, and then output the last node and you are done. It should be noted that you need to find the start node yourself, that is, the point that has not been entered. The following code uses flag to mark whether the node corresponding to the subscript is entered.

#include <bits/stdc++.h>

using namespace std;

vector<int> G[100001];
int flag[100001];

void bfs(int v0)
{
    
    
    queue<int> q;
    int res;
    q.push(v0);
    while(!q.empty())
    {
    
    
        int v=q.front();
        q.pop();
        res=v;
        for(int i=0;i<G[v].size();i++)
        {
    
    
            q.push(G[v][i]);
        }
    }
    cout<<res<<endl;
    return;
}

int main()
{
    
    
    int N;
    cin>>N;
    for(int i=1;i<=N;i++)
    {
    
    
        int K;
        scanf("%d",&K);
        for(int j=0;j<K;j++)
        {
    
    
            int Dj;
            scanf("%d",&Dj);
            flag[Dj]=1;
            G[i].push_back(Dj);
        }
    }
    int v0;//入口得自己找,就是没有路进入的点
    for(int i=1;i<=N;i++)
    {
    
    
        if(flag[i]==0)
        {
    
    
           v0=i;
           break;
        }
    }
    bfs(v0);
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324346543&siteId=291194637