Submission #6068038


Source Code Expand

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

int n, x[100100], y[100100], ds[100100];
vector <int> ver[100100], hor[100100];

int get(int u)
{
  return ds[u] == u ? u : ds[u] = get(ds[u]);
}

void unite(int u, int v)
{
  int du = get(u), dv = get(v);
  if (du != dv)
    ds[dv] = du;
}

int main()
{
  ios::sync_with_stdio(0);
  cin >> n;
  for (int i = 0; i < n; i++)
  {
    ds[i] = i;
    cin >> x[i] >> y[i];
    ver[x[i]].push_back(i);
    hor[y[i]].push_back(i);
  }

  for (int i = 1; i <= 100000; i++)
  {
    for (int j = 1; j < ver[i].size(); j++)
      unite(ver[i][j], ver[i][j - 1]);
    for (int j = 1; j < hor[i].size(); j++)
      unite(hor[i][j], hor[i][j - 1]);
  }

  long long ans = 0;
  for (int i = 0; i < n; i++)
    if (get(i) == i)
    {
      set <int> allX, allY;
      for (int j = 0; j < n; j++)
        if (get(j) == i)
        {
          allX.insert(x[j]);
          allY.insert(y[j]);
        }
      ans += 1LL * allX.size() * allY.size();
    }

  cout << ans - n << endl;
}

Submission Info

Submission Time
Task F - Must Be Rectangular!
User flashmt
Language C++14 (GCC 5.4.1)
Score 600
Code Size 1066 Byte
Status AC
Exec Time 118 ms
Memory 13952 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 600 / 600
Status
AC × 3
AC × 20
Set Name Test Cases
Sample s1.txt, s2.txt, s3.txt
All 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, s1.txt, s2.txt, s3.txt
Case Name Status Exec Time Memory
01.txt AC 3 ms 4992 KB
02.txt AC 3 ms 4992 KB
03.txt AC 3 ms 4992 KB
04.txt AC 3 ms 4992 KB
05.txt AC 3 ms 4992 KB
06.txt AC 5 ms 4992 KB
07.txt AC 5 ms 4992 KB
08.txt AC 5 ms 4992 KB
09.txt AC 97 ms 13952 KB
10.txt AC 93 ms 13952 KB
11.txt AC 95 ms 13952 KB
12.txt AC 81 ms 9728 KB
13.txt AC 77 ms 9728 KB
14.txt AC 111 ms 13568 KB
15.txt AC 118 ms 13568 KB
16.txt AC 114 ms 13568 KB
17.txt AC 39 ms 7168 KB
s1.txt AC 3 ms 4992 KB
s2.txt AC 3 ms 4992 KB
s3.txt AC 3 ms 4992 KB