Poj 1308 and check the set to determine whether a graph is a tree"

Source: Internet
Author: User

Determine whether a graph is a tree:

Method 1:

Topological sorting:

(1) Exclusive Ring

(2) Except for one node (Root), the inbound traffic of each node is 1.

Method 2:

Query set:

(1) Ensure that the graph is a Unicom graph (non-forest)

(2) to ensure that no rings or similar rings appear (1-> 2, 2-> 3, 1-> 3), merge each edge. If a ring or similar ring appears, the two nodes at the end of the edge will have the same parent node when find_set (the parent node here refers to and queries the collection representatives in the aggregation tree ).

# Include <iostream> <br/> # include <cstring> <br/> using namespace STD; <br/> const int max_size = 100000; <br/> int P [max_size]; <br/> int R [max_size]; <br/> int num = 0; <br/> int ST = 0; <br/> bool flag = false; <br/> void make_set (int x) <br/>{< br/> P [x] = X; <br/> r [x] = 1; <br/>}< br/> int find_set (int x) <br/>{< br/> If (P [x]! = X) <br/> P [x] = find_set (P [x]); <br/> return P [x]; <br/>}< br/> void union_set (int x, int y) <br/>{< br/> If (R [x]> r [y]) <br/> P [y] = x; <br/> else if (R [x] <R [y]) <br/> P [x] = y; <br/> else <br/> {<br/> r [y] ++; <br/> P [x] = y; <br/>}< br/> int main () <br/>{< br/> int U, V; <br/> CIN> U; <br/> CIN> V; <br/> int c = 1; <br/> int I = 0; <br/> while (u! =-1 & V! =-1) <br/>{< br/> If (u = 0 & V = 0) <br/> {<br/> cout <"case" <C <"is a tree. "<Endl; <br/> C ++; <br/> CIN> U; <br/> CIN> V; <br/> continue; <br/>}< br/> ST = 0; <br/> memset (p, 0, sizeof (p); <br/> flag = false; <br/> DO <br/> {<br/> If (P [u] = 0) <br/> {<br/> make_set (U ); <br/> num ++; <br/>}< br/> If (P [v] = 0) <br/>{< br/> make_set (V); <br/> num ++; <br/>}< br/> ST = u; <br/> int T1 = find_set (U); <br/> int t2 = find_set (V ); <Br/> If (T1 = t2) <br/>{< br/> flag = true; <br/>}< br/> union_set (T1, t2); <br/> CIN> U; <br/> CIN> V; <br/>}while (u | V ); <br/> If (flag = true) <br/> cout <"case" <C <"is not a tree. "<Endl; <br/> else if (flag = false) <br/>{< br/> for (I = 0; I <max_size; I ++) <br/>{< br/> If (P [I]! = 0) <br/> If (find_set (I )! = Find_set (ST) <br/>{< br/> cout <"case" <C <"is not a tree. "<Endl; <br/> break; <br/>}< br/> if (I = max_size) <br/> cout <"case" <C <"is a tree. "<Endl; <br/>}< br/> C ++; <br/> CIN> U; <br/> CIN> V; <br/>}< br/>

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.