COGS 08-备用交换机 题解——S.B.S.

时间:2025-05-05 00:08:01

8. 备用交换机

★★   输入文件:gd.in   输出文件:gd.out   简单对比
时间限制:1 s   内存限制:128 MB

【问题描述】
n个城市之间有通讯网络,每个城市都有通讯交换机,直接或间接与其它城市连接。因电子设备容易损坏,需给通讯点配备备用交换机。但备用交换机数量有限,不能全部配备,只能给部分重要城市配置。于是规定:如果某个城市由于交换机损坏,不仅本城市通讯中断,还造成其它城市通讯中断,则配备备用交换机。请你根据城市线路情况,计算需配备备用交换机的城市个数,及需配备备用交换机城市的编号。
【输入格式】
输入文件有若干行
第一行,一个整数n,表示共有n个城市(2<=n<=100)
下面有若干行,每行2个数a、b,a、b是城市编号,表示a与b之间有直接通讯线路。
【输出格式】
输出文件有若干行
第一行,1个整数m,表示需m个备用交换机,下面有m行,每行有一个整数,表示需配备交换机的城市编号,输出顺序按编号由小到大。如果没有城市需配备备用交换机则输出0。
【输入输出样例】

输入文件名: gd.in

7

1 2

2 3

2 4

3 4

4 5

4 6

4 7

5 6

6 7

输出文件名:gd.out

2

2

4

————————————————————我是分割线————————————————————————

tarjan算法模改,求割点。

模板题。

p.s.这个OJ上竟然必须打输入输出,否则爆零。(真是滑稽)

 /*COGS 08
by S.B.S.*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<cstdlib>
#include<iomanip>
#include<cassert>
#include<climits>
#define maxn 121
#define inf 0x7fffffff
#define F(i,j,k) for(int i=j;i<=k;i++)
#define FF(i,j,k) for(int i=j;i>=k;i--)
#define M(a,b) memset(a,b,sizeof(b))
using namespace std;
int read(){
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
vector<int> edge[maxn];
int n,m,dfn[maxn],low[maxn];
int cnt,tim=,cut;
int root;
int ans=;
inline void addedge(int u,int v)
{
edge[u].push_back(v);
edge[v].push_back(u);
}
bool gd[maxn];
inline void dfs(int u)
{
low[u]=dfn[u]=++tim;
int v;int tot=;
F(i,,edge[u].size()-)
{
v=edge[u][i];
if(!dfn[v]){
dfs(v);
tot++;
low[u]=min(low[v],low[u]);
if((u==root&&tot>)||(u!=root&&low[v]>=dfn[u]))
if(!gd[u])
{
gd[u]=true;
ans++;
}
}
else
low[u]=min(low[u],dfn[v]);
}
}
int main()
{
std::ios::sync_with_stdio(false);
freopen("gd.in","r",stdin);
freopen("gd.out","w",stdout);
int n,m;
cin>>n;int x,y;
while(cin>>x>>y)
{
addedge(x,y);
}
F(i,,n)
{
if(!dfn[i])
{
root=i;
dfs(i);
}
}
cout<<ans<<endl;
F(i,,n){
if(gd[i]) cout<<i<<endl;
}
return ;
}

COGS 08