CodeForces - 767C Garland(乱搞)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xuejye/article/details/76346991

题目链接:http://codeforces.com/problemset/problem/767/C点击打开链接

C. Garland
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard 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
Note

The garland and cuts scheme for the first example:


先把每个节点的值计算总和 然后后序遍历 将值从下往上累加 碰到等于总和/3的就将当前节点他加入容器中 

如果容器中的数目大于等于三 说明存在能够分成三段的多种情况

取出前面两个就行

#include <iostream>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <stack>
#include <limits.h>
#include <string>
#include <string.h>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <math.h>
using namespace std;
vector < int > root[1111111];
int temp[1111111];
int be;
int sum=0;
vector<int > ans;
int getsum(int now)
{
    for(int i=0;i<root[now].size();i++)
    {
        getsum(root[now][i]);
    }
    for(int i=0;i<root[now].size();i++)
        temp[now]+=temp[root[now][i]];
    if(temp[now]==sum)
       {
           ans.push_back(now);
           temp[now]-=sum;
       }
}
int main()
{
    int n=0;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        int m,k;
        scanf("%d%d",&m,&k);
        root[m].push_back(i);
        temp[i]=k;
        if(m==0)
            be=i;
        sum+=k;
    }

    if(sum%3!=0)
        cout << "-1";
    else
    {
        sum/=3;
        getsum(be);
        //cout << be <<endl;
        if(ans.size()>=3)
            {
                for(int i=0;i<2;i++)
                        cout << ans[i] <<" ";
            }
        else
            cout << "-1";
    }
}



codeforces 621B wrong answer 求解

02-03

题目rnrnB. Wet Shark and Bishopsrnrntime limit per test:2 secondsrnrnmemory limit per test:256 megabytesrnrninput:standard inputrnrnoutput:standard outputrnrnrnToday, Wet Shark is given n bishops on a 1000 by 1000 grid. Both rows and columns of the grid are numbered from 1 to 1000. Rows are numbered from top to bottom, while columns are numbered from left to right.rnrnWet Shark thinks that two bishops attack each other if they share the same diagonal. Note, that this is the only criteria, so two bishops may attack each other (according to Wet Shark) even if there is another bishop located between them. Now Wet Shark wants to count the number of pairs of bishops that attack each other.rnrnrnInputrnrnThe first line of the input contains n (1 ≤ n ≤ 200 000) — the number of bishops.rnrnEach of next n lines contains two space separated integers xi and yi (1 ≤ xi, yi ≤ 1000) — the number of row and the number of column where i-th bishop is positioned. It's guaranteed that no two bishops share the same position.rnrnrnOutputrnrnOutput one integer — the number of pairs of bishops which attack each other. rnrnrnSample test(s)rnrnrnrnInputrn5rn1 1rn1 5rn3 3rn5 1rn5 5rnrnrnrnOutputrn6rnrnrnrnInputrn3rn1 1rn2 3rn3 5rnrnrnrnOutputrn0rnrnrnrnNoternrnIn the first sample following pairs of bishops attack each other: (1, 3), (1, 5), (2, 3), (2, 4), (3, 4) and (3, 5). Pairs (1, 2), (1, 4), (2, 5) and (4, 5) do not attack each other because they do not share the same diagonal.rnrnrnrnrn我的代码rn#includern#includern#includern#includern#includern#includern#includernusing namespace std;rnint x[1050],y[1050],n,a[1050][1050];rnbool panduan(int i,int j)rnrn if((i>=1)&&(i<=1000)&&(j>=1)&&(j<=1000))rn return true;rn elsern return false;rnrnint main(void)rnrn long long sum=0;rn int ii,jj,status,j,i,temp_a,temp_b;rn scanf("%d",&n);rn for(i=1;i<=1000;i++)rn for(j=1;j<=1000;j++)rn a[i][j]=0;rn for(i=0;i

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试