1. 程式人生 > >鄰接表存圖(鏈式前向星或vector)

鄰接表存圖(鏈式前向星或vector)

#include<bits/stdc++.h>
#define maxn 100005
using namespace std;

// 鏈式前向星 常數優秀,使用結構體可獲得更優秀的常數
int info[maxn],to[maxn<<1],Prev[maxn<<1],cnt_e;
inline void Node(int u,int v){ Prev[++cnt_e]=info[u],info[u]=cnt_e,to[cnt_e]=v; }

void dfs1(int now,int ff)
{
	for(int i=info[now];i;i=Prev[i])
		if(to[i]!=ff) dfs(to[i] , now);
}

//vector存鄰接點,可以進行一些操作如按照子樹大小對兒子排序以起到啟發式合併的效果。
vector<int>G[maxn],cst[maxn];
inline void node(int u,int v,int ct)
{
	G[u].push_back(v),cst[u].push_back(ct);
	G[v].push_back(u),cst[v].push_back(ct);
}
void dfs2(int now,int ff)
{
	for(int i=0,siz=G[now].size();i<siz;i++)
		if(G[now][i]!=ff) dfs2(G[now][i] , now);
}

int main()
{
	
}