CF 574E (Bear and Drawing-2 * n dot matrix painting tree)

Source: Internet
Author: User

CF 574E (Bear and Drawing-2 * n dot matrix painting tree)

 

E. Bear and Drawing time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output

Limak is a little bear who learns to draw. People usually start with houses, fences and flowers but why wowould bears do it? Limak lives in the forest and he decides to draw a tree.

Recall that tree is a connected graph consistingNVertices andNInvalid-limit 1 edges.

Limak chose a treeNVertices. He has infinite strip of paper with two parallel rows of dots. Little bear wants to assign vertices of a tree to someNDistinct dots on a paper so that edges wowould intersect only at their endpoints-drawn tree must be planar. Below you can see one of correct drawings for the first sample test.

Is it possible for Limak to draw chosen tree?

Input

The first line contains single integerN(1 digit ≤ DigitNLimit ≤ limit 105 ).

NextNResponse-interval 1 lines contain description of a tree.I-Th of them contains two space-separated integersAIAndBI(1 digit ≤ DigitAI, Bytes,BILimit ≤ limitN, Bytes,AI  =BI) Denoting an edge between verticesAIAndBI. It's guaranteed that given description forms a tree.

Output

Print Yes (without the quotes) if Limak can draw chosen tree. Otherwise, print No (without the quotes ).

Sample test (s) input
81 21 31 66 46 76 57 8
Output
Yes
Input
131 21 31 42 52 62 73 83 93 104 114 124 13
Output
No

 

 

Construct a solution for this question

The structure is like this.

 

 

First, we mark the leaf node and the connected chain as is_lef.

Then count the number of links separated by each vertex (not counted as a big subtree)

Then, except for the chain and Y-shape, the vertex can be divided into two Subtrees at most, otherwise there is no solution.

 

 

 

 

# Include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
# Include
     
      
# Include
      
        # Include
       
         # Include
        
          Using namespace std; # define For (I, n) for (int I = 1; I <= n; I ++) # define Fork (I, k, n) for (int I = k; I <= n; I ++) # define Rep (I, n) for (int I = 0; I
         
           = 0; I --) # define Forp (x) for (int p = pre [x]; p = next [p]) # define Forpiter (x) for (int & p = iter [x]; p = next [p]) # define Lson (x <1) # define Rson (x <1) + 1) # define MEM (a) memset (a, 0, sizeof (a); # define MEMI (a) memset (a, 127, sizeof ()); # define MEMi (a) memset (a, 128, sizeof (a); # define INF (2139062143) # define F (100000007) # define MAXN (200000 + 10) typedef long ll; ll mul (ll a, ll B) {return (a * B) % F;} ll add (ll a, ll B) {return (a + B) % F;} ll sub (ll a, ll B) {return (a-B + (a-B) /F * F + F) % F;} void upd (ll & a, ll B) {a = (a % F + B % F) % F;} int n; int edge [MAXN], pre [MAXN], next [MAXN], siz = 1; void addedge (int u, int v) {edge [++ siz] = v; next [siz] = pre [u]; pre [u] = siz;} void addedge2 (int u, int v) {addedge (u, v), addedge (v, u) ;}int degree [MAXN] = {0}; bool is_lef [MAXN] = {0}; int legs [MAXN] = {0}; void dfs (int x, int f) {is_lef [x] = 1; Forp (x) {int v = edge [p]; if (v = F) continue; if (degree [v] <= 2) dfs (v, x); // chain} int main () {// freopen (E. in, r, stdin); // freopen (. out, w, stdout); MEM (edge) MEM (pre) MEM (next) cin> n; For (I, n-1) {int u, v; scanf (% d, & u, & v); addedge2 (u, v); degree [u] ++; degree [v] ++ ;} // find the chain For (I, n) {if (degree [I] = 1) dfs (I, 0);} // find y For (I, n) {Forp (I) {int v = edge [p]; if (is_lef [v]) ++ legs [I]; // number of links connected by one vertex} bool flag = 1; For (I, n) {if (is_lef [I]) continue; int cnt = 0; Forp (I) {int v = edge [p]; if (! Is_lef [v] & ° ree [v]-min (legs [v], 2)> = 2) // This neighbor is neither a chain nor Y ++ cnt ;} if (cnt> = 3) {// only flag = 0; break ;}} if (flag) puts (Yes); else puts (No); return 0 ;}
         
        
       
      
     
    
   
  
 


 

 

 

 

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.