c_cpp DFS
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp DFS相关的知识,希望对你有一定的参考价值。
#include <iostream>
#include <vector>
using namespace std;
const int MAX_N = 100;
struct Node {
int no, weight;
vector<int> children;
} node[MAX_N];
vector<vector<int>> res;
vector<int> v1;
void dfs(int c, int s) {
for (int i = 0; i < node[c].children.size(); i++) {
v1.push_back(node[c].children[i]);
s -= node[c].weight;
dfs(node[c].children[i], s);
s += node[c].weight;
v1.pop_back();
}
if (!node[c].children.size() && s == node[c].weight) {
res.push_back(v1);
}
}
int main() {
node[0].weight = 10;
node[1].weight = 2;
node[2].weight = 5;
node[3].weight = 8;
node[4].weight = 3;
node[5].weight = 8;
node[0].children.push_back(1);
node[0].children.push_back(2);
node[0].children.push_back(3);
node[2].children.push_back(4);
node[2].children.push_back(5);
// 10(0)
// / | \
// / | \
// / | \
// 2(1) 5(2) 8(3)
// / \
// / \
// / \
// 3(4) 8(5)
dfs(0, 18);
for (auto vec : res) {
cout << 0 << " ";
for (auto val : vec) {
cout << val << " ";
}
cout << endl;
}
// 0 2 4
// 0 3
}
以上是关于c_cpp DFS的主要内容,如果未能解决你的问题,请参考以下文章
c_cpp DFS
c_cpp 图表的DFS
c_cpp DFS
c_cpp 克隆图dfs
c_cpp [dfs] [string]回文分区
c_cpp [graph] [dfs]拓扑排序图