QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#112943 | #6502. Disjoint Set Union | jeffqi | WA | 191ms | 3480kb | C++23 | 2.1kb | 2023-06-15 14:24:20 | 2023-06-15 14:24:21 |
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 cur(vec),dsu(vec);
rep(i,0,n) {
cin >> vec[i]; --vec[i];
}
DSU ed(vec);
vector<vi> e(n),g(n); vi d(n);
vector<array<int,3>> ans;
rep(i,0,n) {
int x = ed.fa[i];
if (x != cur.fa[i]) {
cur.find(i);
dsu.find(i);
ans.pb({1,i,0});
if (x != cur.fa[i]) {
if (!cur.isroot(x)) {
cout << "NO\n";
return;
}
++d[x];
e[cur.find(i)].eb(x);
g[x].eb(cur.find(i));
}
}
}
queue<int> q;
rep(i,0,n) {
if (!d[i]) q.emplace(i);
}
vi p;
while (sz(q)) {
int u = q.front(); q.pop(); p.eb(u);
for (auto v:e[u]) {
if (!(--d[v])) q.push(v);
}
}
for (auto v:p) {
for (auto u:g[v]) {
if (dsu.find(u) != dsu.find(v)) {
cur.merge(v,u);
dsu.merge(v,u);
ans.pb({2,u,v});
rep(i,0,n) {
if (cur.fa[i] != ed.fa[i]) {
cur.find(i); ans.pb({1,i,0});
}
}
}
}
}
rep(i,0,n) {
if (cur.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: 3480kb
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 2 1 1 2
result:
ok good! (YES count = 4, NO count = 1) (5 test cases)
Test #2:
score: 0
Accepted
time: 154ms
memory: 3440kb
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 3 1 1 2 1 5 2 2 4 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:
ok good! (YES count = 100000, NO count = 0) (100000 test cases)
Test #3:
score: -100
Wrong Answer
time: 191ms
memory: 3444kb
input:
50000 10 1 2 3 4 5 6 7 8 6 10 1 10 3 4 5 6 7 8 6 10 10 6 2 3 4 5 6 2 5 5 10 6 6 6 6 6 6 6 6 6 10 10 1 2 3 4 7 7 7 10 9 10 9 7 3 9 9 9 9 9 9 7 10 1 2 3 7 4 2 3 8 3 10 2 2 2 2 2 2 2 2 2 2 10 1 2 3 5 5 8 2 8 9 10 2 2 3 5 10 8 2 10 9 10 10 1 8 3 3 5 6 10 8 9 10 5 5 5 5 5 5 5 5 5 5 10 8 2 7 4 5 6 8 8 9 1...
output:
YES 2 1 2 2 2 10 YES 26 1 2 1 3 1 4 1 5 1 7 1 8 1 9 2 2 6 1 3 1 4 1 5 1 7 1 8 1 9 2 3 6 1 4 1 5 1 8 1 9 2 4 6 1 5 1 8 1 9 2 5 6 1 8 1 9 YES 38 1 1 1 2 1 4 1 5 1 6 1 7 1 8 1 10 2 2 7 1 1 1 4 1 5 1 6 1 7 1 8 1 10 2 10 7 1 1 1 4 1 5 1 6 1 7 1 8 2 1 9 1 4 1 5 1 6 1 7 1 8 2 4 9 1 5 1 6 1 7 1 8 2 7 9 1 5 ...
result:
wrong answer you didn't find a solution but jury did (test case 63)