मध्यम 0 हल किए गए 0% स्वीकृति

Can We Split Into Two Teams?

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

आप क्या सीखेंगे

  • लोगों और टकरावों को ग्राफ़ के रूप में दिखाना
  • दो रंगों से द्विभाजित ग्राफ़ जाँचना
  • ग्राफ़ के अलग-थलग हिस्से संभालना

समस्या

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.

सीमाएँ

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

उदाहरण

उदाहरण 1
इनपुट:
pairs = [
    [1, 2],
    [2, 3],
    [3, 1]
]
people = 3

अपेक्षित आउटपुट:
expected = False
इनपुट:
std::vector<std::vector<long long>> pairs = {
    {1, 2},
    {2, 3},
    {3, 1}
};
long long people = 3;

अपेक्षित आउटपुट:
bool expected = false;
इनपुट:
long[][] pairs = new long[][] {
    new long[] {1, 2},
    new long[] {2, 3},
    new long[] {3, 1}
};
long people = 3;

अपेक्षित आउटपुट:
bool expected = false;

व्याख्या

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

उदाहरण 2
इनपुट:
pairs = [
    [1, 2],
    [2, 3]
]
people = 4

अपेक्षित आउटपुट:
expected = True
इनपुट:
std::vector<std::vector<long long>> pairs = {
    {1, 2},
    {2, 3}
};
long long people = 4;

अपेक्षित आउटपुट:
bool expected = true;
इनपुट:
long[][] pairs = new long[][] {
    new long[] {1, 2},
    new long[] {2, 3}
};
long people = 4;

अपेक्षित आउटपुट:
bool expected = true;

व्याख्या

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

कैसे हल करें

पहले खुद कोशिश करें, फिर एक-एक करके संकेत खोलें।

संकेत 1

यह तभी possible है जब हर constraint component को दो अलग colors में color किया जा सके।

संकेत 2

हर connected component को traverse करो और हर pair constraint पर team assignments alternate करो।

संकेत 3

अगर odd cycle की वजह से किसी person को दोनों teams में देना पड़े, तो answer false हो जाता है।

अपेक्षित जटिलता

समय O(people + pairs), अतिरिक्त स्थान O(people + pairs)

संदर्भ

यह चुनौती CodeUtility के लिए अनुकूलित की गई है। ऊपर दिए गए स्टेटमेंट और सीमाओं का पालन करें।

दूसरों से सीखें

समुदाय समाधान

अभी तक कोई प्रकाशित समाधान नहीं। पहला साझा करने के लिए इस चुनौती को हल करें।