用队列做的但是超时了,求助大佬!
查看原帖
用队列做的但是超时了,求助大佬!
800751
acmwriter楼主2023/5/21 19:42
#include<bits/stdc++.h>
using namespace std;
int main(){
	int n;
	cin>>n;
	queue<int>s;
	s.push(1);
	int c[100005];
	int d=1;
	for(int i=2;i<=n;i++){
		int a,b;
		cin>>a>>b;
		for(int j=1;j<=d;j++){
			c[j]=s.front();
			s.pop();
		}
		for(int j=1;j<=d;j++){
			if(b==0&&c[j]==a)s.push(i);
			s.push(c[j]);
			if(b==1&&c[j]==a)s.push(i);
		}
		d++;
	}
	int m,t;
	cin>>m;
	for(int i=0;i<m;i++){
		cin>>t;
		int flag=0;
		for(int j=1;j<=n;j++){
			c[j]=s.front();
			s.pop();
		}
		for(int j=1;j<=n;j++){
			if(c[j]==t){
				flag=1;
				continue;
			}
			s.push(c[j]);
		}
		if(flag==1)n--;
	}
	for(int i=1;i<=n;i++){
		cout<<s.front()<<" ";
		s.pop();
	}
	return 0;
}
2023/5/21 19:42
加载中...