UVA 10410——Tree Reconstruction
生活随笔
收集整理的這篇文章主要介紹了
UVA 10410——Tree Reconstruction
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題意:給定一顆樹的BFS和DFS,求這棵的每個節點。
思路:用棧模擬維護。對應的BFS為每個節點到根節點的距離,然后比較當前節點和棧頂節點與根的距離,如果當前節點大,則為棧頂節點的孩子,否則彈出繼續比較。
code:
#include <iostream> #include <cstdio> #include <cmath> #include <algorithm> #include <cstring> #include <sstream> #include <string> #include <vector> #include <list> #include <queue> #include <stack> #include <map> #include <set> #include <bitset>using namespace std;typedef long long ll; typedef unsigned long long ull; typedef long double ld;const int INF=0x3fffffff; const int inf=-INF; const int N=1000005; const int M=1005; const int mod=1000000007; const double pi=acos(-1.0);#define cls(x,c) memset(x,c,sizeof(x)) #define fr(i,s,n) for (int i=s;i<=n;i++)int v[M]; vector<int>p[M]; stack<int>s;void sol(int q) {while (1){if (v[q]==2||v[q]>1+v[s.top()]){p[s.top()].push_back(q),s.push(q);break;}else s.pop();} } int main() {int n,x;while (~scanf("%d",&n)&&n){fr(i,1,n) scanf("%d",&x),v[x]=i,p[i].clear();scanf("%d",&x); s.push(x);fr(i,1,n-1) {scanf("%d",&x);sol(x);}fr (i,1,n){printf("%d:",i);for (int j=0;j<p[i].size();j++)printf(" %d",p[i][j]);puts("");}while (!s.empty()) s.pop();} }總結
以上是生活随笔為你收集整理的UVA 10410——Tree Reconstruction的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 成都大熊猫繁育研究基地用不用预约
- 下一篇: 亲亲阿伦河剧情介绍