Submission #19723874
Source Code Expand
Copy
#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < int(n); i++)
#define rrep(i,n) for(int i = int(n)-1; i >= 0; i--)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define pb push_back
using namespace std;
using ll = long long;
using P = pair<ll,ll>;
int dx[] = {1, 0,-1, 0, 1, 1,-1,-1};
int dy[] = {0, 1, 0,-1, 1,-1, 1,-1};
ll mod = 998244353;
ll MOD = 1000000007;
int inf = 1001001001;
ll INF = 1001001001001001001;
struct UnionFind {
vector<int> par, size;
UnionFind(int x) {
par.resize(x);
size.resize(x, 1);
for(int i = 0; i < x; i++) {
par[i] = i;
}
}
int find(int x) {
if(par[x] == x) {
return x;
}
return par[x] = find(par[x]);
}
bool same(int x, int y) {
return find(x) == find(y);
}
int consize(int x) {
return size[find(x)];
}
void unite(int x, int y) {
x = find(x);
y = find(y);
if(x == y) {
return;
}
if(size[x] < size[y]) {
par[x] = y;
size[y] += size[x];
}
else {
par[y] = x;
size[x] += size[y];
}
}
};
int main() {
int N;
cin >> N;
vector<int>a(N),b(N);
map<int,int>number;
UnionFind uf(N);
rep(i,N) {
cin >> a[i] >> b[i];
if(number.count(a[i])) {
uf.unite(number[a[i]],i);
}
if(number.count(b[i])) {
uf.unite(number[b[i]],i);
}
number[a[i]] = i;
number[b[i]] = i;
}
map<int,set<int>>color;
rep(i,N) {
color[uf.find(i)].insert(a[i]);
color[uf.find(i)].insert(b[i]);
}
int ans = 0;
for(pair<int,set<int>>x:color) {
ans += min(uf.consize(x.first),(int)x.second.size());
}
cout << ans << endl;
}
Submission Info
Submission Time |
|
Task |
B - Reversible Cards |
User |
primenumberzz |
Language |
C++ (GCC 9.2.1) |
Score |
400 |
Code Size |
1945 Byte |
Status |
AC |
Exec Time |
471 ms |
Memory |
62560 KB |
Judge Result
Set Name |
Sample |
All |
Score / Max Score |
0 / 0 |
400 / 400 |
Status |
|
|
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 |
8 ms |
3480 KB |
0_001.txt |
AC |
2 ms |
3624 KB |
0_002.txt |
AC |
2 ms |
3412 KB |
1_003.txt |
AC |
2 ms |
3420 KB |
1_004.txt |
AC |
2 ms |
3624 KB |
1_005.txt |
AC |
90 ms |
6328 KB |
1_006.txt |
AC |
5 ms |
3732 KB |
1_007.txt |
AC |
7 ms |
3668 KB |
1_008.txt |
AC |
166 ms |
6352 KB |
1_009.txt |
AC |
460 ms |
35196 KB |
1_010.txt |
AC |
469 ms |
35072 KB |
1_011.txt |
AC |
471 ms |
35112 KB |
1_012.txt |
AC |
358 ms |
62560 KB |
1_013.txt |
AC |
294 ms |
43828 KB |
1_014.txt |
AC |
105 ms |
6256 KB |
1_015.txt |
AC |
222 ms |
34356 KB |
1_016.txt |
AC |
3 ms |
3488 KB |
1_017.txt |
AC |
58 ms |
7908 KB |
1_018.txt |
AC |
119 ms |
13760 KB |
1_019.txt |
AC |
382 ms |
28436 KB |
1_020.txt |
AC |
383 ms |
33648 KB |
1_021.txt |
AC |
2 ms |
3496 KB |
1_022.txt |
AC |
2 ms |
3488 KB |
1_023.txt |
AC |
333 ms |
27620 KB |
1_024.txt |
AC |
151 ms |
15740 KB |
1_025.txt |
AC |
445 ms |
34220 KB |
1_026.txt |
AC |
211 ms |
19008 KB |