mercredi 1 novembre 2017

Tour - fake tournament spoj

http://ift.tt/1nfzvls

I am solving tour problem on spoj, and according to my understanding I formed a solution but it's wrong. I am doing dfs for every node and checking if all the nodes are visited or not. If all visited then "count++" . The answer is wrong which means my understanding of problem is wrong. I would be grateful if somebody could tell me what is wrong with my solution. I looked for solution online and most of them have used strong connected components. please tell me how SCC will be used in this question. I also checked on some other forums some guy had also done the same thing wrong and asked a simliar question but no reply was there .So,I think its more of a common mistake for this problem that people do. Thanks in advance.

  #include<bits/stdc++.h>

 using namespace std;

 void dfs_(std::vector<int> adj[],int n,std::vector<bool>& visited,int i)
{ 
  visited[i] = true;
  for(vector<int>::iterator it = adj[i].begin();it!=adj[i].end();it++)
  {
      if(!visited[(*it)])
      {
          dfs_(adj,n,visited,(*it));
      }
  }
}



int count_(vector<int > adj[],const int n)
{
  int count = 0;

    for(int i=1;i<=n;i++)
    {
     //visited.clear();
         //cout<<"in i "<<i<<endl;
        vector<bool > visited(n+1,false);
        dfs_(adj,n,visited,i);
        int k;
        for(k=1;k<=n;k++)
        {
        //cout<<"visited["<<k<<"]"<<" "<<visited[k]<<endl;
            if(visited[k] == false)
            {
                //cout<<"visited["<<k<<"]" <<" "<< visited[k]<<endl;
                break;
            } 

        }

        if(k == n+1)
        {
             count++;
        }
    }

    return count;
 }

int main()
 {
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        vector<int> adj[n+1];
        for(int i=1;i<=n;i++)
        {
            int m;
            cin>>m;
            for(int j=1;j<=m;j++)
            {
                int k;
                cin>>k;
                 adj[i].push_back(k);
             }
        }  
        int ans = count_(adj,n);
        cout<<ans<<endl;
    }  

     return 0;
}

for this input:

1

6

0 1 1

1 2

1 1

1 4

1 2

spoj toolkit output: 1

my solution : 0

so I know that my solution is wrong but why I can't get that.

Aucun commentaire:

Enregistrer un commentaire