题目链接: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1832
题意: 中文题诶~
思路: 若二叉树中有 k 个节点只有一个子树, 则答案为 1 << k.
详情参见:http://blog.csdn.net/gyhguoge01234/article/details/77836484
代码:
1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 #define ll long long 5 using namespace std; 6 7 const int MAX = 1e2; 8 const int M = 1e9;//1e9为1节 9 const int MAXN = 35; 10 11 struct BigInt{ 12 const static int mod = 10000; 13 const static int DLEN = 4; 14 int a[600], len; 15 BigInt(){ 16 memset(a, 0, sizeof(a)); 17 len = 1; 18 } 19 BigInt(int v){ 20 memset(a, 0, sizeof(a)); 21 len = 0; 22 do{ 23 a[len++] = v % mod; 24 v /= mod; 25 }while(v); 26 } 27 BigInt(const char s[]){ 28 memset(a, 0, sizeof(a)); 29 int L = strlen(s); 30 len = L / DLEN; 31 if(L % DLEN) len++; 32 int index = 0; 33 for(int i = L - 1; i >= 0; i -= DLEN){ 34 int t = 0; 35 int k = i - DLEN + 1; 36 if(k < 0) k = 0; 37 for(int j = k; j <= i; j++) 38 t = t * 10 + s[j] - '0'; 39 a[index++] = t; 40 } 41 } 42 BigInt operator +(const BigInt &b)const{ 43 BigInt res; 44 res.len = max(len, b.len); 45 for(int i = 0; i <= res.len; i++) res.a[i] = 0; 46 for(int i = 0; i < res.len; i++){ 47 res.a[i] += ((i < len) ? a[i] : 0) + ((i < b.len) ? b.a[i] : 0); 48 res.a[i + 1] += res.a[i] / mod; 49 res.a[i] %= mod; 50 } 51 if(res.a[res.len] > 0) res.len++; 52 return res; 53 } 54 BigInt operator *(const BigInt &b)const{ 55 BigInt res; 56 for(int i = 0; i < len; i++){ 57 int up = 0; 58 for(int j = 0; j < b.len; j++){ 59 int temp = a[i] * b.a[j] + res.a[ i + j] + up; 60 res.a[i + j] = temp%mod; 61 up = temp / mod; 62 } 63 if(up != 0) 64 res.a[i + b.len] = up; 65 } 66 res.len = len + b.len; 67 while(res.a[res.len - 1] == 0 && res.len > 1) res.len--; 68 return res; 69 } 70 void output(){ 71 printf("%d", a[len - 1]); 72 for(int i = len - 2; i >= 0; i--) 73 printf("%04d", a[i]); 74 printf("\n"); 75 } 76 }; 77 78 // 先序遍历 X L … R … 79 // 后序遍历 … L … R X 80 81 const int N = 1e5 + 10; 82 int a[N], b[N]; 83 BigInt sol(1); 84 85 void dfs(int al, int ar, int bl, int br){ 86 if(ar - al <= 1) return; 87 al++; 88 br--; 89 int indx = bl, cnt = 0;; 90 while(a[al] != b[indx]) indx++; 91 int newar = al + (indx - bl + 1); 92 int newbr = indx + 1; 93 cnt++; 94 dfs(al, newar, bl, newbr); 95 if(ar - al != indx - bl + 1){ 96 cnt++; 97 dfs(newar, ar, newbr, br); 98 } 99 if(cnt == 1) sol = sol * 2; 100 } 101 102 int main(void){ 103 int n; 104 scanf("%d", &n); 105 for(int i = 0; i < n; i++){ 106 scanf("%d", &a[i]); 107 } 108 for(int i = 0; i < n; i++){ 109 scanf("%d", &b[i]); 110 } 111 sol = 1; 112 dfs(0, n, 0, n); 113 sol.output(); 114 return 0; 115 }