A Telephone Line Company (TLC) is establishing a new telephone cable network. They are connecting several places numbered by integers from 1 to N. No two places have the same number. The lines are bidirectional and always connect together two places and in each place the lines end in a telephone exchange. There is one telephone exchange in each place. From each place it is possible to reach through lines every other place, however it need not be a direct connection, it can go through several exchanges. From time to time the power supply fails at a place and then the exchange does not operate. The officials from TLC realized that in such a case it can happen that besides the fact that the place with the failure is unreachable, this can also cause that some other places cannot connect to each other. In such a case we will say the place (where the failure occured) is critical. Now the officials are trying to write a program for finding the number of all such critical places. Help them.
Input
The input file consists of several blocks of lines. Each block describes one network. In the first line of each block there is the number of places N < 100. Each of the next at most N lines contains the number of a place followed by the numbers of some places to which there is a direct line from this place. These at mostN lines completely describe the network, i.e., each direct connection of two places in the network is contained at least in one row. All numbers in one line are separated by one space. Each block ends with a line containing just 0. The last block has only one line with N = 0.
Output
The output contains for each block except the last in the input file one line containing the number of critical places.
Sample Input
5
5 1 2 3 4
0
6
2 1 3
5 4 6 2
0
0
Sample Output
1
2
题意:
给一个无向连通图,求出割点的数量。
首先输入一个N,下面有不超过N行的数,每行的第一个数字和它后面的每个数字之间都存在边,0表示行输入的结束
思路:
循环每个点,搜索在该点去掉的情况下有几个点是联通的,若联通点个数小于N-1,则表示该点为割点
代码一:
#include<iostream>
#include<string>
using namespace std;
int connect[101][101];//二维数组表示联通情况
int flag[101];//标记当前位置是否访问过
int N;
void initial(int N)//初始化
{
int i,k;
char ch;
memset(connect,0,sizeof(connect));
while(cin>>k&&k)
{
getchar();
while(cin>>i)
{
ch=getchar();
connect[i][k]=1;
connect[k][i]=1;
if(ch=='\n')
{
break;
}
}
}
}
int dfs(int n)
{
int c;
if(flag[n]==1)//若已访问过,返回0
return 0;
else
{
c=1;//c初始化为0
flag[n]=1;//将该点标记为1
for(int i=1;i<=N;i++)
{
if(connect[n][i]==1)//递归搜索所有与n点联通的点
c+=dfs(i);
}
return c;
}
}
int main()
{
int num,sum,i;
while(cin>>N&&N)
{
sum=0;
if(N>2)
{
initial(N);
for(i=1;i<=N;i++)
{
flag[i]=1;//标记位置i为failure
if(i==1)//若位置1为 failure,则从位置2开始搜索每个点能与几个点联通
num=dfs(2);//num表示i位置没电时有几个点联通
else
num=dfs(1);
memset(flag,0,sizeof(flag));//每次搜索一个点后把flag初始化为0
if(num<N-1)//若联通的点数小于N-1,则表明i是一个割点,sum++
sum++;
}
}
cout<<sum<<endl;
}
return 0;
}
代码二:
#include<iostream>
#include<vector>
#include<string>
#include<sstream>
using namespace std;
const int maxsize = 110;
int marked = 0; // for marking visit
class Network{
private:
vector<vector<int> > connect;
int visit[maxsize];
int numberOfCriticalPlaces;
int dfs(int k);
public:
void initial(int n);
void readCase();
void computing();
void outResult(){ cout << numberOfCriticalPlaces << endl;}
};
void Network::initial(int n){
connect.clear();
connect.resize(n + 1); // connect[0] is not used
numberOfCriticalPlaces = 0;
marked++;
}
void Network::readCase(){
int k, m;
string line;
while((cin >> k) && k){
getline(cin, line);
stringstream ss(line);
while(ss >> m){
connect[k].push_back(m);
connect[m].push_back(k);
}
}
}
int Network::dfs(int k){
if(visit[k] == marked){
return 0;
}else{
visit[k] = marked;
int c = 1;
for(int i = 0; i < connect[k].size(); i++){
c += dfs(connect[k][i]);
}
return c;
}
}
void Network::computing(){
if(connect.size() > 3){
int c;
for(int i = 1; i < connect.size(); i++){
visit[i] = marked; //to mark its failure
if(i == 1){
c = dfs(2);
}else{
c = dfs(1);
}
marked++; // for initialization
if(c < connect.size() - 2){
numberOfCriticalPlaces++;
}
}
}
}
int main(){
Network net;
int n;
while((cin >> n) && n){
net.initial(n);
net.readCase();
net.computing();
net.outResult();
}
return 0;
}