#include <stdio.h>
#define MAX_PEOPLE 100
int people;
int friendship[MAX_PEOPLE + 1][MAX_PEOPLE + 1];
int team[MAX_PEOPLE + 1];
void divide(int one){
int teamNum = team[one];
int another;
for (another = 1; another <= people; another++)
if (friendship[one][another] == 1 && team[another] == 0){
//如果某个成员分在了1组,则把他的朋友分在(3 - 1)即2组,如果成员分在了2组,则把他的朋友分在(3 - 2)即1组
team[another] = 3 - teamNum;
divide(another);
}
}
int main(){
scanf("%d", &people);
int member;
for (member = 1; member <= people; member++){
int pal;
while (scanf("%d", &pal) != EOF && pal != 0)
friendship[member][pal] = 1;
}
for (member = 1; member <= people; member++){
if (team[member] == 0){
team[member] = 1;
divide(member);
}
}
int peopleInTeam1 = 0;
for (member = 1; member <= people; member++)
if (team[member] == 1)
peopleInTeam1++;
printf("%d\n", peopleInTeam1);
for (member = 1; member <= people; member++)
if (team[member] == 1)
printf("%d ", member);
printf("\n");
return 0;
}
URAL 1106 Two Teams (DFS + 二分图)
最新推荐文章于 2017-06-28 13:10:02 发布