Codeforces 1159E (topological order, thinking)

Points

  • Sequence means the relationship between the positions of edges connected into common problem of FIG.
  • After some exploration is not difficult to find, for example when there are two sides to cross when there is illegal.
  • -1 vague, that is, how much can fill that in order to avoid cross our greedy that even it it behind the line.
  • I see a very brief dfs practice. From right to left to explore in the end, back when not, until then explore feasible.
const int maxn = 5e5 + 5;
int T, n, a[maxn], ans[maxn], C;

int dfs(int now) {
    int i = now;
    while (i > 1 && (a[i - 1] == now || a[i - 1] == -1))
        i = dfs(i - 1);
    ans[now] = ++C;
    return i;
}

int main() {
    for (read(T); T--; C = 0) {
        read(n);
        rep(i, 1, n)    read(a[i]);
        if (dfs(n + 1) > 1)
            puts("-1");
        else {
            rep(i, 1, n)
                printf("%d%c", ans[i], " \n"[i == n]);
        }
    }   
    return 0;
}

Guess you like

Origin www.cnblogs.com/AlphaWA/p/10949425.html