| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
哪位大牛给组测试数据吧。。。Wrong Answer 了N次了。。。额的代码呈上。。。#include <stdio.h>
#include <memory.h>
#define DF_VAIN (0xFFFF)
#define DF_MINNUM (-9999)
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
int N;
int root;
int father[110];
int child[110];
int brother[110];
int f[110];//f[i]以i为根的树的最小连接数
int F[110];//F[i]以i为根的树min(FfromChild[i],去掉根i后的最小链接数)
int FfromChild[110];//去掉根i和一个链后的最小链接数
bool Process(int _root)//有三个兄弟无效则无解
{//在孩子里面找f-F最大的两个,至少有一个孩子
int index=child[_root];
int maxVal1=DF_MINNUM,maxVal2=DF_MINNUM,maxSubChild=DF_MINNUM,maxSubChildIndex=-1;
bool bFfromChild=true;//FfromChild[index]==f[index]==DF_VAIN则为false
int sum=0;
int vainCount=0;
while (index)
{//在child中不会有F[index]==DF_VAIN&&f[index]==DF_VAIN,但可能有FfromChild[index]==DF_VAIN&&f[index]==DF_VAIN
sum+=f[index];
vainCount+=(f[index]==DF_VAIN?1:0);
if (vainCount>2)
{//此时对外连线和内部连线都不行
f[index]=F[index]=DF_VAIN;
return false;
}
if (F[index]==DF_VAIN)
{//index节点外部连线不行,此时FfromChild[index]==DF_VAIN,但f[index]!=DF_VAIN
index=brother[index];
continue;
}
if (FfromChild[index]!=DF_VAIN)
{
int curChildSubVal=f[index]-FfromChild[index];
maxSubChild=max(curChildSubVal,maxSubChild);
}
else if (f[index]==DF_VAIN)
{
bFfromChild=false;
}
int curVal=f[index]-F[index];
curVal>maxVal1?(maxVal2=maxVal1,maxVal1=curVal)
:(curVal>maxVal2?(maxVal2=curVal):0);
index=brother[index];
}
FfromChild[_root]=(vainCount>1||maxVal1==DF_MINNUM)?DF_VAIN:(sum-maxVal1);
F[_root]=min(FfromChild[_root],(vainCount?DF_VAIN:sum));
f[_root]=(vainCount>2||maxVal2==DF_MINNUM)?(DF_VAIN):(sum-maxVal1-maxVal2+1);//勿忘加一
f[_root]=min(f[_root],((vainCount>1||maxSubChild==DF_MINNUM||!bFfromChild)
?DF_VAIN:(sum-maxSubChild+1)));
return f[_root]!=DF_VAIN||F[_root]!=DF_VAIN;
}
bool dfs(int _root)
{//中序遍历
if (!child[_root])
{
f[_root]=DF_VAIN;
FfromChild[_root]=DF_VAIN;
F[_root]=0;
}
else
{
if(!dfs(child[_root])) return false;
if(!Process(_root)) return false;
}
if(brother[_root]&&!dfs(brother[_root])) return false;
return true;
}
void Init()
{
int i,tmp1,tmp2;
memset(father,0,sizeof(father));
memset(child,0,sizeof(child));
memset(brother,0,sizeof(brother));
//建树
scanf("%d%d",&root,&tmp1);
child[root]=tmp1;father[tmp1]=root;
for (i=2;i<=N-1;i++)
{
scanf("%d%d",&tmp1,&tmp2);
if(father[tmp2])//tmp1优先当根
{//tmp2已有父亲,则交换tmp1,tmp2
int tmpswap=tmp1;
tmp1=tmp2;tmp2=tmpswap;
}
father[tmp2]=tmp1;
if (root==tmp2)
{
int tmproot=tmp1;
while(father[tmproot]) tmproot=father[tmproot];//修改
root=tmproot;
}
if (child[tmp1])
{
int tmpIndex=child[tmp1];
while (brother[tmpIndex])
tmpIndex=brother[tmpIndex];
brother[tmpIndex]=tmp2;
}
else
child[tmp1]=tmp2;
}
}
int main(int argc, char* argv[])
{
while (EOF!=scanf("%d",&N))
{
Init();
if(!dfs(root)||f[root]==DF_VAIN) printf("-1\n");
else printf("%d\n",f[root]);
}
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator