ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1022. Genealogical Tree

WTF??? O (N^3 log^2 N), or, How I can't calculate the time)
Posted by Alibi 20 Dec 2014 14:32
THIS IS ACCEPTED CODE
// *CoDeD by Ye.A
# include <iostream>
# include <cstdlib>
# include <cstdio>
# include <vector>
# include <algorithm>

using namespace std;

const int N = 222;
vector <int> g[N], ans;
int n;
bool used[N];

int main ()
{
    //freopen ("input.txt", "r", stdin);
    //freopen ("output.txt", "w", stdout);
    scanf ("%d", &n);
    for (int i = 1; i <= n; i++)
        while (1)
        {
            int x;
            scanf ("%d", &x);
            if (!x)
                break;
            g[x].push_back (i);
        }

    while (ans.size() != n)
        for (int i = 1; i <= n; i++)
            if (!used[i] && g[i].empty())
            {
                ans.push_back (i);
                used[i] = true;
                for (int j = 1; j <= n; j++)
                    if (binary_search(g[j].begin(), g[j].end(), i))
                        g[j].erase (lower_bound(g[j].begin(), g[j].end(), i));
            }

    for (int i = 0; i < n; i++)
        printf ("%d ", ans[i]);

    return 0;
}