QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#177205#6396. Puzzle: Kusabimendicillin2#WA 31ms6648kbC++174.8kb2023-09-12 17:42:412023-09-12 17:42:42

Judging History

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

  • [2023-09-12 17:42:42]
  • 评测
  • 测评结果:WA
  • 用时:31ms
  • 内存:6648kb
  • [2023-09-12 17:42:41]
  • 提交

answer

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

template <class T> int sz(T&& a) { return int(size(forward<T>(a))); }

template <class T> using vc = vector<T>;
template <class T> using vvc = vc<vc<T>>;

using ll = int64_t;
using vi = vc<int>;

template <class F>
struct ycr {
	F f;
	
	template <class T>
	explicit ycr(T&& f_) : f(forward<T>(f_)) {}

	template <class... Args>
	decltype(auto) operator()(Args&&... args) {
		return f(ref(*this), forward<Args>(args)...);
	}
};
template <class F>
decltype(auto) yc(F&& f) {
	return ycr<decay_t<F>>(forward<F>(f));
}

const int N=1e5+5;
int n;
int p[N],fa[N],op[N];
struct node{
	int to,last;
}tree[N*2];
int ss=1,head[N];
inline void add(int from,int to)
{
	tree[++ss].to=to;
	tree[ss].last=head[from];
	head[from]=ss;
}

int re[N];
int len1=0, len2=0, len3=0;
int re1[N],re2[N],re3[N],deep[N];
bool pre[N],suf[N];

int ans[N];

inline bool cmp(int x,int y)
{
	return deep[x]<deep[y];
}

inline void dfs(int from,int x)
{
	deep[x]=deep[from]+1;
	for(int i=head[x];i;i=tree[i].last)
	{
		int k=tree[i].to;
		dfs(x,k);
	}
	//cout<<x<<" "<<"yes"<<endl;
	len1=len2=len3=0;
	for(int i=head[x];i;i=tree[i].last)
	{
		int k=tree[i].to;
		if(re[k]==0) continue;
		else if(op[re[k]]==1) re1[++len1]=re[k];
		else if(op[re[k]]==2) re2[++len2]=re[k];
		else re3[++len3]=re[k];
	}
	if(op[x]==1) re1[++len1]=x;
	else if(op[x]==2) re2[++len2]=x;
	else if(op[x]==3) re3[++len3]=x; 
	sort(re1+1,re1+len1+1,cmp);
	sort(re2+1,re2+len2+1,cmp);
	sort(re3+1,re3+len3+1,cmp);
	vector<int> rem;
	//if(op[x]==0)
	//{
		// tong
		{
			vector<int> bads;
			for (int st = 1; st <= len2; ) {
				int en = st+1;
				while (en <= len2 && deep[re2[st]] == deep[re2[en]]) en++;
				if ((en - st) % 2 == 1) {
					bads.push_back(re2[st]);
					for (int i = st+1; i+1 < en; i += 2) {
						ans[re2[i]] = re2[i+1];
						ans[re2[i+1]] = re2[i];
					}
				} else {
					for (int i = st; i+1 < en; i += 2) {
						ans[re2[i]] = re2[i+1];
						ans[re2[i+1]] = re2[i];
					}
				}
				st = en;
			}
			if (bads.size() > 1) {
				cout<<"NO"<<endl;
				exit(0);
			}
			if (bads.size() == 1) {
				rem.push_back(bads[0]);
			}
		}

		// chang duan
		{

			if(abs(len1-len3)>1)
			{
				cout<<"NO"<<endl;
				exit(0);
			}
			if(len1==len3)
			{
				for(int i=1;i<=len1;i++)
					if(deep[re1[i]]>=deep[re3[i]])
					{
						cout<<"NO"<<endl;
						exit(0);
					}
					else
					{
						ans[re1[i]]=re3[i];
						ans[re3[i]]=re1[i];
					}
			}
			else if(len1==len3+1)
			{
				pre[0]=suf[len1+1]=true;
				for(int i=1;i<=len3;i++) 
					if(deep[re1[i]]<deep[re3[i]])
						pre[i]=pre[i-1];
					else
						pre[i]=false;
				for(int i=len1;i>=2;i--)
				{
					int t=len1-i+1;
					if(deep[re1[i]]<deep[re3[len3-t+1]])
						suf[i]=suf[i+1];
					else 	
						suf[i]=false;
				}
				int f = -1;
				for(int i=1;i<=len1;i++)
				{
					if(pre[i-1] && suf[i+1])
					{
						f = re1[i];
						break;
					} 
				}
				if(f == -1) 
				{
					cout<<"NO"<<endl;
					exit(0);
				}
				rem.push_back(f);
				int t=1;
				for(int i=1;i<=len3;i++)
				{
					if(re1[t]==f) t++;
					ans[re1[t]]=re3[i];
					ans[re3[i]]=re1[t];
				}
			}
			else
			{
				pre[0]=suf[len3+1]=true;
				for(int i=1;i<=len1;i++) 
					if(deep[re1[i]]<deep[re3[i]])
						pre[i]=pre[i-1];
					else
						pre[i]=false;
				for(int i=len3;i>=2;i--)
				{
					int t=len3-i+1;
					if(deep[re1[len1-t+1]]<deep[re3[i]])
						suf[i]=suf[i+1];
					else 	
						suf[i]=false;
				}
				int f = -1;
				for(int i=len3;i>=1;i--)
				{
					if(pre[i-1] && suf[i+1])
					{
						f = re3[i];
						break;
					} 
				}
				if(f == -1)
				{
					cout<<"NO"<<endl;
					exit(0);
				}
				rem.push_back(f);
				int t=1;
				for(int i=1;i<=len1;i++)
				{
					if(re3[t]==f) t++;
					ans[re1[i]]=re3[t];
					ans[re3[t]]=re1[i];
				}
			}	
		}

		if (rem.size() > 1) {
			cout << "NO" << endl;
			exit(0);
		}
		if (rem.size() == 1) {
			re[x] = rem[0];
		} else {
			re[x] = 0;
		}
		//cerr << "x=" << x << ", re=" << re[x] << endl;
	//}
	// else if(op[x]==1)
	// {
	// 	if(len1 || len2 || len3>1)
	// 	{
	// 		cout<<"NO"<<endl;
	// 		exit(0);
	// 	}
	// 	if(len3==0) re[x]=x;
	// 	else ans[x]=re3[1], ans[re3[1]]=x;
	// }
	// else if(op[x]==2)
	// {
	// 	if(len1 || len3 || len2)
	// 	{
	// 		cout<<"NO"<<endl;
	// 		exit(0);
	// 	}
	// 	re[x]=x;
	// }
	// else if(op[x]==3)
	// {
	// 	if(len1 || len3 || len2)
	// 	{
	// 		cout<<"NO"<<endl;
	// 		exit(0);
	// 	}
	// 	re[x]=x;
	// }
}

int main() {
	ios_base::sync_with_stdio(false), cin.tie(nullptr);
	cout << fixed << setprecision(20);
	cin>>n; int id;
	string s;
	for(int i=2;i<=n;i++)
	{
		cin>>id>>fa[i]>>s;
		add(fa[i],i);
		if(s=="Duan") op[i]=1;
		else if(s=="Tong")  op[i]=2;
		else if(s=="Chang") op[i]=3;
	}
	dfs(0,1);
	if(re[1])
	{
		cout<<"NO"<<endl;
		exit(0);
	}
	cout<<"YES"<<endl;
	for(int i=1;i<=n;i++)
	{
		if(i>ans[i]) continue;
		cout<<i<<" "<<ans[i]<<endl;
	}
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3596kb

input:

8
2 1 -
3 1 -
4 2 Tong
5 2 Tong
6 3 Duan
7 3 -
8 7 Chang

output:

YES
4 5
6 8

result:

ok Correct.

Test #2:

score: 0
Accepted
time: 0ms
memory: 3492kb

input:

10
2 1 Duan
3 2 Duan
4 2 -
5 4 Chang
6 2 Chang
7 1 Duan
8 6 Tong
9 6 Tong
10 3 Chang

output:

YES
2 6
3 10
5 7
8 9

result:

ok Correct.

Test #3:

score: 0
Accepted
time: 1ms
memory: 5600kb

input:

2
2 1 Tong

output:

NO

result:

ok Correct.

Test #4:

score: -100
Wrong Answer
time: 31ms
memory: 6648kb

input:

100000
2 1 Duan
3 1 Duan
4 3 -
5 4 Duan
6 3 -
7 4 Duan
8 4 -
9 8 -
10 7 Duan
11 9 -
12 7 Duan
13 7 Duan
14 8 Duan
15 13 -
16 10 Duan
17 11 Duan
18 12 -
19 1 Duan
20 5 Duan
21 4 Duan
22 14 Duan
23 16 -
24 22 Duan
25 16 Duan
26 13 -
27 13 -
28 17 -
29 5 Duan
30 22 -
31 23 -
32 9 Duan
33 5 -
34 30 Duan...

output:

YES
2 38925
3 17507
5 16234
7 91948
10 75059
12 62351
13 43170
14 879
16 92645
17 78130
19 92154
20 693
21 3946
22 32273
24 18570
25 3211
29 86603
32 92026
34 73620
35 2350
36 7180
38 9297
39 181
40 99610
41 97678
43 2826
47 9478
48 57024
50 58972
51 55750
55 2855
56 1008
57 27675
58 89545
59 5527
6...

result:

wrong answer Vertex 67088 used twice.