zoukankan      html  css  js  c++  java
  • Codeforces Round #398 C题Garland(DFS)解题报告

    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的倍数。用dfs的深搜特性,从叶子结点逐渐往上累加,刷新各个结点为根的子树的值,如果找到是 所有和/3的结点就将这个子树去掉。重复此过程,直到找到2个子树,但仍将刷新结点进行到底。 如果找到了这样的两个子树,并且余下的点值之和(即根此时的值)也为目标值,就找到了答案,输出即可。若不然,不存在这样的解,输出-1.

     1 #include <iostream>
     2 #include<bits/stdc++.h>
     3 #include <stack>
     4 #include <queue>
     5 #include <cstdio>
     6 #include <cstring>
     7 #include <algorithm>
     8 using namespace std;
     9 typedef long long ll;
    10 typedef unsigned long long ull;
    11 const int MAX=1e6+5;
    12 vector <int>edge[MAX];
    13 int n;
    14 int tem[MAX];
    15 int an[5];
    16 int cnt;
    17 int he;
    18 int root;
    19 void dfs(int point)
    20 {
    21 
    22     for(int i=0;i<edge[point].size();i++)
    23     {
    24         dfs(edge[point][i]);
    25         tem[point]+=tem[edge[point][i]];
    26     }
    27     if(tem[point]==he&&cnt<2&&point!=root)
    28     {
    29         an[++cnt]=point;
    30         tem[point]=0;
    31     }
    32 }
    33 int main()
    34 {
    35     scanf("%d",&n);
    36     int i;
    37     int fa;
    38     for(i=1;i<=n;i++)
    39     {
    40         scanf("%d %d",&fa,&tem[i]);
    41         if(fa==0)
    42             root=i;
    43         he+=tem[i];
    44         edge[fa].push_back(i);
    45     }
    46     if(he%3)
    47     {
    48         printf("-1
    ");
    49         return 0;
    50     }
    51     he/=3;
    52     dfs(root);
    53     if(cnt==2&&tem[root]==he)
    54     {
    55         printf("%d %d
    ",an[1],an[2]);
    56         return 0;
    57     }
    58     else
    59         printf("-1
    ");
    60     return 0;
    61 }
    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:

  • 相关阅读:
    执行eclipse,迅速failed to create the java virtual machine。
    hdu4000 &amp;&amp; hrbust1625
    linux高级技巧:heartbeat+lvs(一)
    Android-它们的定义Dialog
    @repository注解
    常用myeclipse的快捷键,对菜鸟超有用的
    myEclipse快捷键
    JDK 1.6.0和 6.0 有啥区别,JavaTM SE 6 的平台名字和版本号的说明(转)
    Cannot return from outside a function or method
    eclipse报错 com/genuitec/eclipse/j2eedt/core/J2EEProjectUtil 转
  • 原文地址:https://www.cnblogs.com/quintessence/p/6415538.html
Copyright © 2011-2022 走看看