#include <iostream> #include <vector> #include <queue> using namespace std; int getRoot(string& str) {     int re = 0;     int i = 0;     while(str[i] != ':')     {         re = re * 10 + (str[i]-'0');         i++;     }     return re; } int getLeft(string& str) {     int re = 0;     int i = 0;     while(str[i] != ':')     {         i++;     }     i++;     bool sign = true;     if(str[i] == '-')     {         sign = false;         i++;     }     while(str[i] != '|')     {         re = re * 10 + (str[i]-'0');         i++;     }     if(sign == false) re = (-1)*re;     return re; } int getRight(string& str) {     int re = 0;     int i = 0;     while(str[i] != '|')     {         i++;     }     i++;     bool sign = true;     if(str[i] == '-')     {         sign = false;         i++;     }     while(str[i] != '\0')     {         re = re * 10 + (str[i]-'0');         i++;     }     if(sign == false) re = (-1)*re;     return re; } void middle(int root, vector< vector<int> >& child, vector<int>& re) {      if(root == -1) return;      middle(child[root][0], child, re);      re.push_back(root);      middle(child[root][1], child, re); } int main() {     vector<int> parent(1023,-1);     vector< vector<int> > child(1023, vector<int>(2,-1));     vector<int> tree;     int ROOT;     cin>>ROOT;     tree.push_back(ROOT);     queue<int> que;     que.push(ROOT);     string str;     int left, right;     int root;     while(que.size()!=0)     {         cin>>str;         root = getRoot(str);         left = getLeft(str);         right = getRight(str);         if(left != -1)         {             parent[left] = root;             que.push(left);             tree.push_back(left);         }         if(right != -1)         {             parent[right] = root;             que.push(right);             tree.push_back(right);         }         child[root][0] =  left;         child[root][1] =  right;         que.pop();     }     vector<int> rere;     middle(ROOT, child, rere);     for(unsigned long i = 1; i < rere.size(); i++)     {         if(rere[i] < rere[i-1])         {             cout<<0<<endl;             return 0;         }     }     cout<<1<<endl;     return 0; }
点赞 评论
牛客网
牛客网在线编程
牛客网题解
牛客企业服务