QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#138575#6502. Disjoint Set Unionnikolapesic2802WA 179ms3872kbC++146.8kb2023-08-11 23:31:412023-08-11 23:31:44

Judging History

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

  • [2023-08-11 23:31:44]
  • 评测
  • 测评结果:WA
  • 用时:179ms
  • 内存:3872kb
  • [2023-08-11 23:31:41]
  • 提交

answer

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>

#define ll long long
#define pb push_back
#define f first
#define s second
#define sz(x) (int)(x).size()
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define ios ios_base::sync_with_stdio(false);cin.tie(NULL)
#define ld long double
#define li __int128

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>; ///find_by_order(),order_of_key()
template<int D, typename T>struct vec : public vector<vec<D - 1, T>> {template<typename... Args>vec(int n = 0, Args... args) : vector<vec<D - 1, T>>(n, vec<D - 1, T>(args...)) {}};
template<typename T>struct vec<1, T> : public vector<T> {vec(int n = 0, T val = T()) : vector<T>(n, val) {}};
template<class T1,class T2> ostream& operator<<(ostream& os, const pair<T1,T2>& a) { os << '{' << a.f << ", " << a.s << '}'; return os; }
template<class T> ostream& operator<<(ostream& os, const vector<T>& a){os << '{';for(int i=0;i<sz(a);i++){if(i>0&&i<sz(a))os << ", ";os << a[i];}os<<'}';return os;}
template<class T> ostream& operator<<(ostream& os, const deque<T>& a){os << '{';for(int i=0;i<sz(a);i++){if(i>0&&i<sz(a))os << ", ";os << a[i];}os<<'}';return os;}
template<class T> ostream& operator<<(ostream& os, const set<T>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T> ostream& operator<<(ostream& os, const set<T,greater<T> >& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T> ostream& operator<<(ostream& os, const multiset<T>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T> ostream& operator<<(ostream& os, const multiset<T,greater<T> >& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T1,class T2> ostream& operator<<(ostream& os, const map<T1,T2>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
int ri(){int x;scanf("%i",&x);return x;}
void rd(int&x){scanf("%i",&x);}
void rd(long long&x){scanf("%lld",&x);}
void rd(double&x){scanf("%lf",&x);}
void rd(long double&x){scanf("%Lf",&x);}
void rd(string&x){cin>>x;}
void rd(char*x){scanf("%s",x);}
template<typename T1,typename T2>void rd(pair<T1,T2>&x){rd(x.first);rd(x.second);}
template<typename T>void rd(vector<T>&x){for(T&p:x)rd(p);}
template<typename C,typename...T>void rd(C&a,T&...args){rd(a);rd(args...);}
//istream& operator>>(istream& is,__int128& a){string s;is>>s;a=0;int i=0;bool neg=false;if(s[0]=='-')neg=true,i++;for(;i<s.size();i++)a=a*10+s[i]-'0';if(neg)a*=-1;return is;}
//ostream& operator<<(ostream& os,__int128 a){bool neg=false;if(a<0)neg=true,a*=-1;ll high=(a/(__int128)1e18);ll low=(a-(__int128)1e18*high);string res;if(neg)res+='-';if(high>0){res+=to_string(high);string temp=to_string(low);res+=string(18-temp.size(),'0');res+=temp;}else res+=to_string(low);os<<res;return os;}

const int N=1005;
vector<set<int>> decica(N);
vector<int> par(N),trebapar(N);
vector<pair<int,int>> ops;
vector<set<int>> trd(N);
vector<int> ordering;
void dfs(int tr){
	for(auto p:decica[tr]){
		dfs(p);
	}
	if(sz(decica[tr]))
		ordering.pb(tr);
}
void test(int ind,int tt){
	ordering.clear();
	ops.clear();
	int n=ri();
	for(int i=0;i<n;i++){
		decica[i].clear();
		trd[i].clear();
	}
	for(int i=0;i<n;i++){
		int a=ri()-1;
		trebapar[i]=a;
		if(a!=i){
			decica[a].insert(i);
		}
	}
	for(int i=0;i<n;i++){
		if(trebapar[i]==i){
			dfs(i);
		}
	}
	vector<int> pars;
	for(int i=0;i<n;i++){
		par[i]=ri()-1;
		if(par[i]==i){
			pars.pb(i);
		}
		if(par[i]!=i)
			trd[par[i]].insert(i);
	}
	/*if(ind==135){
		printf("%i\n",n);
		for(int i=0;i<n;i++){
			printf("%i ",trebapar[i]+1);
		}
		printf("\n");
		for(int i=0;i<n;i++){
			printf("%i ",par[i]+1);
		}
		printf("\n");
	}*/
	while(sz(pars)){
		int tr=pars.back();
		pars.pop_back();
		//printf("Procesiram %i\n",tr);
		//cout << "Pocetni " << trd[tr] << endl;
		// satisfy all child conditions with find
		for(auto i:ordering){
			if(trd[tr].find(i)==trd[tr].end()){
				continue;
			}
			for(auto p:decica[i]){
				if(trd[tr].find(p)!=trd[tr].end()){
					trd[tr].erase(p);
					par[p]=i;
					trd[i].insert(p);
					ops.pb({-1,p});
				}
			}
		}
		vector<int> sa;
		for(auto p:trd[tr]){
			if(sz(decica[p])){
				sa.pb(p);
			}
		}
		//cout << "Sa" << sa << endl;
		for(auto p:sa){
			for(auto d:decica[p]){
				if(trd[tr].find(d)!=trd[tr].end()){
					trd[tr].erase(d);
					par[d]=p;
					trd[p].insert(d);
					ops.pb({-1,d});
				}
				for(auto o:decica[d]){
					if(trd[tr].find(o)!=trd[tr].end()){
						trd[tr].erase(o);
						par[o]=p;
						trd[p].insert(o);
						ops.pb({-1,o});
					}
				}
			}
		}
		vector<int> svi;
		for(auto p:trd[tr]){
			svi.pb(p);
		}
		//cout << trd[tr] << endl;
		for(auto p:svi){
			queue<int> q;
			q.push(p);
			while(q.size()){
				int trr=q.front();
				q.pop();
				for(auto d:decica[trr]){
					if(trd[tr].find(d)!=trd[tr].end()){
						trd[tr].erase(d);
						par[d]=p;
						trd[p].insert(d);
						ops.pb({-1,d});
					}
				}
				for(auto d:trd[trr]){
					q.push(d);
				}
			}
		}
		//cout << trd[tr] << endl;
		vector<int> bez;
		for(auto p:trd[tr]){
			if(!sz(decica[p])&&decica[tr].find(p)==decica[tr].end()){
				bez.pb(p);
			}
		}
		for(int i=1;i<sz(bez);i++){
			trd[tr].erase(bez[i]);
			ops.pb({-1,bez[i]});
			par[bez[i]]=bez[0];
			trd[bez[0]].insert(bez[i]);
		}
		if(sz(bez)){
			vector<int> saa;
			for(auto p:trd[tr]){
				if(sz(decica[p])&&decica[tr].find(p)==decica[tr].end()){
					saa.pb(p);
				}
			}
			for(auto p:saa){
				trd[tr].erase(p);
				ops.pb({-1,p});
				par[p]=bez[0];
				trd[bez[0]].insert(p);
			}
		}
		//cout << trd[tr] << endl;
		vector<int> del;
		for(auto p:trd[tr]){
			if(decica[tr].find(p)==decica[tr].end()){
				ops.pb({p,tr});
				par[p]=p;
				del.pb(p);
				pars.pb(p);
			}
		}
		for(auto p:del){
			trd[tr].erase(p);
		}
	}
	/*for(int i=0;i<n;i++){
		printf("%i ",par[i]);
	}
	printf("\n");*/
	bool isti=1;
	for(int i=0;i<n;i++){
		if(par[i]!=trebapar[i]){
			isti=0;
			break;
		}
	}
	//if(tt<100){
		if(!isti){
			printf("NO\n");
		}
		else{
			printf("YES\n%i\n",sz(ops));
			reverse(all(ops));
			for(auto p:ops){
				if(p.f==-1){
					printf("1 %i\n",p.s+1);
				}
				else{
					printf("2 %i %i\n",p.f+1,p.s+1);
				}
			}
		}
	//}
}
int main()
{
	int t=ri();
	for(int ind=0;ind<t;ind++){
		test(ind,t);
	}
	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 3872kb

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

result:

ok good! (YES count = 4, NO count = 1) (5 test cases)

Test #2:

score: -100
Wrong Answer
time: 179ms
memory: 3828kb

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
2
2 1 2
1 5
YES
0
YES
6
2 3 2
2 2 4
1 3
1 5
2 4 1
1 5
YES
2
2 3 2
1 5
YES
0
YES
2
2 2 3
2 1 5
YES
4
2 3 1
2 5 2
2 2 4
1 5
YES
1
2 5 2
YES
1
2 2 3
YES
3
2 1 2
2 3 4
1 5
YES
1
2 1 5
YES
4
2 1 4
2 4 5
1 1
1 3
YES
4
2 4 3
2 3 5
1 4
2 5 1
YES
5
2 4 3
1 2
2 3 1
1 2
1 5
YES
7
2 1 3
2 3 2
1 1
2 2 5
1 1
...

result:

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