题目链接:https://www.luogu.org/problemnew/show/P1160
题意不难,难的是怎么模拟成功。
就想办法模拟出这个操作即可,我用的是结构体内存left和right+一个标记f,模拟链表指向和删除完成的。
1 #include <iostream> 2 #include <string> 3 #include <algorithm> 4 #include <iomanip> 5 #include <cstdio> 6 #include <cstring> 7 #include <cmath> 8 using namespace std; 9 typedef long long ll; 10 typedef unsigned long long ull; 11 const int maxn=1e6+5; 12 int n,m; 13 struct px 14 { 15 int left; 16 int right; 17 int f; 18 }T[maxn]; 19 20 int main() 21 { 22 ios::sync_with_stdio(false); cin.tie(0); 23 24 cin>>n; 25 T[1].f=1; 26 for(int i=2;i<=n;i++) 27 { 28 int k,p; 29 cin>>k>>p; 30 31 T[i].f=1; 32 if(p==0)//往左边插 33 { 34 T[i].left=T[k].left; 35 T[i].right=k; 36 T[T[k].left].right=i; 37 T[k].left=i; 38 } 39 else//往右边插 40 { 41 T[i].right=T[k].right; 42 T[i].left=k; 43 T[T[k].right].left=i; 44 T[k].right=i; 45 } 46 } 47 cin>>m; 48 while(m--) 49 { 50 int x; 51 cin>>x; 52 T[x].f=0; 53 } 54 55 56 int L=1; 57 while(T[L].left) L=T[L].left; 58 int R=L; 59 while(R) 60 { 61 if(T[R].f) cout<<R<<' '; 62 R=T[R].right; 63 } 64 cout<<endl; 65 66 67 return 0; 68 }
完。