Submission details
Task:Building Teams
Sender:Ciphra
Submission time:2025-09-08 17:49:43 +0300
Language:C++ (C++11)
Status:READY
Result:
Test results
testverdicttime
#10.00 sdetails
#20.00 sdetails
#30.00 sdetails
#40.00 sdetails
#5ACCEPTED0.00 sdetails
#60.01 sdetails
#70.01 sdetails
#80.01 sdetails
#90.01 sdetails
#10ACCEPTED0.01 sdetails
#11ACCEPTED0.00 sdetails
#12ACCEPTED0.00 sdetails

Code

#include <vector>
struct UnionFind {
  std::vector<int> sets;
  int numSepSets;
  //sets[i] = x: if x<0, i is a root, and -x is the size else i is a child of parent x.
  UnionFind(int n) : sets(n, -1), numSepSets(n) {}
  bool sameRoot(int a, int b) { return find(a) == find(b); }
  int sizeOfSet(int x) { return -sets[find(x)]; }

  //Path compression
  int find(int x) 
  { 
    return sets[x] < 0 ? x : sets[x] = find(sets[x]); 
  }

  //Union by rank
  bool unionOf(int a, int b) 
  {
    a = find(a);
    b = find(b);
    if (a == b) return false;
    if (sets[a] > sets[b]) std::swap(a, b); // or if (-sets[a] < -sets[b]) std::swap(a, b);
    sets[a] += sets[b]; 
    sets[b] = a;
    --numSepSets;
    return true;
  }
};


#include <iostream>
int main(){
  int n,m;
  std::cin >> n >> m;
  UnionFind dsu(n + 2);
  for (int f = 0; f<m; ++f){
    int p1, p2;
    std::cin >> p1 >> p2;
    if ((dsu.sameRoot(p1-1, n) && !dsu.sameRoot(p2-1, n+1))){
      dsu.unionOf(p2-1, n+1);
    } else if ((dsu.sameRoot(p1-1, n+1) && !dsu.sameRoot(p2-1, n))){
      dsu.unionOf(p2-1, n);
    } else if ((!dsu.sameRoot(p1-1, n) && dsu.sameRoot(p2-1, n+1))){
      dsu.unionOf(p1-1, n);
    } else if ((!dsu.sameRoot(p1-1, n+1) && dsu.sameRoot(p2-1, n))){
      dsu.unionOf(p1-1, n+1);
    } else if (dsu.sameRoot(p1-1, p2-1)){
      std::cout << "IMPOSSIBLE" << "\n";
      return 0;
    }else {
      dsu.unionOf(p1-1, n);
      dsu.unionOf(p2-1, n+1);
    }
  }
  
  bool possible = true;
  std::vector<int> groups(n);
  for (int p = 0; p<n; ++p){
    if (dsu.sameRoot(p, n) && dsu.sameRoot(p, n+1)){
      possible = false;
      break;
    } else if (dsu.sameRoot(p, n)){
      groups[p] = 1;
    } else if (dsu.sameRoot(p, n+1)){
      groups[p] = 2;
    } else {
      dsu.unionOf(p, n+1);
      groups[p] = 2;
    }
  }

  if (!possible){
    std::cout << "IMPOSSIBLE";
  }else{
    for (int p = 0; p<n; ++p){
      std::cout << groups[p] << " ";
    }
  }
  std::cout << std::endl;
}

Test details

Test 1

Verdict:

input
10 20
3 4
8 10
3 7
1 8
...

correct output
1 1 1 2 2 1 2 2 2 1 

user output
IMPOSSIBLE

Test 2

Verdict:

input
10 20
1 3
8 10
2 4
6 8
...

correct output
1 1 2 2 1 1 1 2 1 1 

user output
IMPOSSIBLE

Test 3

Verdict:

input
10 20
7 10
3 10
9 10
2 10
...

correct output
1 2 2 1 1 1 2 1 2 1 

user output
IMPOSSIBLE

Test 4

Verdict:

input
10 20
2 4
2 10
7 10
4 6
...

correct output
1 2 1 1 2 2 2 1 2 1 

user output
IMPOSSIBLE

Test 5

Verdict: ACCEPTED

input
10 20
3 5
8 10
9 10
1 8
...

correct output
IMPOSSIBLE

user output
IMPOSSIBLE

Test 6

Verdict:

input
100000 200000
47355 96505
90709 92058
735 80715
91802 94265
...

correct output
1 2 2 1 2 1 1 1 2 2 1 2 1 1 1 ...

user output
IMPOSSIBLE

Test 7

Verdict:

input
100000 200000
59991 95794
95150 96051
78453 94730
90411 95523
...

correct output
1 1 1 2 2 1 1 2 1 2 1 2 2 2 1 ...

user output
IMPOSSIBLE

Test 8

Verdict:

input
100000 200000
89827 96402
65137 86792
80965 94708
19479 48078
...

correct output
1 2 1 1 2 1 2 2 2 1 2 1 1 2 1 ...

user output
IMPOSSIBLE

Test 9

Verdict:

input
100000 200000
72952 83723
66197 70052
2949 52160
55753 95651
...

correct output
1 1 2 2 2 1 1 2 2 2 2 2 1 2 1 ...

user output
IMPOSSIBLE

Test 10

Verdict: ACCEPTED

input
100000 200000
38942 96755
70049 82663
7746 72732
87819 99029
...

correct output
IMPOSSIBLE

user output
IMPOSSIBLE

Test 11

Verdict: ACCEPTED

input
5 4
1 2
3 4
4 5
5 3

correct output
IMPOSSIBLE

user output
IMPOSSIBLE

Test 12

Verdict: ACCEPTED

input
4 5
1 2
1 4
2 3
2 4
...

correct output
IMPOSSIBLE

user output
IMPOSSIBLE