QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#112940#6502. Disjoint Set UnionjeffqiWA 1ms3396kbC++232.1kb2023-06-15 14:22:112023-06-15 14:22:12

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:22:12]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3396kb
  • [2023-06-15 14:22:11]
  • 提交

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;
		while (sz(q)) {
			int u = q.front(); q.pop(); p.eb(u);
			for (auto v:e[u]) {
				if (!(--d[v])) q.push(v);
			}
		}
		cout << sz(p) << '\n';
		for (auto v:p) {
			for (auto u:g[v]) if (dsu.fa[u] != ed.fa[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});
					}
				}
			}
		}
		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: 0
Wrong Answer
time: 1ms
memory: 3396kb

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:

3
YES
2
1 1
2 1 2
4
YES
9
1 2
1 3
1 4
2 3 2
1 2
1 3
1 4
2 2 1
1 3
5
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
6
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:

wrong answer Token parameter [name=token] equals to "3", doesn't correspond to pattern "(YES)|(NO)" (test case 1)