24h購物| | PChome| 登入
2012-05-13 07:49:21| 人氣1,707| 回應0 | 上一篇 | 下一篇

[UVA] 615 - Is It A Tree?

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


 Is It A Tree? 

A tree is a well-known data structure that is either empty (null,void, nothing) oris a set of one or more nodes connected by directed edges betweennodes satisfying the following properties.

  • There is exactly one node, called the root, to which no directededges point.
  • Every node except the root has exactly one edge pointing to it.
  • There is a unique sequence of directed edges from the root to each node.

For example, consider the illustrations below, in which nodes arerepresented by circles andedges are represented by lines with arrowheads. The first two of these aretrees, but the last is not.

$textstyle parbox{.3textwidth}{begin{center}mbox{}epsfxsize 1.5inepsfbox{p615a.eps}end{center}}$$textstyle parbox{.4textwidth}{begin{center}mbox{}epsfxsize 2inepsfbox{p615b.eps}end{center}}$$textstyle parbox{.29textwidth}{begin{center}mbox{}epsfxsize 1.5inepsfbox{p615c.eps}end{center}}$

In this problem you will be given several descriptions of collections of nodesconnected by directed edges. For each of these you are to determine ifthe collection satisfies the definition of a tree or not.

Input 

The input will consist of a sequence of descriptions (test cases) followed by apair of negative integers. Each test case will consist of asequence of edge descriptionsfollowed by a pair of zeroes Each edge description will consist of a pairof integers;the first integer identifies the node from which the edge begins, and thesecond integer identifies the node to which the edge is directed.Node numbers will always be greater than zero.

Output 

For each test case display the line ``Case k is a tree."or the line ``Case k is not a tree.", where kcorresponds to the test case number (they are sequentially numbered starting with 1).

Sample Input 

6 8  5 3  5 2  6 45 6  0 08 1  7 3  6 2  8 9  7 57 4  7 8  7 6  0 03 8  6 8  6 45 3  5 6  5 2  0 0-1 -1

Sample Output 

Case 1 is a tree.Case 2 is a tree.Case 3 is not a tree.



Miguel A. Revilla
1999-03-24

#include <stdio.h>
#include <stdlib.h>
#include <map>
#include <queue>
#include <vector>
using namespace std;
struct Arc {
    int to;
};
int main() {
    int i, j, t = 0;
    map<int, int> node;
    while(1) {
        int size = 0, x, y;
        int indeg[100] = {};
        vector<Arc> myLink[100];
        Arc tmp;
        node.clear();
        while(scanf("%d %d", &i, &j) == 2) {
            if(i < 0 && j < 0)  goto End;
            if(!i && !j)    break;
            x = node[i];
            if(!x) {
                ++size;
                node[i] = size;
                x = size;
            }
            y = node[j];
            if(!y) {
                ++size;
                node[j] = size;
                y = size;
            }
            tmp.to = y;
            indeg[y]++;
            myLink[x].push_back(tmp);
        }
        printf("Case %d is ", ++t);
        int flag = 0, cnt = 0, root;
        for(i = 1; i <= size; i++) {
            if(indeg[i] > 1) {
                flag = 1;
            }
            if(indeg[i] == 0) {
                cnt++;
                root = i;
            }
        }
        if(cnt != 1 && size)
            flag = 1;
        if(flag == 0 && size) {
            queue<int> Q;
            int in = 0, used[100] = {}, tn;
            Q.push(root);
            used[root] = 1;
            while(!Q.empty()) {
                tn = Q.front();
                Q.pop();
                in++;
                for(vector<Arc>::iterator i = myLink[tn].begin(); i != myLink[tn].end(); i++) {
                    if(!used[i->to]) {
                        used[i->to] = 1;
                        Q.push(i->to);
                    }
                }
            }
            if(in != size)
                flag = 1;
        }
        if(!flag)
            puts("a tree.");
        else
            puts("not a tree.");
    }
    End:
    return 0;
}

台長: Morris
人氣(1,707) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][Math] 10338 - Mischievous Children
此分類上一篇:[UVA][Trie] 11362 - Phone List

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