1106. Teams
Time limit:1.0 Second
Memory limit:64 MB
The group of people consists of
NMembers. Every member have one or more friends in the group. You is to write program the divides this group into the teams. Every member of each team must has friends in another team. Inputthe first line of input contains the only number
N(
N≤100). Members is 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 was finished by zero. Remember that friendship was always the mutual in the this group. Outputthe first line of output should contain the number of people in the first team or zero if it's impossible to divide People into and teams. If The solution exists you should write the list of the first group to the second line of output. Numbers should is divided by single space. If there is more than one solution you could find any of the them. Sample
input |
Output |
72 3 03 1 01 2 4 5 03 03 07 06 0 |
42 4 5 6 |
problem Author:Dmitry Filimonenkov
problem Source:Tetrahedron Team Contest May 2001
Analysis: Seemingly two-point diagram. But it's simpler than a binary chart.
Open two tag array, record whether everyone has friends in the first group, the second group.
Sweep the person once:
1. If the first group does not have his friend: then put it in the first group and mark his friends in the first group with friends.
2. Otherwise:
(1) If the second group does not have his friend, then put it in the second group and mark his friends in the second group with friends.
(2) Otherwise, the description in both groups have no friends, you can put him casually in a group, also can not handle.
AC Code:
#include <bits/stdc++.h>using namespace Std;bool c[105], b[105];vector<int> a[105];vector<int> ans; int main () {#ifdef sxk freopen ("In.txt", "R", stdin); #endif//SXK int n, x; while (scanf ("%d", &n)! = EOF) {memset (c, False, sizeof (c)); Memset (b, False, sizeof (b)); Ans.clear (); for (int i=1; i<=n; i++) {while (scanf ("%d", &x) && x) a[i].push_back (x); } for (int i=1; i<=n; i++) {if (!c[i]) {//First group No Friends ans.push_back (i); for (int j=0; j<a[i].size (); j + +) c[A[i][j]] = true; } else{if (!b[i]) {//second group No friends for (int j=0; j<a[i].size (); j + +) b[A[i][j] ] = true; }//None, not processed} int cnt = Ans.size (); CNT%= N; printf ("%d\n", CNT); for (int i=0; i<cnt; i++) {printf ("%d%c", Ans[i], I < cnt-1? ' ': ' \ n '); }} return 0;}
URAL 1106. Two Teams (sub-chart)