您的位置:首页 > 其它

POJ 1144 Network

2009-09-26 16:23 369 查看
/*

并查集求割点

*/

#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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: