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

Please, help WA5!
Posted by Auromagma 12 Jul 2024 16:31
Here's my wrong solution, can sb view the code and help me?
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

vector<int> pos;
vector<vector<int>> in;
vector<vector<int>> out;
vector<int> ans;

void lets_go(int i)
{
    for (int j = 0; j < in[i].size(); j++)
    {
        if (pos[in[i][j]] == 0)
        {
            lets_go(in[i][j]);
        }
    }
    pos[i] = 1;
    ans.push_back(i);
    for (int j = 0; j < out[i].size(); j++)
    {
        if (pos[out[i][j]] == 0)
        {
            lets_go(out[i][j]);
        }
    }
}

int main()
{
    int n;
    cin >> n;
    in.resize(n);
    out.resize(n);
    pos.resize(n, 0);
    for (int i = 0; i < n; i++)
    {
        int a;
        cin >> a;
        while (a != 0)
        {
            if (a != 0)
            {
                in[a - 1].push_back(i);
                out[i].push_back(a - 1);
                cin >> a;
            }
        }
    }
    for (int i = 0; i < n; i++)
    {
        if (pos[i] == 0)
        {
            lets_go(i);
        }
    }
    for (int i = 0; i < n; i++)
    {
        cout << ans[i] + 1 << " ";
    }
}