Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

非递归版终于过了,find函数~

Posted by openthedoor at 2013-09-08 03:23:00 on Problem 1988
一直对递归没啥好感的结果就是递归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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator