Garland

来源:互联网 发布:男生衣服软件 编辑:程序博客网 时间:2024/05/23 00:27

Once at New Year Dima had a dream in which he was presented a fairy garland. A garland is a set of lamps, some pairs of which are connected by wires. Dima remembered that each two lamps in the garland were connected directly or indirectly via some wires. Furthermore, the number of wires was exactly one less than the number of lamps.
There was something unusual about the garland. Each lamp had its own brightness which depended on the temperature of the lamp. Temperatures could be positive, negative or zero. Dima has two friends, so he decided to share the garland with them. He wants to cut two different wires so that the garland breaks up into three parts. Each part of the garland should shine equally, i. e. the sums of lamps’ temperatures should be equal in each of the parts. Of course, each of the parts should be non-empty, i. e. each part should contain at least one lamp.
这里写图片描述
Help Dima to find a suitable way to cut the garland, or determine that this is impossible.
While examining the garland, Dima lifted it up holding by one of the lamps. Thus, each of the lamps, except the one he is holding by, is now hanging on some wire. So, you should print two lamp ids as the answer which denote that Dima should cut the wires these lamps are hanging on. Of course, the lamp Dima is holding the garland by can’t be included in the answer.
Input
The first line contains single integer n (3 ≤ n ≤ 106) — the number of lamps in the garland.
Then n lines follow. The i-th of them contain the information about the i-th lamp: the number lamp ai, it is hanging on (and 0, if is there is no such lamp), and its temperature ti ( - 100 ≤ ti ≤ 100). The lamps are numbered from 1 to n.
Output
If there is no solution, print -1.
Otherwise print two integers — the indexes of the lamps which mean Dima should cut the wires they are hanging on. If there are multiple answers, print any of them.
Example
Input
6
2 4
0 5
4 2
2 1
1 1
4 2
Output
1 4
Input
6
2 4
0 6
4 2
2 1
1 1
4 2
Output
-1
Note
The garland and cuts scheme for the first example:
这里写图片描述

这个题有两种思路,一个是从根节点往下面递归,计算出每个子树的sum,如果这个值是总和的1/3,我们就可以剪掉他,继续往上走。当然不能剪根节点,后面有样例,会报错。

#include<cstdio>#include<iostream>#include<cstring>#include<queue>using namespace std;const int Max=1e6+10;/*我十分高兴啊,我竟然会了递归,最近感觉自己就像一个傻逼,很空,也不知道该怎么办,过的好累,过了就好啊说明自己的能力还是很强的,不要害怕*/int val[Max];int root;int flag=0;int sum_tep;int ans[2];vector<int> son[Max];void dfs(int now){    int v_size=son[now].size();    for(int i=0;i<v_size;i++)    {        dfs(son[now][i]);        if(flag==2)            return;        val[now] += val[son[now][i]];//将温度进行记录    }    if(val[now]==sum_tep/3&&now!=root)    {        val[now]=0;        ans[flag++]=now;        if(flag==2)            return;    }  //  printf("now:%d  val[now]:%d\n",now,val[now]);}int main(){   int n;   int father;   sum_tep=0;   scanf("%d",&n);   for(int i=1;i<=n;i++)   {       scanf("%d %d",&father,&val[i]);//记住这种赋值       sum_tep += val[i];       if(father==0)//记录根节点       {           root=i;           continue;       }       son[father].push_back(i);//因为是树么,还是直接出父亲,所以以父亲为下标的里面装上,i;   }   if(sum_tep%3!=0)   {       printf("-1\n");//直接结束主函数       return 0;   }   dfs(root);   if(flag==2)      printf("%d %d\n",ans[0],ans[1]);   else      printf("-1\n");   return 0;}

另一种思路就是向拓扑排序一样,从下往上走,遇见总和为1/3的我们就剪掉他。

#include<cstdio>#include<iostream>#include<cstring>#include<queue>using namespace std;const int Max=1e6+10;int value[Max];int out_degree[Max];int pre[Max];int main(){   int n;   int sum_tep=0;   int root;   scanf("%d",&n);   for(int i=1;i<=n;i++)   {       int father;       scanf("%d %d",&father,&value[i]);       if(father==0)       {           root=i;       }       sum_tep += value[i];       out_degree[father]++;//计算每个节点的孩子数量       pre[i]=father;//要不然后面孩子没有办法找父亲   }   if(sum_tep%3!=0)   {       printf("-1\n");       return 0;   }   queue<int> q;   for(int i=1;i<=n;i++)   {       if(out_degree[i]==0)//将出度为0的放入到队列中        q.push(i);   }   int flag=0;   int ans[2];   while(q.size())   {       int temp=q.front();       q.pop();       if(value[temp]==sum_tep/3&&temp!=root)       {           ans[flag++]=temp;           value[temp]=0;  //赋值之后这个点的权值要归0           if(flag==2)             break;       }       value[pre[temp]] += value[temp];       out_degree[pre[temp]]--;       if(!out_degree[pre[temp]]&&temp!=root)//树根说了不能减,所以我们就不可以把树根放进去         q.push(pre[temp]);   }   if(flag==2)       printf("%d %d\n",ans[0],ans[1]);   else       printf("-1\n");   return 0;}