uva 796(求割边)
2014-01-16 19:17
337 查看
题意:在一张图中,让你求割边。并按照顺序数出来,注意图并不是连通的。
思路:对多个连通分支,每个执行一次dfs求割边。最后排序输出就好了。
代码如下:
View Code
思路:对多个连通分支,每个执行一次dfs求割边。最后排序输出就好了。
代码如下:
#include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <cstring> #include <algorithm> #include <queue> #include <stack> #include <vector> #define MP(a, b) make_pair(a, b) #define PB(a) push_back(a) using namespace std; typedef long long ll; typedef pair<int ,int> pii; typedef pair<unsigned int, unsigned int> puu; typedef pair<int ,double> pid; typedef pair<ll, int> pli; const int INF = 0x3f3f3f3f; const double eps = 1e-6; const int LEN = 1010; struct Arc{ int from, to; }bri[LEN]; int mp[LEN][LEN], n, nbri, low[LEN], dfn[LEN], dfs_clock, vis[LEN]; vector<int> Map[LEN]; inline bool cmp(Arc a, Arc b) { if(a.from != b.from) return a.from<b.from; else return a.to<b.to; } //割边模版 //初始化 //for(int i=0; i<LEN; i++)Map[i].clear(); //memset(vis, 0, sizeof vis); void dfs(int u, int fa) { int v, i, son = 0; vis[u] = 1; dfn[u] = low[u] = dfs_clock++; for(int i = 0; i < Map[u].size(); i++){ v = Map[u][i]; if(vis[v] == 1 && v != fa)low[u] = min(low[u], low[v]); if(vis[v] == 0){ dfs(v, u); son++; low[u] = min(low[u], low[v]); if(low[v] > dfn[u])bri[nbri].from = min(u, v), bri[nbri++].to = max(v, u); } } } int main() { // freopen("in.txt", "r", stdin); int from, to, tn; while(scanf("%d", &n)!=EOF) { memset(mp, 0 ,sizeof mp); for(int i=0; i<LEN; i++)Map[i].clear(); memset(vis, 0, sizeof vis); dfs_clock = 0, nbri = 0; for(int i=0; i<n; i++){ scanf("%d (%d)", &from, &tn); for(int j=0; j<tn; j++){ scanf("%d", &to); if(!mp[from][to]){ mp[from][to] = mp[to][from] = 1; Map[from].PB(to); Map[to].PB(from); } } } for(int i=0; i<n; i++){ if(vis[i]==0){ dfs(i, -1); } } sort(bri, bri+nbri, cmp); printf("%d critical links\n", nbri); for(int i=0; i<nbri; i++) printf("%d - %d\n", bri[i].from, bri[i].to); printf("\n"); } return 0; }
View Code
相关文章推荐
- CodeSmith 使用教程(3): 自动生成Yii Framework ActiveRecord
- HDU - 1231 - 最大连续子序列
- const引用测试小程序
- 我叫MT九游17区邀请ID,444331909
- CodeSmith 使用教程(2): 编写第一个代码模板
- 土法炼钢_百度百科
- MongoDB的学习--文档的插入、删除和更新
- 东芝L60061b 主板型号
- C语言中的加加减减
- CodeSmith 使用教程(1): 概述
- 【CSS】之引入方式
- 分页查询总结
- 向优秀的效率类应用学习--智能日历应用(Cal和Sunrise)
- J2EE开发教程集汇/J2EE系列开发经典书籍/从入门到精通/权威指南
- [Leetcode] Gray Code (Java)
- Java正则表达式中的十大问题
- “借鸡生蛋”九大技巧
- 配置ASA实现内网、DMZ和外网的访问
- J2EE系列开发帮助文档/电子书籍/史上最全API集汇/开发必备工具
- 用OpenSceneGraph实现的NeHe OpenGL教程 - 第一课