WA-galactik

ufff…checked all the test cases but getting wrong ans plzz…guys help me out

#include<cstdio>
#include<iostream>
#include<list>



using namespace std;
int dfs(int a);int mini=10000;int p=0;
list<int> M[1000002];
int visited[100005]={0};

int cost[100005]={0};
int main()
{
    int a,b,c,t,n,m,i,j,k,l;
    scanf("%d %d",&n,&m);
    
    for(i=1;i<=m;i++)
    {
        scanf("%d %d",&a,&b);
        M[a].push_back(b);
        M[b].push_back(a);
    }int ans[n];int s=1;
    for(i=1;i<=n;i++)
    {scanf("%d",&cost[i]);} k=1;
    for(i=1;i<=n;i++)
    {
        if(visited[i]==0)
        {
            dfs(i);
            if(p==0)
            {s=0;goto d;}
           
            ans[k]=mini;mini=10000;p=0;s=1;k++;
        }
    }d:mini=10000;int answ=0;int y=0;
    for(i=1;i<k;i++)
    {
    if(mini>ans[i])
    {mini=ans[i];y=i;}
    }
    for(i=1;i<k;i++)
    {if((i!=y)&&(k>2)){answ=answ+ans[i]+mini;}
    else if(k<3)
    {answ=0;}
     }    
    if(s==1)
    printf("%d\n",answ);
    else if(s==0)
    printf("%d\n",-1); 
   return 0;
}

int dfs(int a)
{  
    visited[a]=1;
    if(cost[a]>=0)
    p=1;
    if((mini>cost[a])&&(cost[a]>=0))
    mini=cost[a];
    list<int>::iterator it;
    for(it=M[a].begin();it!=M[a].end();++it)
    {if(visited[*it]==0)dfs(*it);}
}

plzzzzzzz…koi to test cases bato jisme mera code fail ho rha hai…plzzzzzzzzzzzzzzzzzzzzzzzzzzzz

try this case…

4 0
10000
10000
10000
10000

this is giving ans:-

80000

i think the bug is the initial value of ur variable mini…change it to 10001…and you will get the correct ans, i.e.,

60000

hope this helps…:slight_smile:

EDIT:

try this case…

4 6
1 2
2 3
3 4
4 1
1 3
2 4
-1
-1
-1
-1

ans should be 0…it is giving -1!!!

1 Like

nope this is not workng i :frowning:
heres my link to my new code
http://www.codechef.com/viewsolution/2400781

thnx a lot…:slight_smile:

glad could help…:slight_smile:

//