#include "collapse.h"
#include <bits/stdc++.h>
using namespace std;
struct Dsu{
int n;
vector<int> pr, rk;
Dsu(){}
Dsu(int n):n(n + 4){
pr.resize(n), rk.resize(n);
for(int i = 0; i < n; ++i){
pr[i] = i;
rk[i] = 1;
}
}
int fd(int x){
return (x == pr[x] ? x : pr[x] = fd(pr[x]));
}
bool unite(int x, int y){
x = fd(x), y = fd(y);
if(x == y) return false;
if(rk[x] > rk[y]){
swap(x, y);
}
rk[y] += rk[x];
pr[x] = y;
return true;
}
};
std::vector<int> simulateCollapse(
int N,
std::vector<int> T,
std::vector<int> X,
std::vector<int> Y,
std::vector<int> W,
std::vector<int> P
) {
int n = N, m = (int)T.size(), q = (int)W.size();
vector<int> ans(q);
vector<int> r(m, -1);
map<pair<int, int>, int> mp;
for(int i = m - 1; i >= 0; --i){
if(X[i] > Y[i]){
swap(X[i], Y[i]);
}
if(T[i] == 1){
mp[{X[i], Y[i]}] = i;
}
else{
if(mp[{X[i], Y[i]}]){
r[i] = mp[{X[i], Y[i]}];
}
}
}
for(int i = 0; i < q; ++i){
Dsu gr(n + 1);
int nans = n;
for(int j = 0; j < m; ++j){
if(T[j] == 1) continue;
if(X[j] <= P[i] && Y[j] > P[i]){
continue;
}
if(r[j] != -1 && r[j] < W[i]){
continue;
}
nans -= gr.unite(X[j], Y[j]);
}
ans[i] = nans;
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
596 KB |
Output is correct |
2 |
Incorrect |
2 ms |
312 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
36 ms |
2408 KB |
Output is correct |
2 |
Incorrect |
32 ms |
2500 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
2464 KB |
Output is correct |
2 |
Incorrect |
45 ms |
2488 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
596 KB |
Output is correct |
2 |
Incorrect |
2 ms |
312 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |