zoukankan      html  css  js  c++  java
  • 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("
    "); 
        }
      }
      return 0;
    }
  • 相关阅读:
    75. Sort Colors
    101. Symmetric Tree
    121. Best Time to Buy and Sell Stock
    136. Single Number
    104. Maximum Depth of Binary Tree
    70. Climbing Stairs
    64. Minimum Path Sum
    62. Unique Paths
    css知识点3
    css知识点2
  • 原文地址:https://www.cnblogs.com/yifeiWa/p/10975364.html
Copyright © 2011-2022 走看看