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

来源:互联网 发布:元素周期表 知乎 编辑:程序博客网 时间:2024/05/19 20:23
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上竟然必须打输入输出,否则爆零。(真是滑稽)

 1 /*COGS 08 2    by S.B.S.*/ 3 #include<iostream> 4 #include<cstdio> 5 #include<cstring> 6 #include<cmath> 7 #include<algorithm> 8 #include<queue> 9 #include<cstdlib>10 #include<iomanip>11 #include<cassert>12 #include<climits>13 #define maxn 12114 #define inf 0x7fffffff15 #define F(i,j,k) for(int i=j;i<=k;i++)16 #define FF(i,j,k) for(int i=j;i>=k;i--)17 #define M(a,b) memset(a,b,sizeof(b))18 using namespace std;19 int read(){20     int x=0,f=1;char ch=getchar();21     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}22     while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}23     return x*f;24 }25 vector<int> edge[maxn];26 int n,m,dfn[maxn],low[maxn];27 int cnt,tim=0,cut;28 int root;29 int ans=0;30 inline void addedge(int u,int v)31 {32      edge[u].push_back(v);33      edge[v].push_back(u);34 }35 bool gd[maxn];36 inline void dfs(int u)37 {38     low[u]=dfn[u]=++tim;39     int v;int tot=0;40     F(i,0,edge[u].size()-1)41         { 42             v=edge[u][i];43              if(!dfn[v]){44                  dfs(v);45                  tot++;46                  low[u]=min(low[v],low[u]);47                  if((u==root&&tot>1)||(u!=root&&low[v]>=dfn[u]))48                 if(!gd[u])49                 {50                         gd[u]=true;51                         ans++;52                  } 53         }54         else 55             low[u]=min(low[u],dfn[v]);56      }57 }58 int main()59 {60     std::ios::sync_with_stdio(false);61     freopen("gd.in","r",stdin);62     freopen("gd.out","w",stdout);63     int n,m;64     cin>>n;int x,y;65     while(cin>>x>>y)66     {67         addedge(x,y);68     }69     F(i,1,n)70     {71         if(!dfn[i])72         {73             root=i;74             dfs(i);75         }76     }77     cout<<ans<<endl;78     F(i,1,n){79         if(gd[i]) cout<<i<<endl;80     }81     return 0;82 }
COGS 08

 

0 0