Submission #23504857


Source Code Expand

#include <bits/stdc++.h>
using namespace std;

struct Unionfind {
  // tree number
  vector<int> par;
  // constructor
  Unionfind(int n = 1) : par(n, -1) {}
  // search root
  int root(int x) {
    if (par[x] < 0) return x;
    return par[x] = root(par[x]);
  }
  // is same?
  bool issame(int x, int y) { return root(x) == root(y); }

  // add
  // already added, return 0
  bool uni(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y) return 0;
    if (par[x] > par[y]) swap(x, y);
    par[x] += par[y];
    par[y] = x;
    return 1;
  }
  int size(int x) { return -par[root(x)]; }
};

using P = pair<int, int>;

int n, m;
set<int> a;

vector<P> solve();

int main() {
  cin >> n >> m;
  {
    for (int i = 1; i < n; ++i) a.insert(i);
    for (int i = 0; i < m; ++i) {
      int p;
      cin >> p;
      a.erase(p);
    }
  }
  auto res = solve();
  if (res.size() == n - 1)
    for (auto [x, y] : res) cout << x << " " << y << endl;
  else
    cout << -1 << endl;
  return 0;
}

vector<P> solve() {
  vector<P> res;
  vector<int> base, v;
  for (auto p : a) {
    int x = p;
    for (auto q : base) p = min(p, p ^ q);
    if (p) {
      base.push_back(p);
      v.push_back(x);
    }
  }
  Unionfind uf(n);
  for (int p = 0; p < n; ++p)
    for (auto q : v)
      if (!uf.issame(p, p ^ q)) {
        uf.uni(p, p ^ q);
        res.emplace_back(p, p ^ q);
      }
  return res;
}

Submission Info

Submission Time
Task F - Encounter and Farewell
User m_tsubasa
Language C++ (GCC 9.2.1)
Score 600
Code Size 1469 Byte
Status AC
Exec Time 536 ms
Memory 18564 KiB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:49:18: warning: comparison of integer expressions of different signedness: ‘std::vector<std::pair<int, int> >::size_type’ {aka ‘long unsigned int’} and ‘int’ [-Wsign-compare]
   49 |   if (res.size() == n - 1)
      |       ~~~~~~~~~~~^~~~~~~~

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 600 / 600
Status
AC × 3
AC × 43
Set Name Test Cases
Sample 01_sample.txt, 02_sample.txt, 03_sample.txt
All 01_sample.txt, 02_sample.txt, 03_sample.txt, 04_small.txt, 05_small.txt, 06_small.txt, 07_small.txt, 08_small.txt, 09_small.txt, 10_small.txt, 11_small.txt, 12_small.txt, 13_small.txt, 14_small.txt, 15_small.txt, 16_small.txt, 17_small.txt, 18_hand.txt, 19_bases.txt, 20_bases.txt, 21_bases.txt, 22_bases.txt, 23_bases.txt, 24_bases.txt, 25_bases.txt, 26_bases.txt, 27_large.txt, 28_large.txt, 29_large.txt, 30_large.txt, 31_large.txt, 32_large.txt, 33_large.txt, 34_large.txt, 35_large.txt, 36_large.txt, 37_large.txt, 38_large.txt, 39_large.txt, 40_large.txt, 41_large.txt, 42_large.txt, 43_large.txt
Case Name Status Exec Time Memory
01_sample.txt AC 8 ms 3508 KiB
02_sample.txt AC 2 ms 3596 KiB
03_sample.txt AC 2 ms 3552 KiB
04_small.txt AC 2 ms 3560 KiB
05_small.txt AC 2 ms 3488 KiB
06_small.txt AC 2 ms 3436 KiB
07_small.txt AC 2 ms 3504 KiB
08_small.txt AC 2 ms 3596 KiB
09_small.txt AC 2 ms 3400 KiB
10_small.txt AC 2 ms 3456 KiB
11_small.txt AC 2 ms 3456 KiB
12_small.txt AC 3 ms 3524 KiB
13_small.txt AC 2 ms 3408 KiB
14_small.txt AC 1 ms 3404 KiB
15_small.txt AC 2 ms 3460 KiB
16_small.txt AC 5 ms 3552 KiB
17_small.txt AC 2 ms 3496 KiB
18_hand.txt AC 3 ms 3596 KiB
19_bases.txt AC 531 ms 16036 KiB
20_bases.txt AC 267 ms 9704 KiB
21_bases.txt AC 138 ms 6628 KiB
22_bases.txt AC 79 ms 4908 KiB
23_bases.txt AC 527 ms 15852 KiB
24_bases.txt AC 261 ms 10236 KiB
25_bases.txt AC 138 ms 6628 KiB
26_bases.txt AC 77 ms 5092 KiB
27_large.txt AC 162 ms 17312 KiB
28_large.txt AC 159 ms 17480 KiB
29_large.txt AC 158 ms 15836 KiB
30_large.txt AC 159 ms 15800 KiB
31_large.txt AC 536 ms 17920 KiB
32_large.txt AC 527 ms 17968 KiB
33_large.txt AC 530 ms 17880 KiB
34_large.txt AC 526 ms 17960 KiB
35_large.txt AC 161 ms 17368 KiB
36_large.txt AC 159 ms 17468 KiB
37_large.txt AC 461 ms 18488 KiB
38_large.txt AC 489 ms 18520 KiB
39_large.txt AC 503 ms 18564 KiB
40_large.txt AC 516 ms 18464 KiB
41_large.txt AC 141 ms 15476 KiB
42_large.txt AC 135 ms 17480 KiB
43_large.txt AC 138 ms 18408 KiB