TC SRM 665 DIV2 B LuckyCycle 暴力
LuckyCycle
Time Limit: 20 Sec
Memory Limit: 256 MB
题目连接
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87326#problem/B
Description
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 <int>s edge1, edge2, and weight. Each of these vector <int>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 <int> 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 <int> instead.
Input
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.
Output
vector <int> getEdge(vector <int> edge1, vector <int> edge2, vector <int> weight)
Sample Input
{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}
Sample Output
Returns: {1, 12, 7 }
HINT
题意
给你一颗树,然后让你连两个点,要求连成一个环,然后这个环上面4的边数和7的边数是一样的
题解:
数据范围只有100,所以直接n^3暴力就好了
代码:
- #include<stdio.h>
- #include<string.h>
- #include<iostream>
- #include<algorithm>
- #include<vector>
- using namespace std;
- class LuckyCycle{
- public:
- int Ma[][];
- int vis[];
- int ans1=;
- int ans2=;
- struct node
- {
- int x,y;
- };
- vector<node> E[];
- void dfs(int x,int y,int a,int b)
- {
- if(x==y)
- {
- ans1=a;
- ans2=b;
- return;
- }
- if(vis[x])
- return;
- vis[x]=;
- for(int i=;i<E[x].size();i++)
- {
- if(E[x][i].y==)
- dfs(E[x][i].x,y,a+,b);
- else
- dfs(E[x][i].x,y,a,b+);
- }
- }
- int check(int i,int j)
- {
- if(Ma[i][j]!=)
- return ;
- memset(vis,,sizeof(vis));
- dfs(i,j,,);
- if(ans1==ans2-)
- return ;
- if(ans1-==ans2)
- return ;
- return ;
- }
- vector <int> getEdge(vector <int> edge1, vector <int> edge2, vector <int> weight)
- {
- memset(Ma,,sizeof(Ma));
- for(int i=;i<=edge1.size()+;i++)
- Ma[i][i]=;
- for(int i=;i<edge1.size();i++)
- {
- Ma[edge1[i]][edge2[i]]=weight[i];
- Ma[edge2[i]][edge1[i]]=weight[i];
- E[edge1[i]].push_back((node){edge2[i],weight[i]});
- E[edge2[i]].push_back((node){edge1[i],weight[i]});
- }
- vector<int> ans;
- int flag = ;
- for(int i=;i<=edge1.size()+;i++)
- {
- for(int j=;j<=edge1.size()+;j++)
- {
- ans1=;
- ans2=;
- int x = check(i,j);
- if(x>)
- {
- ans.push_back(i);
- ans.push_back(j);
- ans.push_back(x);
- flag = ;
- break;
- }
- }
- if(flag == )
- break;
- }
- return ans;
- }
- };
TC SRM 665 DIV2 B LuckyCycle 暴力的更多相关文章
- TC SRM 665 DIV2 A LuckyXor 暴力
LuckyXorTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 TC Description A lucky number is a positive int ...
- TC SRM 663 div2 A ChessFloor 暴力
ChessFloor Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 TC Description Samantha is renovating a squa ...
- TC SRM 664 div2 A BearCheats 暴力
BearCheats Problem Statement Limak is an old brown bear. Because of his bad eyesight he sometime ...
- TC SRM 663 div2 B AABB 逆推
AABB Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 TC Description One day, Jamie noticed that many En ...
- TC SRM 607 DIV2
求拼接完成后的字符串包含的子回文串的数目,一开始还用暴力去做,想都不用想 肯定超时了. 复习了一下求最长子回文串的算法,发现可以类似解决. 给相邻字符之间添加一个'@'字符,这样所有的回文串都是奇数长 ...
- TC SRM 593 DIV2 1000
很棒的DP,不过没想出,看题解了..思维很重要. #include <iostream> #include <cstdio> #include <cstring> ...
- TC SRM 591 DIV2 1000
很不错的一题,非常巧妙的用DP顺序解决这个问题... 可以发现,只和A里面最小的有关系... #include <cstdio> #include <cstring> #inc ...
- tc srm 636 div2 500
100的数据直接暴力就行,想多了... ac的代码: #include <iostream> #include <cstdio> #include <cstring> ...
- TC SRM 664 div2 B BearPlaysDiv2 bfs
BearPlaysDiv2 Problem Statement Limak is a little bear who loves to play. Today he is playing by ...
随机推荐
- 最好用的汉字转拼音代码PinYin4Objc(PinYin4J的objc版本)
转:https://github.com/kimziv/PinYin4Objc 最好用的汉字转拼音代码PinYin4Objc(PinYin4J的objc版本)(更新到v1.1.1,增加block异步处 ...
- hdu 3172 Virtual Friends(并查集)University of Waterloo Local Contest 2008.09
题目比较简单,但作为长久不写题之后的热身题还是不错的. 统计每组朋友的朋友圈的大小. 如果a和b是朋友,这个朋友圈的大小为2,如果b和c也是朋友,那么a和c也是朋友,此时这个朋友圈的大小为3. 输入t ...
- [C++]cin读取回车键
最近碰到一个问题,就是从控制台读取一组数,如: 12 23 34 56 若是使用 int data; while ( cin >> data ) {//...} 当回车后,不能有效转换到后 ...
- HDU 4911 Inversion
http://acm.hdu.edu.cn/showproblem.php?pid=4911 归并排序求逆对数. Inversion Time Limit: 2000/1000 MS (Java/ ...
- bjfu1252 贪心
题目意思是给出一些开区间,这些区间有的相交,有的不相交,问你能否选出一些区间,使这些区间之间都不相交,并且选出的区间数最大. 这是个典型的贪心问题了.按区间的结束位置排序,然后顺序地选取区间,只要当前 ...
- 仿酷狗音乐播放器开发日志二十七 用ole为窗体增加文件拖动功能(附源码)
转载请说明原出处,谢谢~~ 中秋到了,出去玩了几天.今天把仿酷狗程序做了收尾,已经开发完成了,下一篇博客把完结的情况说一下.在这篇博客里说一下使用OLE为窗体增加文件拖拽的功能.使用播放器,我更喜欢直 ...
- 基于wke封装的duilib的webkit浏览器控件,可以c++与js互交,源码及demo下载地址
转载请说明原出处,谢谢~~ 前些日子用wke内核封装了duilib的webkit浏览器控件,好多群里朋友私聊我希望可以我公布源码,今天把这个控件的源码和使用demo公布.其实这个控件封装起来没什么难度 ...
- matlab特征值分解和奇异值分解
特征值分解 函数 eig 格式 d = eig(A) %求矩阵A的特征值d,以向量形式存放d. d = eig(A,B) %A.B为方阵,求广义特征值d,以向量形式存放d. ...
- 怎么利用SQL语句查询数据库中具体某个字段的重复行
select * from [tablename] group by SeriNohaving count(SeriNo)<>1
- windows下安装和配置Weka
Weka是一款免费的,非商业化的,基于java环境下的开源的机器学习以及数据挖掘软件.Weka里含有各种数据挖掘工具:数据预处理,分类与回归,聚类,关联规则和可视化工具. 一.安装weka 我们首先需 ...