蒟蒻70分求助
查看原帖
蒟蒻70分求助
306954
芊枫Thomitics楼主2020/8/13 23:23

2,9,10点都WA了

#include <bits/stdc++.h>

using namespace std;

inline long long read()
{
    long long x = 0;
    int f = 1;
    char ch = getchar();
    while (ch < '0' || ch>'9')
    {
        if (ch == '-')
            f = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9')
    {
        x = (x << 1) + (x << 3) + (ch ^ 48);
        ch = getchar();
    }
    return x * f;
}
void write(const long long& x)
{
    if (!x)
    {
        putchar('0');
        return;
    }
    char f[100];
    long long tmp = x;
    if (tmp < 0)
    {
        tmp = -tmp;
        putchar('-');
    }
    long long s = 0;
    while (tmp > 0)
    {
        f[s++] = tmp % 10 + '0';
        tmp /= 10;
    }
    while (s > 0)
    {
        putchar(f[--s]);
    }
}
inline double dread()
{
    double r;
    double x=0, t=0;
    int s=0, f=1;
    char c=getchar();
    for (;!isdigit(c);c=getchar())
    {
        if (c=='-')
        {
            f=-1;
        }
        if (c=='.')
        {
            goto readt;
        }
    }
    for (;isdigit(c)&&c!='.';c=getchar())
    {
        x=x*10+c-'0';
    }
    readt:
    for (;c=='.';c=getchar());
    for (;isdigit(c);c=getchar())
    {
        t=t*10+c-'0';
        ++s;
    }
    r=(x+t/pow(10, s))*f;
    return r;
}

inline void dwrite(long long x)
{
    if (x == 0)
    {
        putchar(48);
        return;
    }
    int bit[20], p = 0, i;
    for (; x; x /= 10)
        bit[++p] = x % 10;
    for (i = p; i > 0; --i)
        putchar(bit[i] + 48);
}
inline void write(double x, int k)
{
    static int n = pow(10, k);
    if (x == 0)
    {
        putchar('0');
        putchar('.');
        for (int i = 1; i <= k; ++i)
            putchar('0');
        return;
    }
    if (x < 0) putchar('-'), x = -x;
    long long y = (long long)(x * n) % n;
    x = (long long)x;
    dwrite(x), putchar('.');
    int bit[10], p = 0, i;
    for (; p < k; y /= 10)
        bit[++p] = y % 10;
    for (i = p; i > 0; i--)
        putchar(bit[i] + 48);
}

int head[500090];
int nxt[500090];
int to[500090];
int totNODE;
int totQUEST;
int ROOT;
int fa[500090][23];
int deepth[500090];
int logging[500090];
int cnt_edges=1;

void add_edge(int x,int y)
{
    nxt[++cnt_edges]=head[x];
    head[x]=cnt_edges;
    to[cnt_edges]=y;
}

void get_log()
{
    for (int i = 1; i <= 500030; i++)
    {
        logging[i]=logging[i-1]+(1<<logging[i-1]==i);
    }
    /*
    for (int i = 1; i <= 500030; i++)
    {
        logging[i]-=1;
    }
     */
}

void DFS(int now, int father)
{
    fa[now][0]=father;
    deepth[now]=deepth[father]+1;
    for (int i = 1; i<=logging[deepth[now]]; i++)
    {
        fa[now][i]=fa[fa[now][i-1]][i-1];
    }
    for (int i = head[now]; i ; i=nxt[i])
    {
        if(to[i]!=father)
        {
            DFS(to[i],now);
        }
    }
}

int correct_LCA(int x,int y)
{
    if (deepth[x]<deepth[y])
    {
        swap(x,y);
    }
    while (deepth[x]>deepth[y])
    {
        x=fa[x][logging[deepth[x]-deepth[y]]-1];
    }
    if (x==y)
    {
        return x;
    }
    for (int i = logging[deepth[x]]-1; i >= 0; i--)
    {
        if (fa[x][i]!=fa[y][i])
        {
            x=fa[x][i];
            y=fa[y][i];
        }
    }
    return fa[x][0];
}

int main()
{
    totNODE=read();
    totQUEST=read();
    ROOT=read();
    int tempx,tempy;
    get_log();
    for (int i = 1; i <= totNODE-1; i++)
    {
        tempx=read();
        tempy=read();
        add_edge(tempx,tempy);
        add_edge(tempy,tempx);
    }
    DFS(ROOT,0);
    for (int i = 1; i <= totQUEST; i++)
    {
        write(correct_LCA(read(),read()));
        putchar('\n');
    }
    return 0;
}//LikiBlaze Code
2020/8/13 23:23
加载中...