codebackup/Data Structure/实验6-图/B - 图的基本存储的基本方式二.cp
2021-05-08 09:21:42 +08:00

69 lines
1.4 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
Description
请定一个无向图顶点编号从0到n-1用深度优先搜索(DFS),遍历并输出。遍历时,先遍历节点编号小的。
Input
输入第一行为整数n0 < n < 100表示数据的组数。 对于每组数据第一行是两个整数k,m0 k 1000 m k*k表示有m条边k个顶点。 下面的m行每行是空格隔开的两个整数uv表示一条连接uv顶点的无向边。
Output
输出有n行对应n组输出每行为用空格隔开的k个整数对应一组数据表示DFS的遍历结果。
Sample
Input
1
4 4
0 1
0 2
0 3
2 3
Output
0 1 2 3
*/
#include<iostream>
#include<vector>
#include<cstring>
using namespace std;
vector<int> a[500005];
int main()
{
int n,m;
int u,v;
int q;
int len,flag;
while(scanf("%d %d",&n,&m)!=EOF)
{
memset(a,0,sizeof (a));
while(m--)
{
scanf("%d %d",&u,&v);
a[u].push_back(v);
}
scanf("%d",&q);
while(q--)
{
flag=0;
scanf("%d %d",&u,&v);
len=a[u].size();
for(int i=0;i<len;i++)
{
if(a[u][i]==v)
{
flag=1;
break;
}
}
if(flag==1)
printf("Yes\n");
else
printf("No\n");
}
}
return 0;
}