国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

codeforces #398(div.2)C

2019-11-08 02:32:44
字體:
來源:轉載
供稿:網友

C. Garland time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard output 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.

Examples 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 題意:給你一棵樹,樹上的每個節點有權值,權值有正有負還有0,問你能不能把這棵樹變成權值和相等的三部分 解題思路:我們知道要把一棵樹變成3部分,就必須刪除兩條邊,題目雖然沒有說這棵樹是有根樹,但是我們可以把它當成有根樹,令dp[i]為以i為根節點的子樹的權值和,然后dfs根,如果中間出現sum/3,就刪除該子樹。為什么這種做法是對的,我們來簡單的證明一下:假設最后是有解的,也就是可以把這棵樹分成三等份,所以最后這棵樹一定被刪除了兩條邊,我們把這棵樹看成有根樹(以哪個節點為根沒有關系,但是這里為了方便,取了一個特殊的點),也就是在這棵有根樹中需要刪除兩條邊,而在有根樹中刪除一條邊產生的效果就是產生一棵子樹,所以最后一定能得到解。需要注意的是這里刪除子樹的做法是讓dp[u] = 0就行,u 是子樹的根。

#include<bits/stdc++.h>using namespace std;const int maxn = 1e6 + 10;int v1,v2,root,n,sum;int dp[maxn];vector<int> g[maxn];void dfs(int u){ for(int i = 0; i < g[u].size(); i++) { int v = g[u][i]; dfs(v); dp[u] += dp[v]; } if(dp[u] == sum/3&&u != root) { if(!v1) { v1 = u; dp[u] = 0; } else if(!v2) { v2 = u; dp[u] = 0; } }}int main(){ while(~scanf("%d",&n)) { v1 = 0; v2 = 0; sum = 0; for(int i = 1; i <= n; i++) { g[i].clear(); } for(int i = 1; i <= n; i++) { int x; scanf("%d%d",&x,&dp[i]); if(x == 0) { root = i; } else { g[x].push_back(i); } sum += dp[i]; } if(sum%3 != 0) { printf("-1/n"); exit(0); } dfs(root); if(v1&&v2) printf("%d %d/n",v1,v2); else printf("-1/n"); } return 0;}
上一篇:潛伏者

下一篇:斯諾登的密碼

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 崇信县| 江城| 房产| 嘉禾县| 汉沽区| 内乡县| 铜山县| 微博| 嵩明县| 昌乐县| 易门县| 宜章县| 潢川县| 扎囊县| 丹巴县| 枝江市| 保靖县| 同仁县| 罗江县| 始兴县| 申扎县| 闽清县| 乌恰县| 建宁县| 新建县| 广昌县| 孝昌县| 聂荣县| 梓潼县| 遂平县| 文安县| 临沂市| 巴彦淖尔市| 寿阳县| 繁昌县| 汽车| 江山市| 南雄市| 开封县| 东莞市| 民县|