| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
邻接表 + DFS,分享一份比较简洁的代码#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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator