URAL 1106. Two Teams (Bipartite Graph), uralteams
1106. Two Teams
Time limit: 1.0 second
Memory limit: 64 MB
The group of people consists
NMembers. 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. inputThe first line of input contains the only number
N(
N≤ 100). Members are numbered from 1
N. The second, the third ,... And (
N+ 1) th line contain list of friends of the first, the second ,... And
NTh member respectively. this list is finished by zero. remember that friendship is always mutual in this group. outputThe first line of output shoshould 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 shoshould write the list of the first group into the second line of output. numbers shoshould be divided by single space. if there are more than one solution you may find any of 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
Resolution: It looks like a bipartite graph. But it is simpler than a bipartite graph.
Open two tag arrays to record whether each person has friends in the first group and the second group.
Scan people once:
1. If there is no friend in the first group: Put it in the first group, and mark his friends who have friends in the first group.
2. otherwise:
(1) If there is no friend in the second group, put it in the second group, and mark his friends who have friends in the second group.
(2) Otherwise, if there are no friends in both groups, you can place them in one group without any further handling.
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) a [I]. push_back (x) ;}for (int I = 1; I <= n; I ++) {if (! C [I]) {// The first group has no friends ans. push_back (I); for (int j = 0; j <a [I]. size (); j ++) c [a [I] [j] = true;} else {if (! B [I]) {// no friends in the second group 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 ;}