比赛 20140425 评测结果 AAAAATTT
题目名称 大话西游 最终得分 62
用户昵称 digital-T 运行时间 3.318 s
代码语言 C++ 内存使用 1.83 MiB
提交时间 2014-04-25 11:12:48
显示代码纯文本
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<vector>
#include<string>
#include<deque>
using namespace std;
int N,Q,importance[100010];
class EDGE
{
public:
	int u,v;
};
vector <EDGE> E;
vector <int> p[100010];
int mini[2],maxi[2];
void dfs(int x,int bef,int mark)
{
	mini[mark]=min(mini[mark],importance[x]);
	maxi[mark]=max(maxi[mark],importance[x]);
	for(unsigned int i=0;i<p[x].size();i++)
	{
		if(E[p[x][i]].v==bef)continue;
		dfs(E[p[x][i]].v,x,mark);
	}
}
int main()
{
	freopen("westward.in","r",stdin);
	freopen("westward.out","w",stdout);
	scanf("%d %d",&N,&Q);
	for(int i=1;i<=N;i++)scanf("%d",&importance[i]);
	int u,v;
	for(int i=1;i<N;i++)
	{
		scanf("%d %d",&u,&v);
		E.push_back((EDGE){u,v});
		E.push_back((EDGE){v,u});
		int temp=E.size();
		p[u].push_back(temp-2);
		p[v].push_back(temp-1);
	}
	char tmp[10];
	int x,y;
	long long ans;
	for(int i=1;i<=Q;i++)
	{
		scanf("%s",tmp);
		if(tmp[0]=='C')
		{
			scanf("%d %d",&x,&y);
			importance[x]=y;
		}
		else
		{
			scanf("%d",&x);
			mini[0]=mini[1]=0x7FFFFFFF;
			maxi[0]=maxi[1]=0;
			dfs(E[(x-1)*2].u,E[(x-1)*2].v,0);
			dfs(E[(x-1)*2].v,E[(x-1)*2].u,1);
			ans=((long long)mini[0])*((long long)maxi[0])+((long long)mini[1]*((long long)maxi[1]));
			printf("%lld\n",ans);
		}
	}
	return 0;
}