UVA 539 The Settlers of Catan dfs无向图最长路
来源:互联网 发布:无网络的手机txt阅读器 编辑:程序博客网 时间:2024/06/08 18:07
点击打开题目链接
给一些点和连接它们的一些边,求最长路径(最多访问边的条数,每个点可以多次访问,每条边只能访问一次)
我们用 vector 可变长数组保存这些边,dfs 最长路径
#include <cstdio> #include <cmath>#include <cstring>#include <iostream> #include <vector>using namespace std;typedef long long LL;const int maxn = 25 + 2;vector<int> Link[maxn];int vis[maxn][maxn];int n, m;int ans;void dfs(int cur, int sum){if (sum > ans) ans = sum;//更新最大值 int cnt = Link[cur].size();//与cur点相连的点的个数 for (int i = 0; i < cnt; i++){int k = Link[cur][i];//与cur相连的点的编号 if (!vis[cur][k])//若这条边还未访问 {vis[cur][k] = vis[k][cur] = 1;//标记 dfs(k, sum + 1);//从下一个点开始dfs vis[cur][k] = vis[k][cur] = 0;//清除标记 }}}int main(){while (~scanf("%d%d", &n, &m)){if (n + m == 0) break;for (int i = 0; i < n; i++) Link[i].clear();int x, y;for (int i = 0; i < m; i++) {scanf("%d%d", &x, &y);Link[x].push_back(y);//保存边 Link[y].push_back(x);}ans = 0;for (int i = 0; i < n; i++)//从每个点dfs出最值 {memset(vis, 0, sizeof(vis));dfs(i, 0);}printf("%d\n", ans);}return 0;}
0 0
- UVA 539 The Settlers of Catan dfs无向图最长路
- UVA 539 The Settlers of Catan dfs找最长链
- UVA 539-The Settlers of Catan(dfs)
- The Settlers of Catan uva dfs+回溯
- UVa - 539 - The Settlers of Catan ( DFS 回溯 )
- UVa 539 The Settlers of Catan
- Uva 539 The Settlers of Catan
- uva 539 - The Settlers of Catan
- uva 539 - The Settlers of Catan
- uva 539 - The Settlers of Catan
- UVa 539 - The Settlers of Catan
- UVA 539 - The Settlers of Catan
- UVa 539 - The Settlers of Catan
- uva 539 The Settlers of Catan
- uva 539 - The Settlers of Catan
- UVA 539 The Settlers of Catan
- uva 539 The Settlers of Catan
- UVA 539 The Settlers of Catan
- 性能测试理论知识
- unity 刚体
- webview一些知识
- 排序算法总结(三)
- 113. Path Sum II
- UVA 539 The Settlers of Catan dfs无向图最长路
- HTTP in iOS你看我就够(转)
- Ubuntu下编写一个简单的c程序
- Treap——题目方法总结
- iOS模型设计---字典转模型
- git 常用命令总结
- 在团队协作中,该如何提交一份干净、可靠的代码给队友?
- java 用 Calendar类写万年历
- “personal.xlsb总是打开”解决办法