HDU 3887 Counting Offspring 樹狀數組 + 棧類比dfs

來源:互聯網
上載者:User

來源:http://acm.hdu.edu.cn/showproblem.php?pid=3887

題意:給一棵樹,樹的根結點給出,邊也給出,現在問每個結點下面有多少個結點的編號比該結點的編號小。

思路:這道題就是POJ 3321 和 HDU 4417的結合。首先用dfs遍曆樹,對每個結點對應一個區間,然後就是求一個區間內比一個數小的數有多少個,和HDU  4417 一樣。不同的是這道題目dfs會爆棧,因此需要和棧類比dfs,或者調棧的大小。

棧類比dfs代碼:

#include <iostream>#include <cstdio>#include <string.h>#include <algorithm>#include <stdio.h>#include <stack>#include <vector>using namespace std;const int N = 100010;struct ask{int lp,rp,value;}aa[N];struct dit{int id,num;}dd[N];int n,root,cnt[N],timeorder = 0;bool vis[N];vector<int> vv[N];//void dfs(int x){//aa[x].lp = timeorder;//aa[x].value = x;//vis[x] = true;//for(int i = 0; i < vv[x].size(); ++i){//    int y = vv[x][i];//if(!vis[y])//   dfs(y);//vis[y] = true;//}//aa[x].rp = timeorder++;//}void dfs(int x){stack<int> ss;ss.push(x);while(!ss.empty()){   int tt = ss.top();   if(!vis[tt]){     vis[tt] = true; aa[tt].lp = timeorder; aa[tt].value = tt;   }   bool flag = false;   for(int i = 0; i < vv[tt].size(); ++i){   int y = vv[tt][i];   if(!vis[y]){   ss.push(y);   flag = true;   break;   }   }   if(flag) continue;   if(vis[tt]){     aa[tt].rp = timeorder++; ss.pop();   }}}bool cmp1(ask a,ask b){return a.rp < b.rp;}bool cmp2(ask a,ask b){return a.value < b.value;}int inline lowbit(int x){return x & (-x);}int inline sum(int x){int s = 0;while(x > 0){   s += cnt[x];   x -= lowbit(x);}return s;}void inline update(int x){while(x < N){//printf("ss\n");   cnt[x]++;   x += lowbit(x);}}int main(){//freopen("1.txt","r",stdin);while(scanf("%d%d",&n,&root) && (n + root)){   memset(cnt,0,sizeof(cnt));   memset(vv,0,sizeof(vv));   memset(vis,0,sizeof(vis));    for(int i = 0; i < N; ++i){      dd[i].id = dd[i].num = -1;  aa[i].lp = aa[i].rp = aa[i].value = -1;   }   int x,y;   for(int i = 1; i < n; ++i){      scanf("%d%d",&x,&y);  vv[x].push_back(y);  vv[y].push_back(x);   }   timeorder = 1;   dfs(root);   sort(aa+1,aa+n+1,cmp1);   for(int i = 1; i <= n; ++i){   int x = aa[i].value;      dd[x].id = i;  dd[i].num = aa[i].value;   }   sort(aa+1,aa+n+1,cmp2);   int ans[N] = {0};   for(int i = 1; i <= n; ++i){      ans[i] = sum(aa[i].rp) - sum(aa[i].lp - 1);  update(dd[i].id);   }   for(int i = 1; i < n; ++i)   printf("%d ",ans[i]);   printf("%d\n",ans[n]);}return 0;}

調棧大小的代碼:

#pragma comment(linker,"/STACK:100000000,100000000")#include <iostream>#include <cstdio>#include <string.h>#include <algorithm>#include <stdio.h>#include <stack>#include <vector>using namespace std;const int N = 100010;struct ask{int lp,rp,value;}aa[N];struct dit{int id,num;}dd[N];int n,root,cnt[N],timeorder = 0;bool vis[N];vector<int> vv[N];void dfs(int x){aa[x].lp = timeorder;aa[x].value = x;vis[x] = true;for(int i = 0; i < vv[x].size(); ++i){    int y = vv[x][i];if(!vis[y])   dfs(y);vis[y] = true;}aa[x].rp = timeorder++;}bool cmp1(ask a,ask b){return a.rp < b.rp;}bool cmp2(ask a,ask b){return a.value < b.value;}int inline lowbit(int x){return x & (-x);}int inline sum(int x){int s = 0;while(x > 0){   s += cnt[x];   x -= lowbit(x);}return s;}void inline update(int x){while(x < N){//printf("ss\n");   cnt[x]++;   x += lowbit(x);}}int main(){//freopen("1.txt","r",stdin);while(scanf("%d%d",&n,&root) && (n + root)){   memset(cnt,0,sizeof(cnt));   memset(vv,0,sizeof(vv));   memset(vis,0,sizeof(vis));    for(int i = 0; i < N; ++i){      dd[i].id = dd[i].num = -1;  aa[i].lp = aa[i].rp = aa[i].value = -1;   }   int x,y;   for(int i = 1; i < n; ++i){      scanf("%d%d",&x,&y);  vv[x].push_back(y);  vv[y].push_back(x);   }   timeorder = 1;   dfs(root);   sort(aa+1,aa+n+1,cmp1);   for(int i = 1; i <= n; ++i){   int x = aa[i].value;      dd[x].id = i;  dd[i].num = aa[i].value;   }   sort(aa+1,aa+n+1,cmp2);   int ans[N] = {0};   for(int i = 1; i <= n; ++i){      ans[i] = sum(aa[i].rp) - sum(aa[i].lp - 1);  update(dd[i].id);   }   for(int i = 1; i < n; ++i)   printf("%d ",ans[i]);   printf("%d\n",ans[n]);}return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.