原来一直超时 看了一个博客的程序 用了map 修改了我的程序 终于对了
发现STL真是个好东西
下面是该博客
http://blog.csdn.net/jmlikun/article/details/49975695
#include<vector> #include<iostream> #include<algorithm> #include<cstdio> #include<cstring> #include<cmath> #include<cstdlib> #include<queue> #include<map> #include<string> #include<set> #include<stack> using namespace std; #define INF 100000000 #define min(a,b) a<b?a:b #define maxsize 10001 map<int, bool> e; struct node{ int value; int next; int location; }a[100000]; void Search(node a[],int root) { vector<struct node> v; int index=root; int next,t; t=index; while(index!=-1) { if(e[abs(a[index].value)]) { a[t].next=a[index].next; v.push_back(a[index]); } else { e[abs(a[index].value)]=true; t=index; } index=a[index].next; } index=root; while(index!=-1) { printf("%05d %d",index,a[index].value); if(a[index].next==-1) printf(" -1\n"); else printf(" %05d\n",a[index].next); index=a[index].next; } for( int i=0;i<v.size();i++) { if(i==0) { printf("%05d %d",v[i].location,v[i].value); } else { printf(" %05d\n",v[i].location); printf("%05d %d",v[i].location,v[i].value); } if(i==v.size()-1) printf(" -1\n"); } } int main() { int root,n; int index; scanf("%d%d",&root,&n); for(int i=0;i<n;i++) { scanf("%d",&index); scanf("%d%d",&a[index].value,&a[index].next); a[index].location=index; } Search(a,root); return 0; }