这里就不加太多的注释了,想一想就能明白代码的目的
// 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; }
原文地址:https://www.cnblogs.com/yifeiWa/p/10975364.html
时间: 2024-11-02 17:47:00