求大佬帮忙看看哪里有问题
查看原帖
求大佬帮忙看看哪里有问题
1382763
xuzongcheng楼主2024/7/22 22:24
#include<iostream>
using namespace std;
struct link{
	int pre;
	int next;
	link():pre(0),next(0){}
};
 int main(){
    int n,head;
    cin>>n;
    if(n==1){
    	int m;
    	cin>>m;
    	while(m--){
    		int temp;
    		cin>>temp;
    		if(temp==1)return 0;
		}
		cout<<1;
    	return 0;
	}
    link a[n+1];
    head=1;
    for(int i=2;i<=n;i++){
    	int stu,p;
    	cin>>stu>>p;
    	
    	if(p==0){
    		
    		if(a[stu].pre!=0){
    			 
    			a[i].pre=a[stu].pre;
    			a[a[stu].pre].next=i;
			}
			else {
				head=i;
			}
    		a[stu].pre=i;
    		a[i].next=stu; 
    		 
		}
		else {
			 
			if(a[stu].next!=0){
				 
    			a[i].next=a[stu].next;
    			a[a[stu].next].pre=i;
			}
			a[stu].next=i;
			a[i].pre=stu;
			
		}
	}
	int m;
	cin>>m;
	while(m--){
		int temp;
		cin>>temp;
		a[a[temp].pre].next=a[temp].next;
		a[temp].pre=a[temp].next=-1;
	}
	while(head!=0){
		cout<<head<<" ";
		head=a[head].next;
	}
     return 0;
 }
2024/7/22 22:24
加载中...