Anniversary party
L K
It means that the K-th employee is an immediate supervisor of the L-th employee. Input is ended with the line
0 0
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int par[6066];//雙親節點
int child[6066][6066];//孩子列表(戶口本)
bool vis[6066];
int dp[6066][5];
int n;
void init(int n)
{
for(int i=1; i<=n; i++)
{
par[i]=i;
vis[i]=0;
dp[i][1]=0;
dp[i][0]=0;
child[i][0]=0;
}
}
void dfs(int root)
{
vis[root]=1;
for(int i=1; i<=child[root][0]; i++)//查詢孩子節點的時候可以直接用戶口本,而不用全抓來做親子鑑定
{
int son=child[root][i];
if(!vis[son])
{
dfs(son);
dp[root][1]+=dp[son][0];
dp[root][0]+=max(dp[son][0],dp[son][1]);
}
}
}
int main()
{
while(~scanf("%d",&n)&&n)
{
init(n);
int a,b;
for(int i=1; i<=n; i++)
{
scanf("%d",&dp[i][1]);
}
while(scanf("%d%d",&a,&b))
{
if(a==0&&b==0)
{
break;
}
par[a]=b;
child[b][0]++;//孩子數目加1
child[b][child[b][0]]=a;//記錄孩子
}
int root=1;
for(int i=1;i<=n;i++)
{
if(i==par[i])
{
root=i;
break;
}
}
dfs(root);
printf("%d\n",max(dp[root][1],dp[root][0]));
}
return 0;
}
不過本人感覺這種方法實在遜爆了!這麼low,還浪費內存,用這樣low的代碼AC了就算不能表現出作爲一個ACMer的意志。
{
nodes *parent;
nodes *child;
nodes *borther;
int val[5];
} node[6066];
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct nodes
{
nodes *parent;
nodes *child;
nodes *borther;
int val[5];
} node[6066];
void dfs(nodes* root)
{
nodes* p=root;
if(p->child!=NULL)
{
p=p->child;
dfs(p);
root->val[1]+=p->val[0];
root->val[0]+=max(p->val[0],p->val[1]);
while(p->borther!=NULL)
{
p=p->borther;
dfs(p);
root->val[1]+=p->val[0];
root->val[0]+=max(p->val[0],p->val[1]);
}
}
}
int main()
{
int n;
while(~scanf("%d",&n)&&n)
{
int a,b;
for(int i=1; i<=n; i++)
{
scanf("%d",&node[i].val[1]);
node[i].borther=NULL;
node[i].child=NULL;
node[i].parent=NULL;
node[i].val[0]=0;
}
while(scanf("%d%d",&a,&b))
{
if(a==0&&b==0)
{
break;
}
node[a].parent=&node[b];
nodes *p=&node[b];
if(p->child!=NULL)
{
p=p->child;
while(p->borther!=NULL)
{
p=p->borther;
}
p->borther=&node[a];
}
else
{
p->child=&node[a];
}
}
nodes* root=&node[1];
while(root->parent!=NULL)
{
root=root->parent;
}
dfs(root);
printf("%d\n",max(root->val[1],root->val[0]));
}
return 0;
}