牛客 Tree VI ###K //K

题目链接:https://ac.nowcoder.com/acm/contest/9247/B

思路: 用bfs 还原二叉树的结构 然后再用dfs模拟一遍解密的过程即可

 1 class Solution {
 2 public:
 3 
 4     vector<int>E[100010];
 5     #define ll long long 
 6     #define pb push_back
 7     ll ans=0;
 8     vector<int>temp;
 9     int cnt=-1;
10     void dfs(int u,int fa,int c)
11     {
12         int p=temp[++cnt];
13         if(c!=-1) ans+=p^c;
14         for(auto &v:E[u])
15         {
16             dfs(v,u,p);
17         }
18     }
19     long long tree6(int k, vector<int>& a) 
20     {
21         // write code here
22         temp=a;
23         queue<int>q;
24         int tot=0;
25         int n=a.size();
26         q.push(tot);
27         while(!q.empty())
28         {
29             int u=q.front();
30             q.pop();
31             for(int i=1;i<=k;i++)
32             {
33                 tot++;
34                 if(tot==n)
35                     break;
36                 E[u].pb(tot);
37                 q.push(tot);
38             }
39             if(tot==n)
40                 break;
41         }
42         dfs(0,-1,-1);
43         return ans;
44         
45     }
46 };
View Code

 

posted @ 2020-11-27 21:38  canwinfor  阅读(144)  评论(0)    收藏  举报