二Day 2 B - Depth First Search

二Day 2 B - Depth First Search


题目正文

Depth-first search (DFS) follows the strategy to search ”deeper” in the graph whenever possible. In DFS, edges are recursively explored out of the most recently discovered vertex v that still has unexplored edges leaving it. When all of v’s edges have been explored, the search ”backtracks” to explore edges leaving the vertex from which v was discovered.

This process continues until all the vertices that are reachable from the original source vertex have been discovered. If any undiscovered vertices remain, then one of them is selected as a new source and the search is repeated from that source.

DFS timestamps each vertex as follows:

d[v] records when v is first discovered.
f[v] records when the search finishes examining v’s adjacency list.
Write a program which reads a directed graph G=(V,E) and demonstrates DFS on the graph based on the following rules:

G is given in an adjacency-list. Vertices are identified by IDs 1,2,…n respectively.
IDs in the adjacency list are arranged in ascending order.
The program should report the discover time and the finish time for each vertex.
When there are several candidates to visit during DFS, the algorithm should select the vertex with the smallest ID.
The timestamp starts with 1.
Input
In the first line, an integer n denoting the number of vertices of G is given. In the next n lines, adjacency lists of u are given in the following format:

u k v1 v2 … vk

u is ID of the vertex and k denotes its degree. vi are IDs of vertices adjacent to u.

Output
For each vertex, print id, d and f separated by a space character in a line. id is ID of the vertex, d and f is the discover time and the finish time respectively. Print in order of vertex IDs.

Constraints
1≤n≤100
Sample Input 1
4
1 1 2
2 1 4
3 0
4 1 3
Sample Output 1
1 1 8
2 2 7
3 4 5
4 3 6
Sample Input 2
6
1 2 2 3
2 2 3 4
3 1 5
4 1 6
5 1 6
6 0
Sample Output 2
1 1 12
2 2 11
3 3 8
4 9 10
5 4 7
6 5 6

题意

深搜,求出每个顶点的访问开始时间和结束时间

代码

代码:

//1.定义两个自定义函数一个为dfs(),另一个是dfs_visit(),
//2.定义一个二维数组e[][],一个一维数组vis[],d[],f[],最后两个为开始时间和结束时间
//3.自定义函数dfs_visit(),传入顶点参数,访问值赋值为1;开始时间加1,循环遍历相邻节点,if语句如果
//相邻节点为零,跳出,if语句如果没有访问,调用自身进行访问;
//4.第二次访问结束,就顶点标记为2,完成时间++;
//5.dfs(),循环遍历每个节点的访问值标记为0;时间赋值为0;for循环遍历顶点,如果访问值为0,则调用dfs_visit()访问
//6.循环输出结果,节点,开始时间和结束时间
//7.主函数描述邻接矩阵,输入n,双层循环,e[][]=0;双层循环输入u,k;u--;第二层循环输入v;v--;e[u][v]=1;
//8.调用dfs();
#include<stdio.h>
#include<iostream>

using namespace std;
const int maxn=100;
int e[maxn][maxn],vis[maxn],d[maxn],f[maxn],times;
int n;
void dfs_visit(int u)
{
    
    
    vis[u]=1;
    d[u]=++times;
    for(int v=0;v<n;v++)
    {
    
    
        if(e[u][v]==0)continue;
        if(vis[v]==0)dfs_visit(v);

    }
    vis[u]=2;
    f[u]=++times;
}
void dfs()
{
    
    
    for(int i=0;i<n;i++)
    {
    
    
        vis[i]=0;
    }
    times=0;
    for(int u=0;u<n;u++)
    {
    
    
        if(vis[u]==0)
            dfs_visit(u);
    }
    for(int i=0;i<n;i++)
        printf("%d %d %d\n",(i+1),d[i],f[i]);
}
int main()
{
    
    
    int u,v,k;
   cin>>n;
    for(int i=0;i<n;i++)
        for(int j=0;j<n;j++)
        e[i][j]=0;
        for(int i=0;i<n;i++)
        {
    
    
            scanf("%d%d",&u,&k);
            u--;
            for(int j=0;j<k;j++)
            {
    
    
                scanf("%d",&v);
                v--;
                e[u][v]=1;
            }
        }
        dfs();
        return 0;
}

总结

dfs()模板题

猜你喜欢

转载自blog.csdn.net/MarigoldLi/article/details/119455115