P1656 炸铁路
题解
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;
}
题解2:
对于能彼此之间至少有两条路能互相到达的节点,我们给他们标号为这一群节点中能最先到的节点的时间戳,对于不同标号的人,是无法相互到达的,所以只需要统计有多少种标号即可,也就是统计所有的边,子节点能到达的最小值到不到得了当前节点
code2
#define ll long long
#include<bits/stdc++.h>
using namespace std;
ll lip[155]={0}; // 代表每个节点第一次遍历的时间戳
ll low[155]={0}; // 代表这个节点往上跑,能到达的最小时间戳的节点
//从时间戳小的点一定能到时间戳大的点
vector<ll> G[155];
struct unit
{
ll x,y;
}E[5005];
ll len=0;
ll cnt=0;
inline void read(ll &x) {
x = 0;
ll flag = 1;
char c = getchar();
while(c < '0' || c > '9'){
if(c == '-')flag = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
x = (x << 3) + (x << 1) + (c ^ 48);
c = getchar();
}
x *= flag;
}
inline void write(ll x)
{
if(x < 0){
putchar('-');
x = -x;
}
if(x > 9)
write(x / 10);
putchar(x % 10 + '0');
}
void ss(ll now,ll fa)
{
lip[now]=++len;
low[now]=len;
for(auto next:G[now])
{
if(next==fa)continue;
if(lip[next])low[now]=min(lip[next],low[now]);
else
{
ss(next,now); // 当搜索结束时,子节点的标号一定是完成的
low[now]=min(low[now],low[next]);
if(low[next]>lip[now]) // 如果你能到达的最小点比我还大,那你肯定到不了我这
{
E[++cnt].x=min(next,now);
E[cnt].y=max(next,now);
}
}
}
}
bool cmp(unit a,unit b)
{
if(a.x!=b.x)return a.x<b.x;
else return a.y<b.y;
}
int main()
{
ll n,m;
read(n); read(m);
for(ll i=1;i<=m;i++)
{
ll x,y;
read(x); read(y);
G[x].push_back(y);
G[y].push_back(x);
}
ss(1,0);
sort(E+1,E+1+cnt,cmp);
for(ll i=1;i<=cnt;i++)
{
write(E[i].x); putchar(' '); write(E[i].y); putchar('\n');
}
return 0;
}

浙公网安备 33010602011771号