Submission #6072235
Source Code Expand
Copy
#include<bits/stdc++.h>
#define REP(x,y,z) for(int x=y;x<=z;x++)
#define MSET(x,y) memset(x,y,sizeof(x))
#define M 100005
using namespace std;
class DSJ {
public:
int ngroups;
vector<int> fa,sz;
DSJ() {}
DSJ(int _n) {
ngroups = _n;
fa = vector<int> (_n+1);
sz = vector<int> (_n+1, 1);
REP(i,0,_n) fa[i] = i;
}
int find(int x) {
return x==fa[x] ? x : fa[x]=find(fa[x]);
}
int size(int x) {
return sz[find(x)];
}
void con(int x,int y) {
x = find(x);
y = find(y);
if (x==y) return;
fa[x] = y;
sz[y] += sz[x];
ngroups--;
}
int groups() {
return ngroups;
}
}dsj;
int n,px[M],py[M];
vector<int> xs[M], ys[M];
int main()
{
while (~scanf("%d", &n)) {
REP(i,0,M-1) {
xs[i].clear();
ys[i].clear();
}
REP(i,1,n) {
int x,y;
scanf("%d %d",&x,&y);
xs[x].push_back(i);
ys[y].push_back(i);
px[i] = x;
py[i] = y;
}
dsj = DSJ(n);
REP(i,0,M-1) REP(j,1,(int)xs[i].size()-1) dsj.con(xs[i][j-1], xs[i][j]);
REP(i,0,M-1) REP(j,1,(int)ys[i].size()-1) dsj.con(ys[i][j-1], ys[i][j]);
vector<int> childs[M];
long long ans = 0;
REP(i,1,n) childs[dsj.find(i)].push_back(i);
REP(i,1,n) if(dsj.find(i)==i) {
set<int> xx, yy;
for (int j:childs[i]) {
xx.insert(px[j]);
yy.insert(py[j]);
}
ans += (long long)xx.size() * yy.size();
}
printf("%lld\n", ans-n);
}
return 0;
}
Submission Info
Submission Time
2019-06-22 21:56:55+0900
Task
F - Must Be Rectangular!
User
moffa
Language
C++14 (GCC 5.4.1)
Score
600
Code Size
1761 Byte
Status
AC
Exec Time
114 ms
Memory
17016 KB
Compile Error
./Main.cpp: In function ‘int main()’:
./Main.cpp:46:33: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&x,&y);
^
Judge Result
Set Name
Sample
All
Score / Max Score
0 / 0
600 / 600
Status
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
6 ms
7296 KB
02.txt
AC
6 ms
7296 KB
03.txt
AC
6 ms
7296 KB
04.txt
AC
6 ms
7296 KB
05.txt
AC
5 ms
7296 KB
06.txt
AC
6 ms
7296 KB
07.txt
AC
6 ms
7296 KB
08.txt
AC
6 ms
7296 KB
09.txt
AC
109 ms
17016 KB
10.txt
AC
108 ms
17016 KB
11.txt
AC
114 ms
17012 KB
12.txt
AC
84 ms
12928 KB
13.txt
AC
80 ms
12928 KB
14.txt
AC
104 ms
16760 KB
15.txt
AC
104 ms
16760 KB
16.txt
AC
102 ms
16632 KB
17.txt
AC
40 ms
10492 KB
s1.txt
AC
5 ms
7296 KB
s2.txt
AC
6 ms
7296 KB
s3.txt
AC
6 ms
7296 KB