在实施dfs的prog中出现不可预知的错误

时间:2014-07-07 14:46:41

标签: c++ graph depth-first-search

实际上我的代码是针对codechef(http://www.codechef.com/problems/FIRESC/)上的FIRESC问题编写的,当我在codeblocks上编译它时效果很好但是当我在网站上提交它时显示错误(错误的答案)..plz help i am发现很难找到错误.. 继承我的代码(http://ideone.com/lHPxyq

#include<bits/stdc++.h>

#define MAXSIZE 10000
#define pb push_back
using namespace::std;

int dfs(vector< vector < int> > &g, int v, bool vs[])
{
    vs[v] = true;
    int count = 1;
    for(auto it = g[v].begin() ; it != g[v].end() ; ++it)//auto it : g[v])
        if(!vs[*it])
        count += dfs(g,*it,vs);
    return count;
}

void firesc(vector <vector <int> > &g, int n, vector <int> &result)
{
    bool vs[n+1];
    vs[0] = true;
    for(int i =1 ; i <= n ; ++i)
        vs[i] = false;
    for(int i = 0 ; i <= n ; ++i)
    if(!vs[i])
        result.pb(dfs(g,i,vs));
}
int main()
{                                                                         //0     1   2   3   4
    int T,N,M,a,b,ways = 1;                                                 //    2   1   2
    vector <int> result;                                                      //      3

    //cout<<"\nenter T :\t";
    cin>>T;
    while(T--)
    {
      //  cout<<"\nenter N and M :\t";
        cin>>N>>M;
        vector <vector <int> > g(N+1);
        while(M--)
        {
        //    cout<<"\nenter couples of friends :\t";
            cin>>a>>b;
            g[a].pb(b);
            g[b].pb(a);
        }
        firesc(g,N,result);
    cout<<result.size()<<' ';                                                                                  //cout<<"\n total routes :\t "<<result.size();
    for(auto it = result.begin() ; it != result.end() ; ++it )          //int x : result
        ways *= (*it);

         cout<<ways%((1000000000+7))<<endl;                                                               //cout<<"\ntotal ways to select captains "<<ways;

    result.clear();
                                                                                /************************* for(auto it : g)
                                                                                                            g[(*it)].clear();*******/

    g.clear();
    ways = 1;
    }

    return 0;
}

0 个答案:

没有答案