比赛 20120810 评测结果 AWWWAAWAWWWWWWWW
题目名称 蚂蚁和瓢虫 最终得分 25
用户昵称 TBK 运行时间 1.939 s
代码语言 C++ 内存使用 97.31 MiB
提交时间 2012-08-10 11:23:21
显示代码纯文本
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <string>
#include <cstring>
#include <cmath>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;
int a[5001][5001],b,c,d,l,m,n,k[1001],r[5001],p[2][200000],h=1,t,s[1001],w;
bool bo[5001];
struct fun
{
	int p;
	int q;
}f[5000];
int Compare(const void*elem1,const void*elem2)
{
	struct fun *elem3=(struct fun *)elem1;
	struct fun *elem4=(struct fun *)elem2;
	if (elem3->p != elem4->p) return elem3->p - elem4->p;
		else return elem3->q - elem4->q;
}
int main(void)
{
	freopen("mro.in","r",stdin);
	freopen("mro.out","w",stdout);
	scanf("%d",&b);
	for (c=0;c<b-1;c++) 
	{
		scanf("%d%d",&f[c].p,&f[c].q);
		f[c+b-1].p=f[c].q;
		f[c+b-1].q=f[c].p;
	}
	qsort(f,2*b-2,sizeof(fun),Compare);
	for (c=1;c<2*b-2;c++)
		if (f[c].p!=f[c-1].p) r[f[c-1].p]=c;
	r[b]=2*b-2;
	for (d=1;d<=b;d++)
	{
		p[0][0]=d;
		while (h>t)
		{
			bo[p[0][t]]=true;
			for (c=r[p[0][t]-1];c<r[p[0][t]];c++)
				if (bo[f[c].q]==false)
				{
					bo[f[c].q]=true;
					p[0][h]=f[c].q;
					p[1][h]=p[1][t]+1;
					a[d][f[c].q]=p[1][h];
					h++;
				}
			t++;
		}
		h=1;
		t=0;
		memset(p,0,sizeof(p));
		memset(bo,0,sizeof(bo));
	}
	scanf("%d",&c);
	for (d=1;d<=c;d++) scanf("%d",&k[d]);
	scanf("%d",&d);
	for (l=0;l<d;l++)
	{
		scanf("%d",&m);
		t=b;
		for (n=1;n<=c;n++)
			if (a[k[n]][m]<t) 
			{
				t=a[k[n]][m];
				w=n;
			}
		s[w]++;
		k[w]=m;
	}
	for (l=1;l<=c;l++) printf("%d %d\n",k[l],s[l]);
	fclose(stdin);
	fclose(stdout);
	return 0;
}