QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#112937 | #6502. Disjoint Set Union | jeffqi | WA | 162ms | 3540kb | C++23 | 2.1kb | 2023-06-15 14:03:26 | 2023-06-15 14:03:27 |
Judging History
answer
#include<bits/stdc++.h>
#define rep(i,a,b) for (int i = (a); i < (b); i++)
#define drep(i,a,b) for (int i = (a); i >= (b); i--)
#define ll long long
#define vi vector<int>
#define vll vector<ll>
#define eb emplace_back
#define pb push_back
#define all(v) v.begin(),v.end()
#define sz(v) ((int)v.size())
#define pii pair<int,int>
#define pll pair<ll,ll>
#define fi first
#define se second
#define umap unordered_map
using namespace std;
namespace qiqi {
struct DSU {
vi fa;
DSU(vi &vec) {fa = vec;}
bool isroot(int x) {return x == fa[x];}
int find(int x) {return x == fa[x] ? x : fa[x] = find(fa[x]);}
void merge(int x,int y) {
x = find(x); y = find(y);
if (x == y) return;
fa[y] = x;
}
};
void main() {
int n; cin >> n;
vi vec(n);
rep(i,0,n) {
cin >> vec[i]; --vec[i];
}
DSU dsu(vec);
rep(i,0,n) {
cin >> vec[i]; --vec[i];
}
DSU ed(vec);
vector<vi> e(n); vi d(n);
vector<array<int,3>> ans;
rep(i,0,n) {
int x = ed.fa[i];
if (x != dsu.fa[i]) {
dsu.find(i); ans.pb({1,i,0});
if (x != dsu.fa[i]) {
if (!dsu.isroot(x)) {
cout << "NO\n";
return;
}
++d[x]; e[dsu.find(i)].eb(x);
}
}
}
queue<int> q;
rep(i,0,n) {
if (!d[i]) q.emplace(i);
}
vi p,rk(n);
while (!q.empty()) {
int u = q.front(); q.pop();
rk[u] = sz(p); p.eb(u);
for (auto v:e[u]) {
if (!(--d[v])) q.push(v);
}
}
auto cmp = [&](int a,int b) {
return rk[a] < rk[b];
};
sort(all(p),cmp);
for (auto u:p) {
sort(all(e[u]),cmp);
for (auto v:e[u]) {
dsu.merge(v,u); ans.pb({2,u,v});
rep(i,0,n) {
if (dsu.fa[i] != ed.fa[i]) {
dsu.find(i); ans.pb({1,i,0});
}
}
break;
}
}
rep(i,0,n) {
if (dsu.fa[i] != ed.fa[i]) {
cout << "NO\n";
return;
}
}
cout << "YES\n" << sz(ans) << '\n';
for (auto [o,x,y]:ans) {
if (o == 1) cout << o << ' ' << x+1 << '\n';
else cout << o << ' ' << x+1 << ' ' << y+1 << '\n';
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T; cin >> T;
while (T--) qiqi::main();
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3540kb
input:
5 3 1 2 3 2 2 3 4 1 2 3 3 1 1 1 2 5 1 2 3 4 5 2 3 4 5 5 5 1 1 1 1 1 1 2 3 4 5 6 1 2 2 4 5 6 1 1 5 1 4 2
output:
YES 2 1 1 2 1 2 YES 9 1 2 1 3 1 4 2 3 2 1 2 1 3 1 4 2 2 1 1 3 YES 14 1 1 1 2 1 3 1 4 2 1 2 1 2 1 3 1 4 2 2 3 1 3 1 4 2 3 4 1 4 2 4 5 NO YES 20 1 2 1 3 1 4 1 5 1 6 2 6 2 1 2 1 3 1 4 1 5 2 2 5 1 2 1 3 1 4 1 5 2 5 4 1 2 1 4 2 4 1 1 2
result:
ok good! (YES count = 4, NO count = 1) (5 test cases)
Test #2:
score: -100
Wrong Answer
time: 162ms
memory: 3412kb
input:
100000 5 1 2 1 1 1 2 2 1 1 2 5 3 2 3 4 1 3 2 3 4 1 5 1 2 3 4 3 1 4 4 1 1 5 1 2 3 5 3 1 2 2 5 2 5 5 2 3 5 5 5 2 3 5 5 5 1 2 3 4 5 5 3 3 4 5 5 1 2 3 4 5 1 4 1 4 4 5 1 2 3 1 5 1 2 3 1 2 5 1 2 3 3 1 1 3 3 3 1 5 1 2 3 4 3 2 2 4 4 4 5 1 2 2 4 5 5 2 2 4 5 5 1 2 1 4 5 5 2 5 5 5 5 1 2 3 4 5 1 2 5 5 1 5 1 4 3...
output:
YES 4 1 1 1 5 2 1 2 1 5 YES 0 YES 13 1 2 1 3 1 4 1 5 2 2 4 1 3 1 4 1 5 2 3 4 1 4 1 5 2 4 1 1 5 YES 4 1 3 1 5 2 3 2 1 5 YES 0 YES 5 1 1 1 2 2 1 5 1 2 2 2 3 YES 9 1 2 1 3 1 5 2 2 4 1 3 1 5 2 3 1 1 5 2 5 4 YES 2 1 5 2 5 2 YES 2 1 2 2 2 3 YES 8 1 1 1 3 1 5 2 1 2 1 3 1 5 2 3 4 1 5 YES 2 1 1 2 1 5 YES 7 1...
result:
wrong answer you didn't find a solution but jury did (test case 5184)