题意:给你一个无向图(可能不连通,但是无自环,无重边),如果本图不连通,那么直接输出0。否则要你输出图中的每条桥边,要求按输入边的顺序输出。
思路:由于要按输入边的顺序输出,所以只能等DFS之后再输出。
对于边(u,v)来说,只要low[u]>pre[v]或者low[v]>pre[u],那么就是桥
Trick:由于有字符串输入,我用的map+string映射,超时了几次...后来把v!=fa这个判断提前了之后995MS险过...
/* 995MS..用的string+map*/
#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <set>
#include <ctime>
#include <cmath>
#include <cctype>
using namespace std;
#define maxn 10005
#define maxm 100000+10
#define LL long long
int cas=1,T;
int n,m;
int sum = 0;
int dfs_clock; //时钟,每访问一个结点增1
vector<int>G[maxn]; //图
int pre[maxn]; //pre[i]表示i结点被第一次访问到的时间戳,若pre[i]==0表示还未被访问
int low[maxn]; //low[i]表示i结点及其后代能通过反向边连回的最早的祖先的pre值
//bool iscut[maxn]; //标记i结点是不是一个割点
//int cut[maxn]; //切割这个结点后的儿子数
//求出以u为根节点(u在DFS树中的父节点是fa)的树的所有割点和桥
//初始调用dfs(root,-1)
int dfs(int u,int fa)
{
int lowu=pre[u]=++dfs_clock;
int child = 0; //子结点数目
for (int i = 0;i<G[u].size();i++)
{
int v = G[u][i];
if (v==fa)
continue;
if (!pre[v])
{
child++; //未访问过的结点才能算是u的孩子
int lowv = dfs(v,u);
lowu = min(lowu,lowv);
/*if (lowv >=pre[u])
{
iscut[u]=1; //u是割点
cut[u]++;
if (lowv > pre[u]) //(u,v)边时桥
// printf("qiao")
}*/
}
else/* if (pre[v] <pre[u] && v!=fa) */ //v!=fa确保了(u,v)是从u到v的反向边
{
lowu = min(lowu,pre[v]);
}
}
return low[u]=lowu;
}
struct Edge
{
string u;
string v;
bool flag;
}e[maxm];
void init()
{
dfs_clock = 0;
sum=0;
memset(pre,0,sizeof(pre));
// memset(iscut,0,sizeof(iscut));
// memset(cut,0,sizeof(cut));
for (int i = 0;i<=n;i++)
G[i].clear();
}
map<string,int> mapp;
int main()
{
//freopen("in","r",stdin);
scanf("%d",&T);
while (T--)
{
scanf("%d%d",&n,&m);
init();
mapp.clear();
int id = 0;
for (int i = 0;i<m;i++)
{
cin >> e[i].u >> e[i].v;
e[i].flag=0;
if (!mapp.count(e[i].u))
{
mapp[e[i].u]=id++;
}
if (!mapp.count(e[i].v))
{
mapp[e[i].v]=id++;
}
int uu = mapp[e[i].u];
int vv = mapp[e[i].v];
G[uu].push_back(vv);
G[vv].push_back(uu);
}
dfs(0,-1);
int flag = 1;
for (int i = 0;i<n;i++)
if (!pre[i])
{
flag = 0;
break;
}
if (!flag)
puts("0");
else
{
int ans = 0;
for (int i = 0;i<m;i++)
{
int uu = mapp[e[i].u];
int vv = mapp[e[i].v];
if (low[uu]>pre[vv] || low[vv]>pre[uu])
{
e[i].flag=1;
ans++;
}
}
printf("%d\n",ans);
for (int i = 0;i<m;i++)
if (e[i].flag)
cout << e[i].u << " " << e[i].v << endl;
}
}
//printf("time=%.3lf",(double)clock()/CLOCKS_PER_SEC);
return 0;
}
/* 300MS */
#include<cstdio>
#include<cstring>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;
const int maxn=10000+10;
const int maxm=100000+10;
int n,m;
struct node
{
char s[20];
bool operator <(const node& rhs)const
{
return strcmp(s,rhs.s)<0;
}
};
map<node,int> mp;//将字符串node映射成节点编号
struct Edge
{
node u,v;
bool flag;//标记该边是不是桥
}e[maxm];
vector<int> G[maxn];
int pre[maxn],low[maxn];
int dfs_clock;
void tarjan(int u,int fa)
{
low[u]=pre[u]=++dfs_clock;
for(int i=0;i<G[u].size();i++)
{
int v=G[u][i];
if(v==fa) continue;
if(!pre[v])
{
tarjan(v,u);
low[u]=min(low[u],low[v]);
}
else low[u] = min(low[u],pre[v]);
}
}
int main()
{
int T; scanf("%d",&T);
while(T--)
{
int id=0;
scanf("%d%d",&n,&m);
mp.clear();
dfs_clock=0;
memset(pre,0,sizeof(pre));
for(int i=1;i<=n;i++) G[i].clear();
for(int i=0;i<m;i++)
{
e[i].flag=false;
scanf("%s%s",e[i].u.s,e[i].v.s);
if(mp.find(e[i].u)==mp.end()) mp[e[i].u]= ++id;
if(mp.find(e[i].v)==mp.end()) mp[e[i].v]= ++id;
int x = mp[e[i].u], y=mp[e[i].v];
G[x].push_back(y);
G[y].push_back(x);
}
tarjan(1,-1);
bool ok=true;//判断是否连通图
for(int i=1;i<=n;i++)if(!pre[i])
{
ok=false;
break;
}
if(!ok) printf("0\n");
else
{
int ans=0;//计数桥总数
for(int i=0;i<m;i++)
{
int u=mp[e[i].u], v=mp[e[i].v];
if(low[u]>pre[v]||low[v]>pre[u]) e[i].flag=true,ans++;
}
printf("%d\n",ans);
for(int i=0;i<m;i++)if(e[i].flag)
printf("%s %s\n",e[i].u.s,e[i].v.s);
}
}
return 0;
}