QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#111564#6502. Disjoint Set UnionExplodingKonjacWA 0ms3516kbC++146.0kb2023-06-07 16:24:122023-06-07 16:24:17

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-07 16:24:17]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3516kb
  • [2023-06-07 16:24:12]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
// #define OPENIOBUF

namespace FastIO
{

class FastIOBase
{
 protected:
#ifdef OPENIOBUF
	static const int BUFSIZE=1<<16;
	char buf[BUFSIZE+1];
	int buf_p=0;
#endif
	FILE *target;
	FastIOBase(FILE *f): target(f){}
	~FastIOBase()=default;
 public:
#ifdef OPENIOBUF
	virtual void flush()=0;
#endif
};

class FastOutput final: public FastIOBase
{
#ifdef OPENIOBUF
 public:
	inline void flush()
	{ fwrite(buf,1,buf_p,target),buf_p=0; }
#endif
 private:
	inline void __putc(char x)
	{
#ifdef OPENIOBUF
		if(buf[buf_p++]=x,buf_p==BUFSIZE) flush();
#else
		putc(x,target);
#endif
	}
	template<typename T>
	inline void __write(T x)
	{
		char stk[64],*top=stk;
		if(x<0) return __putc('-'),__write(-x);
		do *(top++)=x%10,x/=10; while(x);
		for(;top!=stk;__putc(*(--top)+'0'));
	}
 public:
	FastOutput(FILE *f=stdout): FastIOBase(f){}
#ifdef OPENIOBUF
	~FastOutput(){ flush(); }
#endif
	inline FastOutput &operator <<(char x)
	{ return __putc(x),*this; }
	inline FastOutput &operator <<(const char *s)
	{ for(;*s;__putc(*(s++)));return *this; }
	inline FastOutput &operator <<(const string &s)
	{ return (*this)<<s.c_str(); }
	template<typename T>
	inline enable_if_t<is_integral<T>::value,FastOutput&> operator <<(const T &x)
	{ return __write(x),*this; }
	template<typename ...T>
	inline void writesp(const T &...x)
	{ initializer_list<int>{(this->operator<<(x),__putc(' '),0)...}; }
	template<typename ...T>
	inline void writeln(const T &...x)
	{ initializer_list<int>{(this->operator<<(x),__putc('\n'),0)...}; }
	template<typename Iter>
	inline void writesp(Iter begin,Iter end)
	{ while(begin!=end) (*this)<<*(begin++)<<' '; }
	template<typename Iter>
	inline void writeln(Iter begin,Iter end)
	{ while(begin!=end) (*this)<<*(begin++)<<'\n'; }
}qout;

class FastInput final: public FastIOBase
{
#ifdef OPENIOBUF
 public:
	inline void flush()
	{ buf[fread(buf,1,BUFSIZE,target)]=EOF,buf_p=0; }
#endif
 private:
	bool __eof;
	inline char __getc()
	{
		if(__eof) return EOF;
#ifdef OPENIOBUF
		if(buf_p==BUFSIZE) flush();
		char ch=buf[buf_p++];
#else
		char ch=getc(target);
#endif
		return ~ch?ch:(__eof=true,EOF);
	}
	inline void __ungetc(char c)
	{
		__eof=false;
#ifdef OPENIOBUF
		buf_p--;
#else
		ungetc(c,target);
#endif
	}
 public:
	FastInput(FILE *f=stdin): FastIOBase(f),__eof(false)
#ifdef OPENIOBUF
	{ buf_p=BUFSIZE; }
	inline bool eof()const { return buf[buf_p]==EOF; }
#else
	{}
	inline bool eof()const { return feof(target); }
#endif
	inline char peek() { return __getc(); }
	explicit inline operator bool()const { return !__eof; }
	inline FastInput &operator >>(char &x)
	{ while(isspace(x=__getc()));return *this; }
	template<typename T>
	inline enable_if_t<is_integral<T>::value,FastInput&> operator >>(T &x)
	{
		char ch,sym=0;x=0;
		while(isspace(ch=__getc()));
		if(__eof) return *this;
		if(ch=='-') sym=1,ch=__getc();
		for(x=0;isdigit(ch);x=(x<<1)+(x<<3)+(ch^48),ch=__getc());
		return __ungetc(ch),sym?x=-x:x,*this;
	}
	inline FastInput &operator >>(char *s)
	{
		while(isspace(*s=__getc()));
		if(__eof) return *this;
		for(;!isspace(*s) && !__eof;*(++s)=__getc());
		return __ungetc(*s),*s='\0',*this;
	}
	inline FastInput &operator >>(string &s)
	{
		char str_buf[(1<<8)+1]={0},*p=str_buf;
		char *const buf_end=str_buf+(1<<8);
		while(isspace(*p=__getc()));
		if(__eof) return *this;
		for(s.clear(),p++;;p=str_buf)
		{
			for(;p!=buf_end && !isspace(*p=__getc()) && !__eof;p++);
			if(p!=buf_end) break;
			s.append(str_buf);
		}
		__ungetc(*p),*p='\0',s.append(str_buf);
		return *this;
	}
	template<typename ...T>
	inline void read(T &...x)
	{ initializer_list<int>{(this->operator>>(x),0)...}; }
	template<typename Iter>
	inline void read(Iter begin,Iter end)
	{ while(begin!=end) (*this)>>*(begin++); }
}qin;

} // namespace FastIO
using FastIO::qin,FastIO::qout;

using LL=long long;
using LD=long double;
using UI=unsigned int;
using ULL=unsigned long long;

#ifndef DADALZY
#define FILEIO(file) freopen(file".in","r",stdin),freopen(file".out","w",stdout)
#else
#define FILEIO(file)
#endif

int T,n,a[1005],b[1005],a2[1005],b2[1005];

int fa[1005];
inline int findFa(int x)
{ return x!=fa[x]?fa[x]=findFa(fa[x]):x; }
inline int merge(int x,int y)
{ return x=findFa(x),y=findFa(y),fa[x]=y,x!=y; }

vector<int> g[1005],ord;
int deg[1005],col[1005];
void topo()
{
	queue<int> q;
	for(int i=1;i<=n;i++)
		if(!deg[i] && a[i]==i)
			q.push(i),col[i]=i;
	while(!q.empty())
	{
		int u=q.front();
		ord.push_back(u);
		for(auto &v: g[u])
			if(!--deg[v])
				q.push(v),col[v]=col[u];
		q.pop();
	}
	reverse(ord.begin(),ord.end());
}

#define NO_IF(cond) { if(cond) { qout<<"NO\n";goto clear_all_data; } }
vector<tuple<int,int,int>> ans;

int main()
{
	qin>>T;
	while(T--)
	{
		qin>>n;
		qin.read(a+1,a+n+1);
		qin.read(b+1,b+n+1);
		iota(fa+1,fa+n+1,1);
		for(int i=1;i<=n;i++) NO_IF(b[i]!=i && !merge(i,b[i]));
		copy(a+1,a+n+1,fa+1);
		transform(fa+1,fa+n+1,a2+1,findFa);
		copy(b+1,b+n+1,fa+1);
		transform(fa+1,fa+n+1,b2+1,findFa);
		for(int i=1;i<=n;i++) NO_IF(b2[i]!=b2[a2[i]]);
		copy(a+1,a+n+1,fa+1);
		for(int i=1;i<=n;i++)
		{
			if(fa[i]==b[i]) continue;
			findFa(i);
			ans.emplace_back(1,i,0);
		}
		for(int i=1;i<=n;i++)
		{
			int x=fa[i],y=b[i];
			if(x==y || fa[y]!=y) continue;
			g[y].push_back(x),deg[x]++;
		}
		topo();
		for(int c=1;c<=n;c++)
		{
			int lst=0;
			for(auto &u: ord) if(col[u]==c)
			{
				if(lst) merge(lst,u),ans.emplace_back(2,lst,u);
				lst=u;
				for(int i=1;i<=n;i++)
				{
					if(fa[i]==b[i]) continue;
					findFa(i);
					ans.emplace_back(1,i,0);
				}
			}
		}
		for(int i=1;i<=n;i++) NO_IF(fa[i]!=b[i]);
		qout<<"YES\n";
		for(auto &[opt,x,y]: ans)
		{
			qout<<opt<<' ';
			if(opt==1) qout<<x<<'\n';
			else qout<<x<<' '<<y<<'\n';
		}
		clear_all_data:
		for(int i=1;i<=n;i++)
			g[i].clear(),deg[i]=col[i]=0;
		ans.clear(),ord.clear();
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 3516kb

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

wrong answer f[1] = 1, but expected 2 (test case 1)