uva 315 Network

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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章