UVa10410代码

Posted yifeiwa

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVa10410代码相关的知识,希望对你有一定的参考价值。

这里就不加太多的注释了,想一想就能明白代码的目的

// UVa 10410 
#include <iostream> 
#include <cstdio>
#include <cstring>
#include <vector> 
#include <stack> 
using namespace std;

const int maxn = 1000 + 5;

vector<int> node[maxn];
int pos[maxn]; 

int main() 
  int N, m;
  while (scanf("%d", &N) == 1) 
    for (int i = 1; i <= N; ++i)
      node[i].clear();
    for (int i = 1; i <= N; ++i)  // bfs
      scanf("%d", &m);
      pos[m] = i;    
    
    stack<int> st;
    int b; 
    scanf("%d", &b);
    st.push(b);
    for (int i = 1; i < N; ++i)  // dfs 
      scanf("%d", &b);
      while (1) 
        int a = st.top();
        if (pos[a]+1 < pos[b] || (pos[a]+1 == pos[b] && a > b) || pos[a] == 1) 
          node[a].push_back(b);
          st.push(b);     
          break; 
        
        else 
          st.pop();
      
    
    for (int i = 1; i <= N; ++i) 
      printf("%d:", i); 
      for (int j = 0; j < node[i].size(); ++j)
        printf(" %d", node[i][j]);
      printf("\n"); 
    
  
  return 0;

 

以上是关于UVa10410代码的主要内容,如果未能解决你的问题,请参考以下文章

UVa 10410 树重建

UVA - 10410 Tree Reconstruction(栈处理递归)

UVA - 10410 Tree Reconstruction (根据dfs序和bfs序恢复一颗树)

Paypal Express 结帐:10410 令牌无效

UVa 1593代码对齐

UVa 1442 - Cave