here is my code :
Code: Select all
REMOVED
Moderator: Board moderators
how ?Just think that, you can run the bfs or dfs only once. As I see that you are calling the bfs l times.
Code: Select all
1
7 4 2
1 2
2 3
4 5
5 6
2
4
Code: Select all
int bfs(int i,vector< vector < int > > a)
Code: Select all
int bfs(int i,vector< vector < int > > &a) // Check that the reference is passed
Code: Select all
#include<stdio.h>
#include<string.h>
#include<vector>
#include<list>
using namespace std;
#define _pf printf
#define _sc scanf
#define max 10002
int n,m,l,C;
list<int> L[max];
bool map[max];
bool check[max];
void dfs(int i){
int r;
C++;
map[i]=false;
list<int>::iterator It;
for(It=L[i].begin();It!=L[i].end();It++){
r=(*It);
if(map[r]){
dfs(r);
}
}
}
int main(){
int T,x,y,z,S;
_sc("%d",&T);
while(T--){
memset(map,true,sizeof(map));
memset(check,true,sizeof(check));
S=0;
_sc("%d %d %d",&n,&m,&l);
while(m--){
_sc("%d %d",&x,&y);
if(x>n || y>n) continue;
L[x].push_back(y);
}
while(l--){
_sc("%d",&z);
if(z>n)continue;
if(check[z]){
C=0;
check[z]=false;
dfs(z);
S+=C;
}
}
_pf("%d\n",S);
for(int i=0;i<max;i++)
L[i].clear();
}
return 0;
}
Hi tanmoy...You just made a silly mistake...
Check ur bool map array !!!!!!!!!!!
Here, z's aren't unique.Each of the following l lines contains a single integer z indicating that the domino numbered z is knocked over by hand.