ural 1106. Two Teams dfs

1106. Two Teams

Time limit: 1.0 second
Memory limit: 64 MB
The group of people consists of  N members. Every member has one or more friends in the group. You are to write program that divides this group into two teams. Every member of each team must have friends in another team.

Input

The first line of input contains the only number  N ( N ≤ 100). Members are numbered from 1 to  N. The second, the third,…and the ( N+1)th line contain list of friends of the first, the second, …and the Nth member respectively. This list is finished by zero. Remember that friendship is always mutual in this group.

Output

The first line of output should contain the number of people in the first team or zero if it is impossible to divide people into two teams. If the solution exists you should write the list of the first group into the second line of output. Numbers should be divided by single space. If there are more than one solution you may find any of them.

Sample

input output
7
2 3 0
3 1 0
1 2 4 5 0
3 0
3 0
7 0
6 0
4
2 4 5 6
Problem Author: Dmitry Filimonenkov
Problem Source: Tetrahedron Team Contest May 2001
class InputReader():
    def nextInt(self):
        return  int(input().strip())

    def nextSplitInts(self):
        a = self.nextInts()
        return tuple(a)

    def nextLine(self):
        return input()

    def nextString(self):
        return input().strip()

    def nextInts(self):
        ints = []
        str = input().strip().split()
        for s in str:
            if (s is None) or (len(s) == 0):
                continue
            ints.append(int(s))
        return ints

    def nextFloats(self):
        floats = []
        str = input().strip().split()
        for s in str:
            if (s is None) or (len(s) == 0):
                continue
            floats.append(float(s))
        return floats

class Ural1106():

    def solve(self):
        reader = InputReader()
        n = reader.nextInt()
        self.adj = [[] for i in range(n+1)]
        for u in range(1 , n+1):
            vs = reader.nextInts()
            vs.pop()
            for v in vs:
                self.adj[u].append(v)
        self.color = [-1]*(n+1)
        for u in range(1 , n+1):
            if self.color[u] == -1:
                self.dfs(u , 1)
        sum = 0
        us = []
        for u in range(1 , n+1):
            if self.color[u] == 1 :
                sum += 1
                us.append(u)
        print(sum)
        for u in us:
            print(u , end=" ")

    def dfs(self , u , c):
        self.color[u] = c
        for v in self.adj[u]:
            if self.color[v] == -1:
                self.dfs(v , 1-c)

if __name__ == '__main__':
    task = Ural1106()
    task.solve()


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值