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 暴力的更多相关文章

  1. TC SRM 665 DIV2 A LuckyXor 暴力

    LuckyXorTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 TC Description A lucky number is a positive int ...

  2. TC SRM 663 div2 A ChessFloor 暴力

    ChessFloor Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 TC Description Samantha is renovating a squa ...

  3. TC SRM 664 div2 A BearCheats 暴力

     BearCheats Problem Statement    Limak is an old brown bear. Because of his bad eyesight he sometime ...

  4. TC SRM 663 div2 B AABB 逆推

    AABB Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 TC Description One day, Jamie noticed that many En ...

  5. TC SRM 607 DIV2

    求拼接完成后的字符串包含的子回文串的数目,一开始还用暴力去做,想都不用想 肯定超时了. 复习了一下求最长子回文串的算法,发现可以类似解决. 给相邻字符之间添加一个'@'字符,这样所有的回文串都是奇数长 ...

  6. TC SRM 593 DIV2 1000

    很棒的DP,不过没想出,看题解了..思维很重要. #include <iostream> #include <cstdio> #include <cstring> ...

  7. TC SRM 591 DIV2 1000

    很不错的一题,非常巧妙的用DP顺序解决这个问题... 可以发现,只和A里面最小的有关系... #include <cstdio> #include <cstring> #inc ...

  8. tc srm 636 div2 500

    100的数据直接暴力就行,想多了... ac的代码: #include <iostream> #include <cstdio> #include <cstring> ...

  9. TC SRM 664 div2 B BearPlaysDiv2 bfs

    BearPlaysDiv2 Problem Statement    Limak is a little bear who loves to play. Today he is playing by ...

随机推荐

  1. 在英文 sql2005中 比较nvarchar 与 varchar的速度

    declare @str1 varchar(max); declare @count int; ; print 'begin' begin set @str1 = @str1 + '*'; ; end ...

  2. MYSQL内存

    全局内存(BASE MEMORY) 线程内存(MEMORY PER CONNECTION) max_conecctions:整个 MySQL 允许的最大连接数; max_user_connection ...

  3. HDU 5407 CRB and Candies

    题意:给一个正整数k,求lcm((k, 0), (k, 1), ..., (k, k)) 解法:在oeis上查了这个序列,得知答案即为lcm(1, 2, ..., k + 1) / (k + 1),而 ...

  4. C# DataGridView的列对象属性探讨 (未完待续)

    比较难的几个属性的释义[1]:

  5. Spring 事务管理原理探究

    此处先粘贴出Spring事务需要的配置内容: 1.Spring事务管理器的配置文件: 2.一个普通的JPA框架(此处是mybatis)的配置文件: <bean id="sqlSessi ...

  6. 通过ListActivity使用ListView布局方法

    先简单的介绍一下ListActivity ListActivity是一个专门显示ListView的Activity类,它内置了ListView对象,只要我们设置了数据源,就会自动地显示出来.ListA ...

  7. android 官网处理图片 代码

    /** * 获取压缩后的图片 (官网大图片加载对应代码) * * @param res * @param resId * @param reqWidth * 所需图片压缩尺寸最小宽度 * @param ...

  8. lightoj 1024 (高精度乘单精度)

    题意:给你一些数,求它们的最小公倍数,结果可能会很大. 统计出每个素因子出现的最大次数,把他们相乘即可,需要高精度. #include<cmath> #include<cstdio& ...

  9. Web服务器(Apache)虚拟主机的配置

    一.定义    所谓虚拟主机是指在一台服务器里运行几个网站,提供WEB.FTP.Mail等服务.    二.虚拟主机的实现方法有三种:    基于IP的方法,基于主机名的方法和基于端口的法官法.    ...

  10. 你是怎么理解“MVC”的

    MVC就是三个字母的组合,M-模型, V-视图, C-控制器. 这些在百度上随便一索就可以索到,而且网上对这三个部分的解释又过于笼统,使人没法完全理解MVC的含义.   这里我简单的谈谈我对MVC这三 ...