24h購物| | PChome| 登入
2011-12-10 07:23:22| 人氣1,498| 回應0 | 上一篇 | 下一篇

[UVA] 280 - Vertex

推薦 0 收藏 0 轉貼0 訂閱站台


 Vertex 

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 tex2html_wrap_inline31 , numbered consecutively tex2html_wrap_inline33 , 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, tex2html_wrap_inline73 , define the series of edges i -> tex2html_wrap_inline77 -> k, and the last integer on the line is always 0. Each possible start vertex i, tex2html_wrap_inline83 will appear once or not at all. Following each graph definition, there will be a one line containing 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



作法 : 給一張圖, 判斷不能走到的節點

#include<stdio.h>
#include<string.h>
#define oo 0xffff
int main() {
    int n, x, y, i, j, k, m;
    int map[101][101];
    while(scanf("%d", &n) == 1 && n) {
        memset(map, 0, sizeof(map));
        while(scanf("%d", &x) == 1 && x) {
            while(scanf("%d", &y) == 1 && y)
                map[x][y] = 1;
        }
        for(i = 1; i <= n; i++)
            for(j = 1; j <= n; j++)
                if(map[i][j] == 0)
                    map[i][j] = oo;
        for(k = 1; k <= n; k++)
            for(i = 1; i <= n; i++)
                for(j = 1; j <= n; j++)
                    if(map[i][j] > map[i][k] + map[k][j])
                        map[i][j] = map[i][k] + map[k][j];
        scanf("%d", &m);
        while(m--) {
            scanf("%d", &x);
            int Ans[101], At = 0;
            for(i = 1; i <= n; i++)
                if(map[x][i] == oo)
                    Ans[At++] = i;
            printf("%d", At);
            for(i = 0; i < At; i++)
                printf(" %d", Ans[i]);
            puts("");
        }
    }
    return 0;
}

台長: Morris
人氣(1,498) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA] 429 - Word Transformation
此分類上一篇:[UVA] 12347 - Binary Search Tree

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文