POJ 1144 Network

/*
并查集求割点
*/
#include <iostream>
#include <string>
#define MAX_N 100
#define MAX_L 4950
using namespace std;

struct line
{
    int id1, id2;
    bool have(int id)
    {
        if(id != id1 && id != id2)
            return false;
        return true;
    }
}lines[MAX_L + 10];

struct set
{
    int id, count, level;
}sets[MAX_N + 1];
int num, lineNum;

void init()
{
    for(int i = 1; i <= num; i++)
    {
        sets[i].id = i;
        sets[i].count = 1;
        sets[i].level = 0;
    }
}

int find(int setId)
{
    if(sets[setId].id != setId)
        sets[setId].id = find(sets[setId].id);
    return sets[setId].id;
}

void connet(int nId1, int nId2)
{
    int sd1 = find(nId1);
    int sd2 = find(nId2);
    if(sd1 == sd2)
        return;
    if(sets[sd1].level > sets[sd2].level)
    {
        sets[sd2].id = sd1;
        sets[sd1].count += sets[sd2].count;
    }
    else if(sets[sd1].level < sets[sd2].level)
    {
        sets[sd1].id = sd2;
        sets[sd2].count += sets[sd1].count;
    }
    else
    {
        sets[sd2].id = sd1;
        sets[sd1].count += sets[sd2].count;
        sets[sd1].level++;
    }
}

int main()
{
    int from, to, i;
    char temp[300];
    while(cin>>num && num != 0)
    {
        lineNum = 0;
        getchar();
        while(cin.getline(temp, 299, 10) && temp[0] != '0')
        {
            bool first = true;
            int countV = 0;
            from = to = 0;
            for(i = 0; i < strlen(temp); i++)
            {
                int len = strlen(temp);
                if(temp[i] != ' ')
                    countV = countV * 10 + int(temp[i] - '0');
                if(temp[i] == ' ' || i == (strlen(temp) - 1))
                {
                    if(countV != 0)
                    {
                        if(first)
                        {
                            from = countV;
                            first = false;
                        }
                        else
                        {
                            to = countV;
                            if(from == to)
                                continue;
                            lineNum++;
                            line tempL = {from, to};
                            lines[lineNum] = tempL;
                        }
                    }
                    countV = 0;
                }
            }
        }
        int count = 0, n1, n2, sId;
        for(int curN = 1; curN <= num; curN++)
        {
            init();
            for(int curl = 1; curl <= lineNum; curl++)
            {
                if(lines[curl].have(curN))
                    continue;
                n1 = lines[curl].id1;
                n2 = lines[curl].id2;
                connet(n1, n2);
            }
            if(curN == num) n1 = curN - 1;
            else n1 = curN + 1;
            sId = find(n1);
            if(sets[sId].count < num - 1)
                count++;
        }
        cout<<count<<endl;
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值