QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#112938#6502. Disjoint Set UnionjeffqiWA 114ms3476kbC++232.1kb2023-06-15 14:14:032023-06-15 14:14:06

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-06-15 14:14:06]
  • 评测
  • 测评结果:WA
  • 用时:114ms
  • 内存:3476kb
  • [2023-06-15 14:14:03]
  • 提交

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),g(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);
					g[x].eb(dsu.find(i));
				}
			}
		}
		queue<int> q;
		rep(i,0,n) {
			if (!d[i]) q.emplace(i);
		}
		vi p,rk(n);
		while (sz(q)) {
			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];
		};
		for (auto v:p) if (sz(g[v])) {
			int u = *max_element(all(g[v]),cmp);
			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});
				}
			}
		}
		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: 1ms
memory: 3476kb

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: 114ms
memory: 3432kb

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
NO
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
NO
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
NO
NO
YES
11
1 2
1 3
1 4
1 5
2 4 3
1 2
1 3
1 5
2 3 1
1 2
1 5
NO
NO
NO
YES
1
1 4
NO
YES
2
1 4
2 4 3
YES
2
1 4
2 4 5...

result:

wrong answer you didn't find a solution but jury did (test case 3)