E. Bear and Drawingtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output
Limak is a little bear who learns to draw. People usually start with houses, fences and flowers-why would bears do it? Limak lives in the forest and he decides to draw a tree.
Rec All That tree is a connected graph Consisting Of n vertices and n ?-? 1 edges.
Limak chose a tree with n vertices. He has infinite strip of paper with and parallel rows of dots. Little Bear wants to assign vertices of a tree to some n distinct dots in a paper so that edges would intersect O Nly at their endpoints-drawn tree must is planar. Below you can see one of correct drawings for the first sample test.
Is it possible-Limak to draw chosen tree?
Input
The first line contains a single integer n (1?≤? N? ≤?105).
Next n?-? 1Lines contain description of a tree.I-th of them contains, space-separated integers ai and bi (1?≤? a i,? b i? ≤? n,? a i? ≠? b I ) denoting an edge between vertices ai and bi . 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
The problem is to construct a solution
The solution to the construction is this
First we put the leaf node and the linked chain tag Is_lef
Then count the number of chains that each point counts (one large subtree does not count)
Then, in addition to the chain and the Y-shape, the point divides up to 2 large subtrees, otherwise no solution
#include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> #include < functional> #include <iostream> #include <cmath> #include <cctype> #include <ctime>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<n;i++) #define ForD (I,n) for (int. i=n;i;i--) #define REPD (I,n) for (int. i=n;i>=0;i--) #define FORP (x) for ( int p=pre[x];p; p=next[p]) #define FORPITER (x) for (int &p=iter[x];p; 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 (a)), #define MEMI (a) memset ( A,128,sizeof (a)); #define INF (2139062143) #define F (100000007) #define MAXN (200000+10) typedef long 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)/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;voidAddedge (int u,int v) {edge[++siz]=v;next[siz]=pre[u];p re[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%d", &u,&v), Addedge2 (u,v);d egree[u]++;d egree[v]++;} Find chain for (i,n) {if (degree[i]==1) DFS (i,0);} Find Y-type for (i,n) {Forp (i) {int v=edge[p];if (IS_LEF[V]) ++legs[i];//The number of links to a point}}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 not a chain nor y ++cnt;} if (cnt>=3) {//only to both sides F Lag=0;break;}} if (flag) puts ("Yes"), Else puts ("No"); return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
CF 574E (Bear and Drawing-2*n dot-matrix drawing tree)