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

貌似转成RMQ数据量小了有点慢。。。我79ms。。。 不知道用±1RMQ的O(n)方法会怎么样。。。

Posted by Debugcool at 2010-08-05 09:41:50 on Problem 1330
In Reply To: 最近一直挫败……好不容易用rmq一次ac了一题,纪念一下,帖下代码 Posted by:fyq at 2010-05-11 13:39:17
#include <cstdio>
#include <cstring>
#include <vector>
#include <cmath>

#define MIN(a,b) (a < b ? a : b)
#define MAX(a,b) (a > b ? a : b)

using namespace std;

const int SIZE = 20480;

int nv;
int elc;
bool used[SIZE];
int level[SIZE];
int euler[SIZE];
int first[SIZE];
int ances[SIZE];

int dp[SIZE][15];

vector<int> gmap[SIZE];

void fillTable(int n)
{
    for (int i = 0;i < n;i++)
        dp[i][0] = i;
    for (int j = 1;1 << j <= n;j++)
        for (int i = 0;i + (1 << j) - 1 < n;i++)
            if (level[dp[i][j-1]] < level[dp[i+(1<<(j-1))][j-1]])
                dp[i][j] = dp[i][j-1];
            else
                dp[i][j] = dp[i+(1<<(j-1))][j-1];
}

int rmq(int i,int j)
{
    int k = (int)floor(log(j-i+1)/log(2));
    return level[dp[i][k]] < level[dp[j-(1<<k)+1][k]] ? dp[i][k] : dp[j-(1<<k)+1][k];
}

void dfs(int u,int depth)
{
    level[elc] = depth;
    euler[elc++] = u;
    int size = gmap[u].size();
    for (int i = 0;i < size;i++)
    {
        dfs(gmap[u][i],depth + 1);
        level[elc] = depth;
        euler[elc++] = u;
    }
}

int main ()
{
    int x;
    int y;
    int cas;
    scanf("%d",&cas);
    for (int cc = 0;cc < cas;cc++)
    {
        memset(used,true,sizeof(used));

        scanf("%d",&nv);

        for (int i = 0;i < nv;i++)
            gmap[i].clear();

        for (int i = 0;i < nv - 1;i++)
        {
            scanf("%d %d",&x,&y);
            gmap[x-1].push_back(y-1);
            used[y-1] = false;
        }

        elc = 0;
        for (int i = 0;i < nv;i++)
            if (used[i])
            {
                dfs(i,0);
                break;
            }

        memset(used,false,sizeof(used));
        for (int i = 0;i < elc;i++)
            if (!used[euler[i]])
            {
                first[euler[i]] = i;
                used[euler[i]] = true;
            }

        fillTable(2 * nv - 1);

        scanf("%d %d",&x,&y);
        printf("%d\n",euler[rmq(MIN(first[x-1],first[y-1]),MAX(first[x-1],first[y-1]))] + 1);

    }
    return 0;
}

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