TopCoder SRM665 Div2 B

来源:互联网 发布:淘宝店铺页面怎么设计 编辑:程序博客网 时间:2024/06/17 14:11

Problem Statement

This problem is about trees. A tree consists of some special points (called nodes), and some lines (called edges) that connect those points. Each edge connects exactly two nodes. If there are N nodes in a tree, there are exactly N-1 edges. The edges of a tree must connect the nodes in such a way that the tree is connected: it must be possible to get from any node to any other node by traversing some sequence of edges. Note that this implies that a tree never contains a cycle: for each pair of nodes there is exactly one way to reach one from the other without using the same edge twice.

Dog has a tree. The edges in Dog’s tree have weights. As Dog likes the numbers 4 and 7, the weight of each edge is either 4 or 7.

Cat loves modifying trees. Cat is now going to modify Dog’s tree by adding one new edge. The new edge will also have a weight that is either 4 or 7. The new edge will connect two nodes that don’t already have an edge between them. Note that adding any such edge will create exactly one cycle somewhere in the tree. (A cycle is a sequence of consecutive edges that starts and ends in the same node.)

A cycle is balanced if the number of edges on the cycle is even, and among them the number of edges with weight 4 is the same as the number of edges with weight 7. Cat would like to add the new edge in such a way that the cycle it creates will be balanced.

You are given the description of Dog’s current tree in vector s edge1, edge2, and weight. Each of these vector s will have exactly n-1 elements, where n is the number of nodes in Dog’s tree. The nodes in Dog’s tree are labeled 1 through n. For each valid i, Dog’s tree contains an edge that connects the nodes edge1[i] and edge2[i], and the weight of this edge is weight[i].

Return a vector with exactly three elements: {P,Q,W}. Here, P and Q should be the nodes connected by the new edge, and W should be the weight of the new edge. (Note that P and Q must be between 1 and N, inclusive, and W must be either 4 or 7.) If there are multiple solutions, return any of them. If there are no solutions, return an empty vector instead.
Definition

Class:

LuckyCycle

Method:

getEdge

Parameters:

vector , vector , vector

Returns:

vector

Method signature:

vector getEdge(vector edge1, vector edge2, vector weight)
(be sure your method is public)

Limits

Time limit (s):
2.000
Memory limit (MB):
256
Stack limit (MB):
256

Constraints

N will be between 2 and 100, inclusive.

edge1, edge2, and weight will each contain exactly N-1 elements.

Each element of weight will be either 4 or 7

Each element of edge1 and edge2 will be between 1 and N, inclusive.

The input will define a tree.

Examples

0)

{1}
{2}
{4}
Returns: { }
We cannot add any edge because the only two nodes are already connected by an edge.

1)

{1, 3, 2, 4}
{2, 2, 4, 5}
{4, 7, 4, 7}
Returns: {1, 5, 7 }
The input describes a tree with 5 nodes. The tree contains the following edges: 1-2 (weight 4), 3-2 (weight 7), 2-4 (weight 4), and 4-5 (weight 7). The example return value describes a new edge that connects nodes 1 and 5, and has weight 7. Adding the new edge creates a cycle that goes through the nodes 1, 2, 4, and 5, in this order. This cycle is balanced: two of its four edges have weight 4 and the other two have weight 7.

2)

{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}
{2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}
{4, 4, 4, 4, 4, 4, 7, 7, 7, 7, 7, 7}
Returns: {1, 12, 7 }

3)

{1, 2, 3, 5, 6}
{2, 3, 4, 3, 5}
{4, 7, 7, 7, 7}
Returns: {1, 4, 4 }

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.

题意

给一颗树,树的边权不是4就是7,要求添加一条边权为47的边,产生一个环,其中边权为4的边和边权为7的边数相等。

题解

可以暴力枚举每一个点做起点,跑dfs,记录路径上4边和7边的数量,当他们相差1并且和大于1的时候表示可以将起点和当前点连接,新边边权为数量较少的边的权值(这次只切了250和500。。。Div1离我还很远啊。。。)

代码

class LuckyCycle {public:    vector <int> getEdge(vector <int>, vector <int>, vector <int>);    int f,s;    int vis[105];    struct node    {        int v;        int w;    };    node E[105][105];    int Enum[105];    int dfs(int x)    {        int ans;        if(s+f>1&&abs(s-f)==1)            return x;        vis[x]=1;        for(int i=0;i<Enum[x];i++)        {            if(vis[E[x][i].v]==0)            {                if(E[x][i].w==7)                    s++;                else                    f++;                if(ans=dfs(E[x][i].v))                    return ans;                if(E[x][i].w==7)                    s--;                else                    f--;            }        }        return 0;    }};
0 0
原创粉丝点击