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 1136. Parliament

Why Crash (access violation) #1
Posted by dima11221122 23 Nov 2010 20:20
#pragma comment(linker, "/STACK:16777216")
#include <iostream>
#include <vector>
#include <string>

using namespace std;
int tree[65536][2];
int a[3000], n;
vector<int> b;
string flag[3000];
void insert_tree(int zveno, int znach)
{
    if(zveno>znach)
    {
    if(tree[zveno][0]!=-1)
    {
        zveno=tree[zveno][0];
        insert_tree(zveno, znach);
    }
    else
        tree[zveno][0]=znach;
    }
    else if(zveno<znach)
    {
    if(tree[zveno][1]!=-1)
    {
        zveno=tree[zveno][1];
        insert_tree(zveno, znach);
    }
    else
        tree[zveno][1]=znach;
    }
}

void topological_sort(int k)
{
    flag[k]="GRAY";
    for(int i=1; i>=0; i--)
    {
    int ind=tree[k][i];
    if(flag[ind]=="WHITE")
        topological_sort(ind);
    }
    flag[k]="BLACK";
    b.push_back(k);
}
int main()
{
    cin>>n;
    for(int i=0; i<n; i++)
    {
        cin>>a[i];
        tree[a[i]-1][0]=-1;
        tree[a[i]-1][1]=-1;
        flag[a[i]-1]="WHITE";
    }
    int zveno=a[n-1]-1;
    for(int i=n-2; i>=0; i--)
    insert_tree(zveno, a[i]-1);
    topological_sort(zveno);
    for(int i=0; i<n; i++)
    cout<<b[i]+1<<" ";
    return 0;
}