Deep search of ac graph theory

portal

stingy country

Time Limit: 1000  ms | Memory Limit: 65535  KB
Difficulty: 3
describe
There are N cities in a stingy country, and there are only N-1 roads between these N cities to connect these N cities. Now, Tom is in city S, he has a map of the country, and he wants to know which city is the previous city he has to pass through if he wants to visit city T (assuming you don't go the same way).
enter
Enter an integer M in the first line to indicate that there are M (1<=M<=5)
groups of test data. Enter a positive integer N (1<=N<=100000) and a positive integer S ( 1<=S<=100000), N represents the total number of cities, S represents the number of the city where the visitor is located The
following N-1 lines, each line has two positive integers a,b(1<=a,b< =N), indicating that there is a road connection between city a and city b.
output
Input N positive integers for each set of test data, where the ith number represents the number of the previous city that must be passed from S to city i. (When i=S, please output -1)
sample input
1
10 1
1 9
1 8
8 10
10 3
8 6
1 2
10 4
9 5
3 7
Sample output
-1 1 10 10 9 8 3 1 1 8




#include <iostream>//Introduce input and output stream header files
#include<stdlib.h>//The function malloc is required to apply for space
using namespace std;//Introduce standard namespace
#define MAXN 100005 //Define the maximum number of vertices
typedef struct g//Because there are too many vertices and only n-1 edges, so choose the adjacency list decisively
{
    int v;//Define the serial number connected to it
    int id;//Define its own id
    struct g* next;//The address of the next node adjacent to it
}*Game;
int n,s;
Gra head[MAXN];//Define the header
int vis[MAXN];//Access flag array
int p[MAXN];//Prefix array
void build(int w,int v)//Build an adjacency list
{
    Game a, b;
    a=(Gra)malloc(sizeof(struct g));
    b=(Gra)malloc(sizeof(struct g));
    a->v=w;
    a->id=v;
    a->next=head[v];//Front insertion
    head[v]=a;
    b->v=v;
    b->id=w;
    b->next=head[w];
    head[w]=b;
}
void DFS(Gra t,int pre)//Deep search
{
    vis[t->id]=1;//Access is set to 1, t->id has been visited
    if(t->id==pre)//If the prefix is ​​the same as the node to be visited
    {
        p[t->id]=-1;//The prefix is ​​set to -1
    }
    else
    {
        p[t->id]=pre;//Otherwise set to pre
    }
    Gra pp=t;//traverse all the adjacent points of the point
    while(pp)
    {
        if(vis[pp->v]==0)//If the store has not been visited
        {
           DFS(head[pp->v],pp->id);//Access the node
        }
        pp=pp->next;
    }
}
intmain()
{
    int ncase;//Number of groups
    cin>>ncase;
    while(ncase--)
    {
        cin>>n>>s;
        //Initialization headers are all NULL
        for(int i=0; i<=n; i++)
        {
            head[i]=NULL;//The head nodes are all initialized to NULL
            show [i] = 0; // show 清 0
        }
        // build a map
        for(int i=0; i<n-1; i++)
        {
            int w,v;
            cin>>w>>v;
            build(w,v);
        }
        //deep search
        DFS(head[s],s);
        // output
        for(int i=1;i<=n;i++)
            cout<<p[i]<<" ";
        cout<<endl;
    }

    return 0;
}

Guess you like

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