UVA280 LA5588 VertexDFS

Posted 海岛Blog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVA280 LA5588 VertexDFS相关的知识,希望对你有一定的参考价值。

Write a program that searches a directed graph for vertices which are inaccessible from a given starting vertex.
    A directed graph is represented by n vertices where 1 ≤ n ≤ 100, numbered consecutively 1 . . . n , and a series of edges p -> q which connect the pair of nodes p and q in one direction only.
    A vertex r is reachable from a vertex p if there is an edge p -> r, or if there exists some vertex q for which q is reachable from p and r is reachable from q.
    A vertex r is inaccessible from a vertex p if r is not reachable from p.
Input
The input data for this program consists of several directed graphs and starting nodes.
    For each graph, there is first one line containing a single integer n. This is the number of vertices in the graph.
    Following, there will be a group of lines, each containing a set of integers. The group is terminated by a line which contains only the integer ‘0’. Each set represent a collection of edges. The first integer in the set, i, is the starting vertex, while the next group of integers, j . . . k, define the series of edges i -> j . . . i -> k, and the last integer on the line is always ‘0’. Each possible start vertex i, 1 ≤ i ≤ n will appear once or not at all. Following each graph definition, there will be one line containing a list of integers. The first integer on the line will specify how many integers follow. Each of the following integers represents a start vertex to be investigated by your program. The next graph then follows. If there are no more graphs, the next line of the file will contain only the integer ‘0’.
Output
For each start vertex to be investigated, your program should identify all the vertices which are inaccessible from the given start vertex. Each list should appear on one line, beginning with the count of inaccessible vertices and followed by the inaccessible vertex numbers.
Sample Input
3
1 2 0
2 2 0
3 1 2 0
0
2 1 2
0
Sample Output
2 1 3
2 1 3

问题链接UVA280 LA5588 Vertex
问题简述:统计一个有向图中,指定结点不能到达的结点。
问题分析:用DFS来实现,不解释。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA280 LA5588 Vertex */

#include <bits/stdc++.h>

using namespace std;

const int N = 100 + 1;
int g[N][N], vis[N], cnt;

void dfs(int n, int u)
{
    for (int v = 1; v <= n; v++)
        if (g[u][v] == 1 && vis[v] == 0) {
            vis[v] = 1;
            cnt--;
            dfs(n, v);
        }
}

int main()
{
    int n, u, v;
    while (cin >> n && n) {
        memset(g, 0, sizeof g);
        while (cin >> u && u)
            while (cin >> v && v)
                g[u][v] = 1;

        int num;
        cin >> num;
        for (int i = 1; i <= num; i++) {
            cin >> u;

            memset(vis, 0, sizeof vis);
            cnt = n;
            dfs(n, u);

            cout << cnt;
            for (int j = 1; j <= n; j++)
                if (vis[j] == 0) {
                    if (cnt) cout << " ";
                    cout << j;
                    cnt--;
                }
            cout << endl;
        }
    }

    return 0;
}

以上是关于UVA280 LA5588 VertexDFS的主要内容,如果未能解决你的问题,请参考以下文章

UVA557 LA5578 Burger概率

UVA763 LA5339 Fibinary Numbers大数

UVA1224 LA3904 Tile Code铺砖问题

UVA1482 LA5059 Playing With StonesSG函数

UVA1226 LA3997 Numerical surprises大数

UVA1258 LA4721 Nowhere Money数学计算