HDU 1325 is it a tree?

Source: Internet
Author: User
Problem descriptiona tree is a well-known data structure that is either empty (null, void, nothing) or is a set of one or more nodes connected by directed edges between nodes satisfying the following properties.
There is exactly one node, called the root, to which no directed edges point.

Every node should t 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 specified strations below, in which nodes are represented by circles and edges are represented by lines with arrowheads. The first two of these are trees, but the last is not.



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

Inputthe input will consist of a sequence of descriptions (test cases) followed by a pair of negative integers. each test case will consist of a sequence of edge descriptions followed by a pair of zeroes each edge description will consist of a pair of integers; the first integer identifies the node from which the edge begins, and the second integer identifies the node to which the edge is directed. node numbers will always be greater than zero. outputfor each test case display the line ''case K is a tree. "or the line'' case K is not a tree. ", where k corresponds to the test case number (they are sequentially numbered starting with 1 ). sample input6 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 outputcase 1 is a tree. case 2 is a tree. case 3 is not a tree.

 

Question: give you some directed edges and ask if they can form a tree ..

Idea: Unlike the maze of HDU 1272, This is a directed graph, which is an undirected graph.

1. If the tree edge is treated as an undirected edge, all vertices belong to the same set and use and query the set.

2. Only one entry level is allowed between all vertices and multiple vertices.

3. Only one root node can exist.

4. 0, 0,

 1 #include <stdio.h> 2 #include <string.h> 3 #define MAXN    (1002) 4  5 int    set[MAXN]; 6 int    flag, tcase; 7  8 void init() 9 {10     int i;11     memset(set, -1, sizeof(set));12     flag = 0;13 }14 15 int find(int x)16 {17     if (set[x] == -1)18         set[x] = x;19     if (set[x] == x)20         return x;21     return set[x] = find(set[x]);22 }23 24 int main()25 {26     int i, j;27     int x, y;28     tcase = 1;29     init();30     while (scanf("%d %d", &x, &y) != EOF)31     {32         if (x == -1 && y == -1)33             break;34         if (x == 0 && y == 0)35         {36             printf ("Case %d", tcase++);37             if (!flag)38             {39                 j = 0;40                 for (i = 0; i < MAXN; ++i)41                     if (set[i] == i)42                         ++j;43                     if (j > 1)    // not a tree: 1 2 3 4 0 0, a tree: 0 044                         flag = 1;45             }46             if (!flag)47             {48                 printf (" is a tree.\n");49             }50             else51                 printf (" is not a tree.\n");52             init();53             continue;54         }55         int px = find(x);56         int py = find(y);57 58         if (py != y && px != py || x == y || (x != y && px == py))// not a tree: 1 2 1 3 2 1 0 059         {// not a tree: 1 1 0 060             flag = 1;61         }62         else63             set[py] = set[px];64     }65     return 0;66 }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.