Todos los desafíos
Intermedio 0 resueltos 0% de aceptación

Can We Split Into Two Teams?

Decide whether people can be assigned to two teams so that each listed pair is on opposite teams.

Problema

Decide whether people can be assigned to two teams so that each listed pair is on opposite teams. Every person receives exactly one team; either team may be empty. People without a listed pair can go on either team. Check every connected group of constraints.

Implement split_into_two_teams(people, pairs).

Input

people: The number of people, numbered from 1.

pairs: Rows [first_person, second_person] that must be separated.

Output

Return true if any valid assignment exists, otherwise false. Do not return the assignment.

Restricciones

1 ≤ people ≤ 100,000. 0…200,000 pairs of distinct person IDs in 1…people. Duplicate pairs are allowed and repeat the same constraint.

Ejemplos

Ejemplo 1
Entrada:
pairs = [
    [1, 2],
    [2, 3],
    [3, 1]
]
people = 3

Salida esperada:
expected = False
Entrada:
std::vector<std::vector<long long>> pairs = {
    {1, 2},
    {2, 3},
    {3, 1}
};
long long people = 3;

Salida esperada:
bool expected = false;
Entrada:
long[][] pairs = new long[][] {
    new long[] {1, 2},
    new long[] {2, 3},
    new long[] {3, 1}
};
long people = 3;

Salida esperada:
bool expected = false;

Explicación

If 1 is on team A, 2 must be on B, and 3 must be on A. The last pair then violates the rule.

Ejemplo 2
Entrada:
pairs = [
    [1, 2],
    [2, 3]
]
people = 4

Salida esperada:
expected = True
Entrada:
std::vector<std::vector<long long>> pairs = {
    {1, 2},
    {2, 3}
};
long long people = 4;

Salida esperada:
bool expected = true;
Entrada:
long[][] pairs = new long[][] {
    new long[] {1, 2},
    new long[] {2, 3}
};
long people = 4;

Salida esperada:
bool expected = true;

Explicación

Put 1 and 3 on team A and 2 on team B. Person 4 can join either team.

Referencias

Este reto está adaptado para CodeUtility. Sigue el enunciado y las restricciones indicadas arriba.

Aprende de otros

Soluciones de la comunidad

Aún no hay soluciones publicadas. Resuelve este reto para compartir la primera.