DFS深度优先搜索(7)--CodeForces

来源:互联网 发布:深入浅出数据分析pdf 编辑:程序博客网 时间:2024/05/29 12:28

                                                     Garland


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
62 40 54 22 11 14 2
Output
1 4
Input
62 40 64 22 11 14 2
Output
-1
Note

The garland and cuts scheme for the first example:

这道题的题意:给你一颗有n个节点的树,每个节点都有一个值,问你是否能删去两条边使删后的三块值的和都相等。
解题思路:先可以先算出总大小sum。接着%3看能不能分,不能-1,能继续。然后DFS算出每个子树的权值,和,在DFS的同时,去找权值为sum/3的子树(是从树的底层往顶层找),找到一个之后记录下此点并将该子树的权值变为0,然后继续去找第二个权值为sum/3的子树,找到之后记录下来就OK了,否则的话,输出-1.
代码如下:
//  main.cpp//  temp////  Created by Sly on 2017/2/22.//  Copyright © 2017年 Sly. All rights reserved.//#include <iostream>#include <stdio.h>#include <string.h>#include <vector>#define N 1000005using namespace std;vector<int>edg[N];int v[N],head,fa,cnt;int ans[5];int avr;void Dfs(int x){    int i;    for(i=0;i<edg[x].size();i++)    {        Dfs(edg[x][i]);        v[x]+=v[edg[x][i]];    }    if(cnt<2&&v[x]==avr&&x!=head)    {        ans[++cnt]=x;        v[x]=0;    }}int main(){    int n,i;    while(scanf("%d",&n)!=EOF)    {        int sum=0;        cnt=0;        for(i=1;i<=n;i++)        {            scanf("%d %d",&fa,&v[i]);            sum+=v[i];            edg[fa].push_back(i);            if(fa==0)head=i;        }        if(sum%3!=0){            printf("-1\n");            continue;        }        avr=sum/3;        Dfs(head);        if(v[head]==avr&&cnt==2)            printf("%d %d\n",ans[1],ans[2]);        else printf("-1\n");    }    return 0;}



0 0
原创粉丝点击