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 1078. Segments

It works on sample test but get WA 1. WHY??
Posted by Гладких Максим 4 Sep 2005 01:30
This program only makes an oriented graph and dfs it.

#include <stdio.h>
#include <vector>
using namespace std;

vector <int> v[500];
int d_[500],cm,cmp,totmax=0;
char c[500],c_[500];
short l[500],r[500],path[500];


void dfs(int ve,int d)
{
    c[ve]=1;
    c_[ve]=1;
    if(d>cm)
    {
        cm=d;
        cmp=ve;
    }
    int i;
    for(i=0;i<v[ve].size();i++)
    {
        if(!c[v[ve][i]])
        {
            d_[v[ve][i]]=ve+1;
            dfs(v[ve][i],d+1);
        }
    }
}
void swap()
{
    int p=cmp;
    for(int i=0;;i++,p=d_[p]-1)
    {
        path[i]=p+1;
        if(!d_[p])
            break;
    }
}

int main()
{
//    freopen("in","r",stdin);
    int n,i,j,tm1,tm2;
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%d%d",&tm1,&tm2);
        l[i]=tm1;
        r[i]=tm2;
        for(j=0;j<i;j++)
        {
            if(l[j]>l[i]&&r[j]<r[i])
                v[i].push_back(j);
            else if(l[j]<l[i]&&r[j]>r[i])
                v[j].push_back(i);
        }
    }
/*    for(i=0;i<n;i++)
    {
        for(j=0;j<v[i].size();j++)
            printf("%d ",v[i][j]);
        printf("\n");
    }*/
    for(i=0;i<n;i++)
        c_[i]=0;
    for(i=0;i<n;i++)
    {
        if(c_[i])
            continue;
        for(j=0;j<n;j++)
        {
            d_[j]=-1;
            c[j]=0;
        }
        d_[i]=0;
        cm=0;
        dfs(i,1);
        /*for(j=0;j<n;j++)
            printf("%d ",path[j]);
        printf("\n");*/
        if(cm>totmax)
        {
            totmax=cm;
            swap();
        }
    }
    printf("%d",totmax);
    for(i=0;i<totmax;i++)
        printf(" %d",path[i]);
    return 0;
}