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.
题意
给一颗树,树的边权不是
题解
可以暴力枚举每一个点做起点,跑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; }};
- TopCoder SRM665 Div2 B
- TopCoder SRM665 Div2 A
- Topcoder SRM 683 Div2 B
- Topcoder SRM 636 Div2 B
- TopCoder SRM 637 Div2 B
- Topcoder 721 div2-B RememberWordsEasy
- topcoder SRM 513 DIV2
- TopCoder SRM 543 DIV2
- topcoder 544 div2
- Topcoder SRM548 div2
- topcoder-srm-233-div2
- Topcoder SRM Div2 Level2
- topcoder-595-div2
- topcoder srm606 div2
- topcoder srm607 div2
- TopCoder SRM 144 div2
- TOPCODER SRM 612 DIV2
- TOPCODER SRM 613 DIV2
- 算法思想篇(2)————递推算法
- mac利用QQ快速截图
- 一切关于兼容
- python网络编程(3)
- 如何在子类外调用父类的方法
- TopCoder SRM665 Div2 B
- 细说Cocos2d-JS——你必须知道的JavaScript
- 哈夫曼树详解
- 《算法导论》第18章 B树
- 网页MP4播放器代码
- 双拖动条自定义seekbar(带浮动textView)
- HDU-1596 find the safest road(最短路[Dijkstra])
- 一个小小的操作空调程序
- Android程序:IDE Eclipse主题样式和提示设置