Common BoardWHY WA? Problem 1136 -> Parliament #include <iostream> #include <cstring> using namespace std; struct treetag { int left, right; } tree[60001]; int n,inp[3001]; int done=0; void leftt(int l, int r,int root); void rightt(int l,int r,int root); void print(int root); int main() { int rt; memset(&tree, 0 , sizeof(tree)); cin>> n; for (int i=1;i<=n;i++) cin>> inp[i]; rt=inp[n]; leftt (1,n,rt); rightt(1,n,rt); print(rt); cout<<endl; return 0; } void leftt(int l, int r,int root) { int i=l; if (l>r) return; while (inp[i]<root) i++; i--; if ((inp[i]>=root)||(i<l)) return; tree[root].left=inp[i]; leftt(l,i,inp[i]); rightt(l,i,inp[i]); } void rightt(int l,int r,int root) { int i=r; if (l>r) return; while (inp[i]>=root) i--; i++; if ((inp[i]<=root)||(i>r)) return; tree[root].right=inp[r-1]; leftt(i,r-1,inp[r-1]); rightt(i,r-1,inp[r-1]); } void print(int root) { if ((tree[root].left!=0)||(tree[root].right!=0)) { if (tree[root].right!=0) print(tree[root].right); if (tree[root].left!=0) print(tree[root].left); } if (done!=1) cout<< " "; cout<< root; done=1; } Why don't you send email to "hyz12345678@163.com". > > #include <iostream> > #include <cstring> > using namespace std; > > struct treetag { > int left, right; > } tree[60001]; > > int n,inp[3001]; > int done=0; > > void leftt(int l, int r,int root); > void rightt(int l,int r,int root); > void print(int root); > > int > main() { > int rt; > > memset(&tree, 0 , sizeof(tree)); > cin>> n; > for (int i=1;i<=n;i++) > cin>> inp[i]; > rt=inp[n]; > > leftt (1,n,rt); > rightt(1,n,rt); > > print(rt); > cout<<endl; > return 0; > } > > void leftt(int l, int r,int root) { > int i=l; > if (l>r) return; > while (inp[i]<root) i++; > i--; > if ((inp[i]>=root)||(i<l)) return; > tree[root].left=inp[i]; > leftt(l,i,inp[i]); > rightt(l,i,inp[i]); > > } > > > void rightt(int l,int r,int root) { > int i=r; > if (l>r) return; > while (inp[i]>=root) i--; > i++; > if ((inp[i]<=root)||(i>r)) return; > tree[root].right=inp[r-1]; > leftt(i,r-1,inp[r-1]); > rightt(i,r-1,inp[r-1]); > > } > > void print(int root) { > if ((tree[root].left!=0)||(tree[root].right!=0)) { > if (tree[root].right!=0) print(tree[root].right); > if (tree[root].left!=0) print(tree[root].left); > } > if (done!=1) cout<< " "; > cout<< root; > done=1; > } |