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.
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.
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.
62 40 54 22 11 14 2
1 4
62 40 64 22 11 14 2
-1
The garland and cuts scheme for the first example:
// 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;}
- DFS深度优先搜索(7)--CodeForces
- 深度优先搜索 DFS
- 深度优先搜索 DFS
- DFS 深度优先搜索
- DFS 深度优先搜索
- 深度优先搜索(DFS)
- 深度优先搜索(DFS)
- 深度优先搜索DFS
- DFS深度优先搜索
- 深度优先搜索(DFS)
- dfs深度优先搜索
- dfs 深度优先搜索
- 深度优先搜索(DFS)
- 深度优先搜索DFS
- DFS深度优先搜索
- 深度优先搜索(DFS)
- 深度优先搜索DFS
- 深度优先搜索(DFS)
- redis cluster 添加删除节点
- react native 填坑之旅
- Django 部署时得到 Bad Request (400)(DEBUG = False)
- HTTPClient调用---org.apache.commons.httpclient
- Android studio如何使用SVN进行版本控制
- DFS深度优先搜索(7)--CodeForces
- Eclipse JPA project Change Event Handler问题解决
- html中文档流的理解
- RxJava系列-RxJava1.x操作符一览
- JDBC(七)数据库事务与JDBC事务处理
- leetcode111~Minimum Depth of Binary Tree
- windows编程初步
- TensorFlow MNIST案例代码
- 获取APP的版本号,名称,序列号,设备名称等