边和图的存储(邻接表)

// 边和图的存储(邻接表)
#include
#include
using namespace std;
const int N=100010,M=N*2;
// h 存的 n 个链表的链表头
int h[N],e[N],ne[N],idx;
bool st[N];

void add(int a,int b)
{
	e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}

void dfs(int u)
{
	st[u]=true;
	for(int i=h[u];i!=-1;i=ne[i])
	{
		int j=e[i];
		if(!st[j]) dfs(j);
	}
}

int main()
{
	memset(h,-1,sizeof h);
	dfs(1);
	return 0;
}

你可能感兴趣的:(深度优先,图论,算法)