提交记录 8319


用户 题目 状态 得分 用时 内存 语言 代码长度
LuoshuiTianyi noip18f. 【NOIP2018】保卫王国 Accepted 100 171.155 ms 78456 KB C++ 2.79 KB
提交时间 评测时间
2019-02-12 16:26:13 2020-08-01 01:16:08
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n,m,nx[200001],to[200001],head[100001],a,b,c,d,type;
int fa[200001],deep[200001],w[100001],jump[100001][18];
long long f[100001][2],g[100001][2],dp[100001][18][2][2],inf=2e15;
void read(int &x)
{
x=0;
char ch=getchar();
while(!isdigit(ch))
ch=getchar();
while(isdigit(ch))
{
x=x*10+ch-'0';
ch=getchar();
}
}
void add(int u,int v,int d)
{
	nx[d]=head[u];
	to[d]=v;
	head[u]=d;
}
void build(int x)
{
	for(int i=head[x];i;i=nx[i])
		if(to[i]!=fa[x])
		{
			fa[to[i]]=x;
			deep[to[i]]=deep[x]+1;
			build(to[i]);
		}
}
void DP(int x)
{
	f[x][1]=w[x];
	if(x==a)
		f[x][(c+1)%2]=inf;
	if(x==b)
		f[x][(d+1)%2]=inf;
	for(int i=head[x];i;i=nx[i])
		if(to[i]!=fa[x])
		{
			int p=to[i];
			DP(p);
			f[x][1]+=min(f[p][0],f[p][1]);
			f[x][0]+=f[p][1];
		}
}
void D_P(int x)
{
	for(int i=head[x];i;i=nx[i])
		if(to[i]!=fa[x])
		{
			int p=to[i];
			g[p][0]=g[x][1]+f[x][1]-min(f[p][0],f[p][1]);
			g[p][1]=min(g[x][0]+f[x][0]-f[p][1],g[p][0]);
			D_P(p);
		}
}
long long work()
{
	if(deep[a]<deep[b])
	{
		swap(a,b);
		swap(c,d);
	}
	long long tx[2]={inf,inf},ty[2]={inf,inf},nx[2],ny[2];
	tx[c]=f[a][c],ty[d]=f[b][d];
	for(int i=17;i>=0;i--)
		if(deep[jump[a][i]]>=deep[b])
		{
			nx[0]=nx[1]=inf;
			for(int j=0;j<=1;j++)
				for(int k=0;k<=1;k++)
					nx[j]=min(nx[j],tx[k]+dp[a][i][k][j]);
			tx[0]=nx[0],tx[1]=nx[1];
			a=jump[a][i];
		}
	if(a==b)
		return tx[d]+g[b][d];
	for(int i=17;i>=0;i--)
		if(jump[a][i]!=jump[b][i])
		{
			nx[0]=nx[1]=inf;
			ny[0]=ny[1]=inf;
			for(int j=0;j<=1;j++)
				for(int k=0;k<=1;k++)
				{
					nx[j]=min(nx[j],tx[k]+dp[a][i][k][j]);
					ny[j]=min(ny[j],ty[k]+dp[b][i][k][j]);
				}
			tx[0]=nx[0],tx[1]=nx[1];
			ty[0]=ny[0],ty[1]=ny[1];
			a=jump[a][i];
			b=jump[b][i];
		}
	int lca=fa[a];
	return min(f[lca][0]-f[a][1]-f[b][1]+tx[1]+ty[1]+g[lca][0],f[lca][1]-min(f[a][0],f[a][1])-min(f[b][0],f[b][1])+min(tx[0],tx[1])+min(ty[0],ty[1])+g[lca][1]);
}
int main()
{
read(n),read(m),read(type);
	int u,v;
	for(int i=1;i<=n;i++)
read(w[i]);
	for(int i=1;i<n;i++)
	{
read(u),read(v);
		add(u,v,i);
		add(v,u,i+n);
	}
	deep[1]=1;
	build(1);
	DP(1);
	D_P(1);
	for(int i=1;i<=n;i++)
	{
		jump[i][0]=fa[i];
		dp[i][0][0][0]=2e15;
		dp[i][0][0][1]=f[fa[i]][1]-min(f[i][0],f[i][1]);
		dp[i][0][1][0]=f[fa[i]][0]-f[i][1];
		dp[i][0][1][1]=f[fa[i]][1]-min(f[i][0],f[i][1]);
	}
	for(int j=1;j<=17;j++)
		for(int i=1;i<=n;i++)
		{
			jump[i][j]=jump[jump[i][j-1]][j-1];
			for(int k=0;k<=1;k++)
				for(int l=0;l<=1;l++)
				{
					dp[i][j][k][l]=2e15;
					for(int q=0;q<=1;q++)
						dp[i][j][k][l]=min(dp[i][j][k][l],dp[i][j-1][k][q]+dp[jump[i][j-1]][j-1][q][l]);
				}
		}
	for(int i=1;i<=m;i++)
	{
read(a),read(c),read(b),read(d);
		if((fa[a]==b&&c==0&&d==0)||(fa[b]==a&&c==0&&d==0))
		{
			cout<<"-1\n";
			continue;
		}
		cout<<work()<<endl;
	}
}

CompilationN/AN/ACompile OKScore: N/A

Testcase #142.99 us84 KBAcceptedScore: 4

Testcase #249.17 us84 KBAcceptedScore: 4

Testcase #349.63 us84 KBAcceptedScore: 4

Testcase #443.12 us84 KBAcceptedScore: 4

Testcase #579.59 us152 KBAcceptedScore: 4

Testcase #679.25 us152 KBAcceptedScore: 4

Testcase #781.51 us144 KBAcceptedScore: 4

Testcase #81.142 ms1 MB + 612 KBAcceptedScore: 4

Testcase #91.126 ms1 MB + 612 KBAcceptedScore: 4

Testcase #101.191 ms1 MB + 524 KBAcceptedScore: 4

Testcase #111.177 ms1 MB + 524 KBAcceptedScore: 4

Testcase #12132.067 ms76 MB + 632 KBAcceptedScore: 4

Testcase #13131.967 ms76 MB + 632 KBAcceptedScore: 4

Testcase #14116.336 ms76 MB + 436 KBAcceptedScore: 4

Testcase #15116.5 ms76 MB + 440 KBAcceptedScore: 4

Testcase #16116.574 ms76 MB + 440 KBAcceptedScore: 4

Testcase #17171.155 ms76 MB + 632 KBAcceptedScore: 4

Testcase #18117.253 ms68 MB + 1012 KBAcceptedScore: 4

Testcase #19117.231 ms68 MB + 1012 KBAcceptedScore: 4

Testcase #20123.938 ms72 MB + 432 KBAcceptedScore: 4

Testcase #21124.077 ms72 MB + 432 KBAcceptedScore: 4

Testcase #22111.086 ms72 MB + 236 KBAcceptedScore: 4

Testcase #23160.22 ms72 MB + 428 KBAcceptedScore: 4

Testcase #24160.18 ms72 MB + 432 KBAcceptedScore: 4

Testcase #25160.091 ms72 MB + 444 KBAcceptedScore: 4


Judge Duck Online | 评测鸭在线
Server Time: 2026-04-06 23:51:34 | Loaded in 1 ms | Server Status
个人娱乐项目,仅供学习交流使用 | 捐赠