Problem Type : Graph(DFS)
#include<bits/stdc++.h>
#define MAX 10009
using namespace std;
long node ,edge, call,coun=0,color[MAX];
vector<long >vcc[MAX];
void dfs(long k)
{
color[k]=1;
long i;
for(i=0; i<vcc[k].size(); i++)
{
if(color[vcc[k][i]]==0)
{
dfs(vcc[k][i]);
}
}
}
int main()
{
long test,v,e,i,call1;
scanf("%ld",&test);
while(test--)
{
scanf("%ld %ld %ld",&node,&edge,&call);
for(i=0; i<=MAX; i++)
{
vcc[i].clear();
}
for(i=0; i<edge; i++)
{
scanf("%ld %ld",&v,&e);
vcc[v].push_back(e);
}
memset(color,0,sizeof(color));
for(i=1; i<=call; i++)
{
scanf("%ld",&call1);
dfs(call1);
}
coun=0;
for(i=1; i<=node; i++)
{
if(color[i]==1)
{
coun++;
}
}
printf("%ld\n",coun);
}
}
#include<bits/stdc++.h>
#define MAX 10009
using namespace std;
long node ,edge, call,coun=0,color[MAX];
vector<long >vcc[MAX];
void dfs(long k)
{
color[k]=1;
long i;
for(i=0; i<vcc[k].size(); i++)
{
if(color[vcc[k][i]]==0)
{
dfs(vcc[k][i]);
}
}
}
int main()
{
long test,v,e,i,call1;
scanf("%ld",&test);
while(test--)
{
scanf("%ld %ld %ld",&node,&edge,&call);
for(i=0; i<=MAX; i++)
{
vcc[i].clear();
}
for(i=0; i<edge; i++)
{
scanf("%ld %ld",&v,&e);
vcc[v].push_back(e);
}
memset(color,0,sizeof(color));
for(i=1; i<=call; i++)
{
scanf("%ld",&call1);
dfs(call1);
}
coun=0;
for(i=1; i<=node; i++)
{
if(color[i]==1)
{
coun++;
}
}
printf("%ld\n",coun);
}
}
No comments:
Post a Comment