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.
Give a relationship between vertices and edges and determine whether the relationship is a tree (Note: The forest is not acceptable !!!)
Idea: Read the edge information, merge the two points, and send the FA of the latter point to the former point. If the two points are already in the same set, they are not trees, finally, you can determine whether it is a forest...
Code: # Include <iostream> # include <cstdio> # include <cstring> using namespace STD; int Fa [100001] = {0}; int visit [100001] = {false }, visitt [100001] = {false}; int Rool (int x) {If (Fa [x]! = X) Fa [x] = Rool (Fa [x]); Return Fa [X];} int main () {int A, B, R1, R2, ci = 0, I, T, maxn; bool F = false; while (CIN> A> B) {++ ci; F = false; if (A =-1 & B =-1) break; for (I = 0; I <= 100000; ++ I) Fa [I] = I; memset (visit, false, sizeof (visit); memset (visitt, false, sizeof (visitt); t = maxn = 0; while (! = 0 | B! = 0) {if (a> maxn) maxn = A; If (B> maxn) maxn = B; visit [a] = visit [B] = true; r1 = Rool (a); r2 = Rool (B); If (r1 = R2) F = true; Fa [R2] = R1; cin> A> B;} If (! F) {for (I = 1; I <= maxn; ++ I) if (visit [I]) {R1 = Rool (I); If (! Visitt [R1]) {visitt [R1] = true; ++ t ;}} if (T> 1) F = true ;} cout <"case" <CI <"is"; if (f) cout <"not"; cout <"a tree. "<Endl ;}} |
Poj1308 is it a tree? (Easy ...)