Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

邻接表 + DFS,分享一份比较简洁的代码

Posted by 10185101210 at 2020-05-12 16:07:25 on Problem 2230
#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>

#define MAXL 100005
using namespace std;

struct edge {
	int to;
	bool visited;
};

vector<edge> G[MAXL];
int N, M;
int num = 0;

void dfs(int s) {

	for (int i = 0; i < G[s].size(); i++) {
		if(!G[s][i].visited){
			G[s][i].visited = 1;
			dfs(G[s][i].to);
		}
	}
	printf("%d\n", s);
}

int main() {
	cin >> N >> M;
	while (M--) {
		int a, b;
		cin >> a >> b;
		edge e;
		e.to = b;
		e.visited = 0;
		G[a].push_back(e);
		e.to = a;
		G[b].push_back(e);
	}
	dfs(1);
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator