AC代码:
#include<stdio.h> int judge(int left,int right); struct Que{ int num; int pre; int next; }q[1010]; int main() { int n,m,x,y,i,t,cnt=0,flag_x,flag_y; scanf("%d %d",&n,&m); for(i=1;i<=n;i++) scanf("%d",&q[i].num); for(i=1;i<n;i++){ q[q[i].num].next=q[i+1].num; q[q[i+1].num].pre=q[i].num; } while (m--){ scanf("%d %d",&x,&y); for (i=1;i<=n;i++){ if (q[i].num==x) flag_x=i; if (q[i].num==y) flag_y=i; } if(flag_x>flag_y){ t=x; x=y; y=t; } if(judge(x,y)!=0){ q[q[x].pre].next=0; q[x].pre=0; q[q[y].next].pre=0; q[y].next=0; } } for(i=1;i<=n;i++) if(q[q[i].num].next==0) cnt++; printf("%d\n",cnt); return 0; } int judge(int left,int right){ for(int k=left;k!=0; k=q[k].next) if(k==right) return 1; return 0; }