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 1124. Mosaic

WA #12, T_T Give me some tests plz
Posted by Latina_Matrix 20 Nov 2008 06:47
This is my program, i tried to fix it 4 6 hours. But no fault's detected. Help me plz....

-----------------------
#include <stdio.h>
#include <math.h>

#include <iostream>

using namespace std;

#define DEBUG 0
#define MaxM 510

int t;
int m, n;
int cnt;

int ou[ MaxM];
int a[ MaxM][ MaxM];

void readInput()
{
       cin >> m >> n;

    cnt = 0;
    memset( a, 0, sizeof( a));
    memset( ou, 0, sizeof( ou));

    for( int i = 1; i <= m; i ++)
        for( int j = 0; j < n; j ++)
        {
            cin >> t;
            if( t != i)
            {
                a[ i][ t] ++;
                ou[ i] ++;
            }
        }
}

void visit( int index)
{
    if( DEBUG)
    {
        cout << "---> " << index;
    }

    for( int i = 1; i <= m; i ++)
        if( a[ index][ i] != 0 && ou[ i] > 0)
        {
            cnt ++;
            ou[ index] --;
            a[ index][ i] --;

            visit( i);
            return ;
        }

    for( int i = 1; i <= m; i ++)
        if( a[ index][ i] != 0)
        {
            cnt ++;
            ou[ index] --;
            a[ index][ i] --;

            visit( i);
            return ;
        }
}

int main()
{
    if( DEBUG)
    {
        freopen( "mosaic.in", "r", stdin);
        freopen( "mosaic.ou", "w", stdout);
    }

    readInput();

    for( int i = 1; i <= m; i ++)
        while( ou[ i] % 2 != 0)
        {
            cnt ++;
            visit( i);

            if( DEBUG)
            {
                cout << endl;
            }
        }

    for( int i = 1; i <= m; i ++)
        while( ou[ i] != 0)
        {
            cnt ++;
            visit( i);

            if( DEBUG)
            {
                cout << endl;
            }
        }

    if( cnt > 0)
        cout << cnt-1 << endl;
    else
        cout << cnt << endl;

    return 0;
}