Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
非递归版终于过了,find函数~一直对递归没啥好感的结果就是递归1y, 非递归wa无数才y... 给个非递归find函数的写法,有更好写法的欢迎讨论~ int find(int x) { int t = x, tmp; int l=0, fx = x, i; while (fx!=f[fx]) { buf[l++] = dist[fx]; fx = f[fx]; } for (i=l-1; i>=1; --i) { buf[i-1] += buf[i]; } i=0; while (x!=f[x]) { dist[x] = buf[i++]; x = f[x]; } while (t!=x) { tmp = f[t]; f[t] = x; t = tmp; } return x; // 下边是递归版~还是递归的简洁啊,满满都是泪 // int tmp = f[x]; // if (x!=f[x]) // { // f[x] = find(f[x]); // dist[x] += dist[tmp]; // } // return f[x]; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator