您的位置:首页 > 其它

uva 796(求割边)

2014-01-16 19:17 337 查看
题意:在一张图中,让你求割边。并按照顺序数出来,注意图并不是连通的。

思路:对多个连通分支,每个执行一次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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: