例子1:图的遍历
#include <iostream>
#include <vector>
using namespace std;
vector<int> G[110];//邻接表用动态数组实现
bool vis[110];
void dfs(int u){//u为某一结点,从u结点开始dfs
vis[u]=true;//u结点被访问过
cout<<u<<endl;
for(int i=0;i<G[u].size();i++){
int v=G[u][i];
if(!vis[v]){//为0(false)是未访问过
dfs(v);
}
}
}
int main() {
int n, m;
cin >> n >> m;
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
G[u].push_back(v);
G[v].push_back(u);
}
dfs(1);
return 0;
}
例子2:求解连通块的数量
#include <iostream>
#include <vector>
using namespace std;
vector<int> G[110];
bool vis[110];
void dfs(int u) {
vis[u] = true;
for (int i = 0; i < G[u].size(); i++) {
int v = G[u][i];
if (!vis[v]) {
dfs(v);
}
}
}
int main() {
int n, m;
cin >> n >> m;
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
G[u].push_back(v);
G[v].push_back(u);
}
int cnt=0;
for(int i=1;i<=n;i++){
if(!vis[i]){//边判断有无访问过,边进行搜索
cnt++;
dfs(i);
}
}
cout<<cnt<<endl;
return 0;
}
例子3:树上节点深度
#include <iostream>
#include <vector>
using namespace std;
vector<int> G[110];
int d[110];
void dfs(int u,int fa){
if(u!=1){
d[u]=d[fa]+1;
}
for(int i=0;i<G[u].size();i++){
int v=G[u][i];
if(v!=fa){
dfs(v,u);
}
}
}
int main() {
int n;
cin >> n;
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
G[u].push_back(v);
G[v].push_back(u);
}
dfs(1,0);
for(int i=1;i<=n;i++){
cout<<i<<" "<<d[i]<<endl;
}
return 0;
}
例子4:求解子树的大小
#include <iostream>
#include <vector>
using namespace std;
vector<int> G[110];
int sz[110];
int dfs(int u, int fa) {
for (int i = 0; i < G[u].size(); i++) {
int v = G[u][i];
if (v != fa) {
sz[u]+=dfs(v,u);
}
}
sz[u]++;
return sz[u];
}
int main() {
int n;
cin >> n;
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
G[u].push_back(v);
G[v].push_back(u);
}
dfs(1, 0);
for (int i = 1; i <= n; i++) {
cout << i << " " << sz[i] << endl;
}
return 0;
}
例子5:最大连通块
思路:
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
const int N = 20005;
int n, m, cnt, ans;
bool vis[N];
vector<int> G[N];
void dfs(int u) { // 请完善 dfs 函数
/* vis[u] = true;
int sum=0;
for (int i = 0; i < G[u].size(); i++) {
int v = G[u][i];
if (!vis[v]) {
dfs(v);
sum++;
}
}
cnt=sum+1;*/
cnt++;
vis[u] = true;
for (int i = 0; i < G[u].size(); i++) {
int v = G[u][i];
if (!vis[v]) {
dfs(v);
}
}
}
int main() {
freopen("block.in", "r", stdin);
freopen("block.out", "w", stdout);
cin >> n >> m;
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
G[u].push_back(v);
G[v].push_back(u);
}
for (int i = 1; i <= n; i++) {
if (!vis[i]) {
cnt = 0;
dfs(i);
ans = max(ans, cnt);
}
}
cout << ans << endl;
return 0;
}
例子6:蒜头君的旅行
思路:
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
const int N = 20005;
int n, m, cnt, ans;
bool vis[N];
struct node {
int v;
int w;
};
vector<node> G[N];
void insert(int u, int v, int w) {
node temp;
temp.v = v;
temp.w = w;
G[u].push_back(temp);
}
void dfs(int u) { // 请完善 dfs 函数
/* vis[u] = true;
cout << u << " ";*/
vis[u] = true;
cout << u << " ";
int minw = 1000000, minv = 1000000, x;
for (int i = 0; i < G[u].size(); i++) {
if (G[u][i].w < minw) {
minw = G[u][i].w;
minv = G[u][i].v;
x = G[u][i].v;
}
else if (G[u][i].v < minv && G[u][i].w == minw) {
minv = G[u][i].v;
x = G[u][i].v;
}
}
if (!vis[x]) {
dfs(x);
}
}
int main() {
freopen("travel.in", "r", stdin);
freopen("travel.out", "w", stdout);
cin >> n >> m;
for (int i = 0; i < m; i++) {
int u, v, w;
cin >> u >> v >> w;
insert(u, v, w);
insert(v, u, w);
}
dfs(1);
return 0;
}