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 1962. In Chinese Restaurant

Where is bug?
Posted by Felix_Mate 30 Oct 2016 00:02
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

const long long mod=1000000007;
const int nmax=200;

int n,m,t,p;
long long v[nmax],fact[nmax],step[nmax];
bool used[nmax];
bool a[nmax][nmax];
long long ans;

void DFS(int v)
{
       for(int j=1;j<=n;j++)
       {
              if(a[v][j] && !used[j])
              {
                     t++;
                     used[j]=true;
                     DFS(j);
              }
       }
}

bool FindCycle(int v,int pr)
{
     bool f=false;
     for(int j=1;j<=n;j++)
     {
            if(a[v][j] && j!=pr)
            {
                   if(used[j]) return true;
                   else
                   {
                     used[j]=true;
                     t++;
                     f=f | FindCycle(j,v);
                   }
            }
     }
     return f;
}

void main()
{
       scanf("%d%d\n",&n,&m);

        if(n<=2)
        {
               printf("1");
               return;
        }

       fact[0]=1;
       for(int i=1;i<=n;i++)
              fact[i]=fact[i-1]*((long long)i) % mod;

       for(int i=1;i<=n;i++) step[i]=0;

       for(int i=1;i<=n;i++)
       {
              for(int j=1;j<=n;j++)
                     a[i][j]=false;
       }

       for(int i=1;i<=m;i++)
       {
          scanf("%d\n",&t);
          if(!a[i][t])
          {
              a[i][t]=a[t][i]=true;
              step[i]++;
              step[t]++;

          }
       }

       p=1;
       while(p<=n && step[p]<=2) p++;
       if(p<=n)
       {
              printf("0");
              return;
       }

       for(int i=1;i<=n;i++) used[i]=false;

       for(int i=1;i<=n;i++)
       {
              if(!used[i])
              {
                   used[i]=true;
                   t=1;
                   if(FindCycle(i,-1))
                   {
                          if(t<n)
                          {
                            printf("0");
                            return;
                          }
                          if(t==n)
                          {
                            printf("2");
                            return;
                          }
                   }

              }
       }

       for(int i=1;i<=n;i++) used[i]=false;
       p=0;
       ans=1;
       for(int i=1;i<=n;i++)
       {
              if(!used[i])
              {
                     p++;
                     t=1;
                     used[i]=true;
                     DFS(i);
                     if(t>1) t=2;
                     ans*=((long long)t) % mod;
              }
       }
       ans=ans*fact[p-1] % mod;
       while(ans<0) ans+=mod;
       printf("%lld",ans);
}
Re: Where is bug?
Posted by Jane Soboleva (SumNU) 3 Nov 2016 19:38
Test #9 is the first test with N = 100, and it's also the first with M = 100.
I've generated 2k+ random tests with those parameters but got no difference between programs, so i guess it should be a quite specific test.
Anyways, the right answer to that one is 950M ± 10M, and yours is 580M ± 10M.
Re: Where is bug?
Posted by Felix_Mate 5 Nov 2016 00:06
В-общем,я в своём репертуаре. ТОТ ЖЕ ГОВНОКОД, но на ПАСКАЛЕ, дал АС!

P.S.
 Glad to see you, Jane Soboleva!!!
Re: Where is bug?
Posted by Jane Soboleva (SumNU) 5 Nov 2016 00:23
Oh, i guess it was something syntax-specific then, like in a certain place there goes a wrong order of operations or something like that...
Felix_Mate wrote 5 November 2016 00:06
P.S.
 Glad to see you, Jane Soboleva!!!
<3