Submission #19291549


Source Code Expand

Copy
#define LOCAL
#define _USE_MATH_DEFINES
#include <array>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <vector>
#include <queue>
#include <stack>
#include <list>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>
#include <complex>
#include <cmath>
#include <numeric>
#include <bitset>
#include <functional>
#include <random>
#include <ctime>

using namespace std;

template <typename A, typename B>
ostream& operator <<(ostream& out, const pair<A, B>& a) {
  out << "(" << a.first << "," << a.second << ")";
  return out;
}
template <typename T, size_t N>
ostream& operator <<(ostream& out, const array<T, N>& a) {
  out << "["; bool first = true;
  for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]";
  return out;
}
template <typename T>
ostream& operator <<(ostream& out, const vector<T>& a) {
  out << "["; bool first = true;
  for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]";
  return out;
}
template <typename T, class Cmp>
ostream& operator <<(ostream& out, const set<T, Cmp>& a) {
  out << "{"; bool first = true;
  for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "}";
  return out;
}
template <typename U, typename T, class Cmp>
ostream& operator <<(ostream& out, const map<U, T, Cmp>& a) {
  out << "{"; bool first = true;
  for (auto& p : a) { out << (first ? "" : ", "); out << p.first << ":" << p.second; first = 0;} out << "}";
  return out;
}
#ifdef LOCAL
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...) 42
#endif
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
  cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
  const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}

typedef long long int64;
typedef pair<int, int> ii;
#define SZ(x) (int)((x).size())
template <typename T> static constexpr T inf = numeric_limits<T>::max() / 2;
const int MOD = 1e9 + 7;
mt19937 mrand(random_device{}());
int rnd(int x) { return mrand() % x; }

struct fast_ios {
  fast_ios() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
  };
} fast_ios_;

const int M = 4e5 + 10;
const int N = 6e5 + 10;
int f[N], c1[N], c2[N];

int find(int x) {
  if (f[x] != f[f[x]]) f[x] = find(f[x]);
  return f[x];
}

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < N; ++i) f[i] = i;
  for (int i = 0; i < n; ++i) c1[i] = 1;
  for (int i = 0; i < M; ++i) c2[n + i] = 1;
  for (int i = 0; i < n; ++i) {
    int x, y;
    cin >> x >> y;
    --x; --y;
    x += n; y += n;
    int ri = find(i), rx = find(x);
    if (ri != rx) {
      f[rx] = ri;
      c1[ri] += c1[rx];
      c2[ri] += c2[rx];
    }
    int ry = find(y);
    if (ri != ry) {
      f[ry] = ri;
      c1[ri] += c1[ry];
      c2[ri] += c2[ry];
    }
  }

  int ret = 0;
  for (int i = 0; i < n + M; ++i) {
    if (find(i) != i) continue;
    ret += min(c1[i], c2[i]);
  }
  cout << ret << '\n';

  return 0;
}

Submission Info

Submission Time
Task B - Reversible Cards
User cuiaoxiang
Language C++ (Clang 10.0.0)
Score 400
Code Size 3390 Byte
Status AC
Exec Time 255 ms
Memory 11720 KB

Compile Error

./Main.cpp:78:42: warning: unused variable 'inf' [-Wunused-const-variable]
template <typename T> static constexpr T inf = numeric_limits<T>::max() / 2;
                                         ^
./Main.cpp:79:11: warning: unused variable 'MOD' [-Wunused-const-variable]
const int MOD = 1e9 + 7;
          ^
2 warnings generated.

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 400 / 400
Status
AC × 3
AC × 27
Set Name Test Cases
Sample 0_000.txt, 0_001.txt, 0_002.txt
All 0_000.txt, 0_001.txt, 0_002.txt, 1_003.txt, 1_004.txt, 1_005.txt, 1_006.txt, 1_007.txt, 1_008.txt, 1_009.txt, 1_010.txt, 1_011.txt, 1_012.txt, 1_013.txt, 1_014.txt, 1_015.txt, 1_016.txt, 1_017.txt, 1_018.txt, 1_019.txt, 1_020.txt, 1_021.txt, 1_022.txt, 1_023.txt, 1_024.txt, 1_025.txt, 1_026.txt
Case Name Status Exec Time Memory
0_000.txt AC 13 ms 7068 KB
0_001.txt AC 9 ms 6880 KB
0_002.txt AC 8 ms 6992 KB
1_003.txt AC 12 ms 6928 KB
1_004.txt AC 7 ms 6876 KB
1_005.txt AC 145 ms 8728 KB
1_006.txt AC 11 ms 7004 KB
1_007.txt AC 20 ms 6968 KB
1_008.txt AC 178 ms 8564 KB
1_009.txt AC 251 ms 8492 KB
1_010.txt AC 255 ms 8504 KB
1_011.txt AC 255 ms 8512 KB
1_012.txt AC 202 ms 8452 KB
1_013.txt AC 203 ms 8596 KB
1_014.txt AC 198 ms 11720 KB
1_015.txt AC 176 ms 11628 KB
1_016.txt AC 8 ms 6872 KB
1_017.txt AC 46 ms 7220 KB
1_018.txt AC 83 ms 7572 KB
1_019.txt AC 230 ms 8564 KB
1_020.txt AC 230 ms 8512 KB
1_021.txt AC 9 ms 6880 KB
1_022.txt AC 14 ms 6940 KB
1_023.txt AC 190 ms 8108 KB
1_024.txt AC 107 ms 7548 KB
1_025.txt AC 245 ms 8564 KB
1_026.txt AC 128 ms 7668 KB