题解
1.暴力模拟
对每条边枚举(枚举之前先对边排序),然后对除去枚举边之外的边做并查集
code1:
#include<bits/stdc++.h>
using namespace std;
struct unit
{
int x,y;
}edge[5005];
bool cmp(unit a,unit b)
{
if(a.x!=b.x)return a.x<b.x;
else return a.y<b.y;
}
int fa[155]={0};
int finds(int now)
{
return fa[now]=(fa[now]==now?now:finds(fa[now]));
}
void build(int x,int y)
{
fa[finds(x)]=finds(y);
}
int main()
{
int n,m;
cin>>n>>m;
for(int i=1;i<=m;i++)
{
cin>>edge[i].x>>edge[i].y;
if(edge[i].x>edge[i].y) swap(edge[i].x,edge[i].y);
}
sort(edge+1,edge+1+m,cmp);
for(int i=1;i<=m;i++)
{
for(int j=1;j<=n;j++)fa[j]=j;
for(int j=1;j<=m;j++)
{
if(i!=j)
{
build(edge[j].x,edge[j].y);
}
}
set<int> q;
for(int j=1;j<=n;j++)
{
q.insert(finds(j));
if(q.size()>1)break;
}
if(q.size()>1)
{
cout<<edge[i].x<<" "<<edge[i].y<<endl;
}
}
return 0;
}
标签:int,P1656,铁路,枚举,edge,unit,cmp
From: https://www.cnblogs.com/pure4knowledge/p/18027970