Query poj1308 hd1272

Source: Internet
Author: User

Query poj1308 hd1272

 

Enter several groups of test data. The input ends at (-1-1. Each group of test data is ended with input (0 0. Then, based on all the data given (father, child), determine whether a tree can be formed.

Analysis: to understand that a tree has only one root node, we can determine whether there are multiple trees and whether each node has only one Father's Day node. Then we can determine whether the tree is a ring, ring formation is not a tree.

Note: ① It can be an empty tree. ② it is impossible for the given node to form a forest (multiple trees). It must be a tree.

# Include
  
   
# Include
   
    
# Include
    
     
Using namespace std; int out, flag, x, y, num, pre [10010]; int find (int a) // find the root node {int r, I, j; r = a; I = a; while (pre [r]! = R) r = pre [r]; while (pre [I]! = R) {j = pre [I]; pre [I] = r; I = j;} return r;} int main () {num = 0; out = 1; while (out) {// initialize the root node of all nodes for (int I = 1; I <= 10000; I ++) pre [I] = I; flag = 1; while (scanf (% d, & x, & y) {if (x = 0 & y = 0) break; else if (x =-1 & y =-1) {out = 0; break;} int fx = find (x); int fy = find (y ); // here, we determine whether the ring is formed. // if the root nodes of x and y are the same, they belong to the same tree. // If x is the Father's Day of y, then the ring if (fx = fy) flag = 0; // if x and y root nodes Different, that is, it does not belong to the same tree, then it is merged into a tree else if (fx! = Fy) pre [fy] = fx;} int k = 0; // here we determine whether it is a forest or not, for all nodes (excluding vertices not involved) the root node // for statistics. If not all are the same, it indicates that there are multiple trails and there are multiple trees. Otherwise, it is a tree. For (int I = 1; I <= 10000; I ++) {int ans = find (I); if (ans! = I & k = 0) k = ans; else if (k! = 0 & ans! = I) {if (ans! = K) flag = 0 ;}}if (out = 1 & flag = 0) printf (Case % d is not a tree ., ++ num); else if (out = 1 & flag = 1) printf (Case % d is a tree ., ++ num);} return 0 ;}
    
   
  
Hangdian's 1272 and this question can be changed slightly.
#include
  
   #include
   
    #include
    
     using namespace std;int out, flag, x, y, pre[100010];int find(int a){    int r, i, j;    r = a; i = a;    while(pre[r] != r)        r = pre[r];    while(pre[i] != r)    {        j = pre[i];        pre[i] = r;        i = j;    }    return r;}int main(){    out = 1;    while(out)    {        for(int i = 1; i <= 100000; i++)        {            pre[i] = i;        }        flag = 1;        while(scanf(%d%d, &x, &y))        {            if(x == 0 && y == 0)                break;            else if(x == -1 && y == -1)            {                out = 0;                break;            }            int fx = find(x);            int fy = find(y);            if(fx != fy)            {                pre[fx] = fy;            }            else if(fx == fy)            {                flag = 0;            }        }        int k = 0;        for(int i = 1; i <= 100000; i++)        {            int ans = find(i);            if(ans != i && k == 0)                k = ans;            else if(k != 0 && ans != i)            {                if(ans != k)                    flag = 0;            }        }        if(out == 1 && flag == 0)            printf(No);        else if(out == 1 && flag == 1)            printf(Yes);    }    return 0;}
    
   
  

Related Article

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.