提交记录 7314


用户 题目 状态 得分 用时 内存 语言 代码长度
ouuan noip18f. 【NOIP2018】保卫王国 Accepted 100 163.285 ms 93692 KB C++ 5.10 KB
提交时间 评测时间
2019-01-25 08:29:04 2020-08-01 01:02:58
#include <iostream>
#include <cstdio>
#include <cctype>
#include <algorithm>

using namespace std;

int read()
{
int out=0;
char c;
while (!isdigit(c=getchar()));
for (;isdigit(c);c=getchar()) out=out*10+c-'0';
return out;
}

const int N=100010;
const long long INF=1e10;

void dfs1(int u);
void dfs2(int u);
void add(int u,int v);

int head[N],nxt[N<<1],to[N<<1],cnt;
long long n,m,p[N],f[N][2],g[N][2],bz[N][20][2][2],fa[N][20],dep[N],ans[2][2],anss[2][2][2],ansss;
char type[20];

int main()
{
    int i,u,v,a,b,x,y,cur=0;
    
n=read();m=read();
    scanf("%s",type);
    
    for (i=1;i<=n;++i) p[i]=read();
    
    for (i=1;i<n;++i)
    {
        u=read();
v=read();
        add(u,v);
        add(v,u);
    }
    
    fa[1][0]=1;
    
    dfs1(1);
    dfs2(1);
    
    for (i=1;i<=16;++i)
    {
        for (u=1;u<=n;++u)
        {
            fa[u][i]=fa[fa[u][i-1]][i-1];
            bz[u][i][0][1]=min(bz[u][i-1][0][0]+bz[fa[u][i-1]][i-1][0][1],bz[u][i-1][0][1]+bz[fa[u][i-1]][i-1][1][1]);
            bz[u][i][1][0]=min(bz[u][i-1][1][0]+bz[fa[u][i-1]][i-1][0][0],bz[u][i-1][1][1]+bz[fa[u][i-1]][i-1][1][0]);
            bz[u][i][1][1]=min(bz[u][i-1][1][0]+bz[fa[u][i-1]][i-1][0][1],bz[u][i-1][1][1]+bz[fa[u][i-1]][i-1][1][1]);
            bz[u][i][0][0]=min(bz[u][i-1][0][0]+bz[fa[u][i-1]][i-1][0][0],bz[u][i-1][0][1]+bz[fa[u][i-1]][i-1][1][0]);
        }
    }
    
    while (m--)
    {
        a=read();
x=read();
b=read();
y=read();
        u=a;
        v=b;
        if (dep[u]<dep[v])
        {
            swap(u,v);
            swap(a,b);
            swap(x,y);
        }
        ans[cur][x]=f[u][x];
        ans[cur][x^1]=INF;
        for (i=16;i>=0;--i)
        {
            if ((dep[u]-dep[v])&(1<<i))
            {
                cur^=1;
                ans[cur][0]=min(bz[u][i][0][0]+ans[cur^1][0],bz[u][i][1][0]+ans[cur^1][1]);
                ans[cur][1]=min(bz[u][i][0][1]+ans[cur^1][0],bz[u][i][1][1]+ans[cur^1][1]);
                u=fa[u][i];
            }
        }
        if (u==v)
        {
            if (ans[cur][y]+g[u][y]>=INF)
            {
                puts("-1");
                continue;
            }
            printf("%lld\n",ans[cur][y]+g[u][y]);
            continue;
        }
        anss[cur][0][y]=ans[cur][0]+f[v][y];
        anss[cur][1][y]=ans[cur][1]+f[v][y];
        anss[cur][0][y^1]=anss[cur][1][y^1]=INF;
        for (i=16;i>=0;--i)
        {
            if (fa[u][i]!=fa[v][i])
            {
                cur^=1;
                anss[cur][0][0]=min(min(bz[u][i][0][0]+bz[v][i][0][0]+anss[cur^1][0][0],
                                        bz[u][i][1][0]+bz[v][i][0][0]+anss[cur^1][1][0]),
                                    min(bz[u][i][0][0]+bz[v][i][1][0]+anss[cur^1][0][1],
                                        bz[u][i][1][0]+bz[v][i][1][0]+anss[cur^1][1][1]));
                anss[cur][0][1]=min(min(bz[u][i][0][0]+bz[v][i][0][1]+anss[cur^1][0][0],
                                        bz[u][i][1][0]+bz[v][i][0][1]+anss[cur^1][1][0]),
                                    min(bz[u][i][0][0]+bz[v][i][1][1]+anss[cur^1][0][1],
                                        bz[u][i][1][0]+bz[v][i][1][1]+anss[cur^1][1][1]));
                anss[cur][1][0]=min(min(bz[u][i][0][1]+bz[v][i][0][0]+anss[cur^1][0][0],
                                        bz[u][i][1][1]+bz[v][i][0][0]+anss[cur^1][1][0]),
                                    min(bz[u][i][0][1]+bz[v][i][1][0]+anss[cur^1][0][1],
                                        bz[u][i][1][1]+bz[v][i][1][0]+anss[cur^1][1][1]));
                anss[cur][1][1]=min(min(bz[u][i][0][1]+bz[v][i][0][1]+anss[cur^1][0][0],
                                        bz[u][i][1][1]+bz[v][i][0][1]+anss[cur^1][1][0]),
                                    min(bz[u][i][0][1]+bz[v][i][1][1]+anss[cur^1][0][1],
                                        bz[u][i][1][1]+bz[v][i][1][1]+anss[cur^1][1][1]));
                u=fa[u][i];
                v=fa[v][i];
            }
        }
        ansss=min(min(min(anss[cur][0][0],anss[cur][0][1]),min(anss[cur][1][0],anss[cur][1][1]))+g[fa[u][0]][1]+f[fa[u][0]][1]-min(f[u][0],f[u][1])-min(f[v][0],f[v][1]),anss[cur][1][1]+g[fa[u][0]][0]+f[fa[u][0]][0]-f[u][1]-f[v][1]);
        if (ansss>=INF)
        {
            puts("-1");
            continue;
        }
        printf("%lld\n",ansss);
    }
    
    return 0;
}

void dfs1(int u)
{
    int i,v;
    f[u][1]=p[u];
    for (i=head[u];i;i=nxt[i])
    {
        v=to[i];
        if (v!=fa[u][0])
        {
            fa[v][0]=u;
            dep[v]=dep[u]+1;
            dfs1(v);
            f[u][0]+=f[v][1];
            f[u][1]+=min(f[v][0],f[v][1]);
        }
    }
}

void dfs2(int u)
{
    int i,v;
    for (i=head[u];i;i=nxt[i])
    {
        v=to[i];
        if (v!=fa[u][0])
        {
            g[v][0]=g[u][1]+f[u][1]-min(f[v][0],f[v][1]);
            g[v][1]=min(g[u][0]+f[u][0]-f[v][1],g[u][1]+f[u][1]-min(f[v][0],f[v][1]));
            bz[v][0][0][1]=bz[v][0][1][1]=f[u][1]-min(f[v][0],f[v][1]);
            bz[v][0][1][0]=f[u][0]-f[v][1];
            bz[v][0][0][0]=INF;
            dfs2(v);
        }
    } 
}

void add(int u,int v)
{
    nxt[++cnt]=head[u];
    head[u]=cnt;
    to[cnt]=v;
}

CompilationN/AN/ACompile OKScore: N/A

Testcase #144.38 us84 KBAcceptedScore: 4

Testcase #250.69 us84 KBAcceptedScore: 4

Testcase #352.26 us84 KBAcceptedScore: 4

Testcase #444.11 us84 KBAcceptedScore: 4

Testcase #591.45 us168 KBAcceptedScore: 4

Testcase #6106.23 us168 KBAcceptedScore: 4

Testcase #7106.7 us160 KBAcceptedScore: 4

Testcase #81.204 ms1 MB + 916 KBAcceptedScore: 4

Testcase #91.211 ms1 MB + 916 KBAcceptedScore: 4

Testcase #101.265 ms1 MB + 828 KBAcceptedScore: 4

Testcase #111.252 ms1 MB + 828 KBAcceptedScore: 4

Testcase #12122.722 ms91 MB + 508 KBAcceptedScore: 4

Testcase #13122.622 ms91 MB + 508 KBAcceptedScore: 4

Testcase #14109.821 ms91 MB + 312 KBAcceptedScore: 4

Testcase #15109.764 ms91 MB + 316 KBAcceptedScore: 4

Testcase #16109.757 ms91 MB + 316 KBAcceptedScore: 4

Testcase #17163.285 ms91 MB + 508 KBAcceptedScore: 4

Testcase #18112.794 ms83 MB + 888 KBAcceptedScore: 4

Testcase #19112.569 ms83 MB + 888 KBAcceptedScore: 4

Testcase #20114.476 ms87 MB + 308 KBAcceptedScore: 4

Testcase #21114.562 ms87 MB + 308 KBAcceptedScore: 4

Testcase #22105.386 ms87 MB + 112 KBAcceptedScore: 4

Testcase #23155.381 ms87 MB + 304 KBAcceptedScore: 4

Testcase #24155.485 ms87 MB + 308 KBAcceptedScore: 4

Testcase #25155.253 ms87 MB + 320 KBAcceptedScore: 4


Judge Duck Online | 评测鸭在线
Server Time: 2024-12-05 10:37:18 | Loaded in 2 ms | Server Status
个人娱乐项目,仅供学习交流使用 | 捐赠