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 1077. Travelling Tours

what's wrong with my code? i use the greedy algorithm
Posted by liufeng 1 Apr 2002 13:16
#include<stdio.h>
#include<memory.h>
int map[200][200],mark[200],m,n;
void make(int v){
    int i;
    for (i=0;i<n;i++)
        if (mark[i]==0 && map[v][i]>0) {
            mark[i]=v+1;
            map[v][i]=-map[v][i];
            map[i][v]=-map[i][v];
            make(i);
        }
}
void main(){
    int i,j,aa,bb,la,lb,a[200],b[200],count,k;
    scanf("%d%d",&n,&m);
    memset(map,0,sizeof map);
    for (i=0;i<m;i++) {
        scanf("%d%d",&aa,&bb);
        map[aa-1][bb-1]=map[bb-1][aa-1]=1;
    }
    memset(mark,0,sizeof mark);
    i=0;
    do{
        while (i<n && mark[i]) i++;
        if (i==n) break;
        mark[i]=32767;
        make(i);
    }while (1);
    count=0;
    for (i=0;i<n;i++)
        for (j=i+1;j<n;j++) if (map[i][j]>0) count++;
    printf("%d\n",count);
    for (i=0;i<n;i++)
        for (j=i+1;j<n;j++)
        if (map[i][j]>0) {
            la=0;
            aa=i;
            do{
                a[la++]=aa;
                if (mark[aa]==32767) break;
                aa=mark[aa]-1;
            }while (1);
            lb=0;
            bb=j;
            do{
                b[lb++]=bb;
                if (mark[bb]==32767) break;
                bb=mark[bb]-1;
            }while(1);

            while (a[la-1]==b[lb-1]) {la--;lb--;}
            printf("%d ",la+lb+1);
            for (k=0;k<la+1;k++) printf("%d ",a[k]+1);
            for (k=lb-1;k>=0;k--) printf("%d ",b[k]
+1);
            printf("\n");
        }
}
Use DFS + Greedy Method
Posted by Nazarov Denis (nsc2001@rambler.ru) 1 Apr 2002 14:11
> #include<stdio.h>
> #include<memory.h>
> int map[200][200],mark[200],m,n;
> void make(int v){
>     int i;
>     for (i=0;i<n;i++)
>         if (mark[i]==0 && map[v][i]>0) {
>             mark[i]=v+1;
>             map[v][i]=-map[v][i];
>             map[i][v]=-map[i][v];
>             make(i);
>         }
> }
> void main(){
>     int i,j,aa,bb,la,lb,a[200],b[200],count,k;
>     scanf("%d%d",&n,&m);
>     memset(map,0,sizeof map);
>     for (i=0;i<m;i++) {
>         scanf("%d%d",&aa,&bb);
>         map[aa-1][bb-1]=map[bb-1][aa-1]=1;
>     }
>     memset(mark,0,sizeof mark);
>     i=0;
>     do{
>         while (i<n && mark[i]) i++;
>         if (i==n) break;
>         mark[i]=32767;
>         make(i);
>     }while (1);
>     count=0;
>     for (i=0;i<n;i++)
>         for (j=i+1;j<n;j++) if (map[i][j]>0) count++;
>     printf("%d\n",count);
>     for (i=0;i<n;i++)
>         for (j=i+1;j<n;j++)
>         if (map[i][j]>0) {
>             la=0;
>             aa=i;
>             do{
>                 a[la++]=aa;
>                 if (mark[aa]==32767) break;
>                 aa=mark[aa]-1;
>             }while (1);
>             lb=0;
>             bb=j;
>             do{
>                 b[lb++]=bb;
>                 if (mark[bb]==32767) break;
>                 bb=mark[bb]-1;
>             }while(1);
>
>             while (a[la-1]==b[lb-1]) {la--;lb--;}
>             printf("%d ",la+lb+1);
>             for (k=0;k<la+1;k++) printf("%d ",a[k]+1);
>             for (k=lb-1;k>=0;k--) printf("%d ",b[k]
> +1);
>             printf("\n");
>         }
> }