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. TCP/IP详解学习笔记(1)-基本概念

    为什么会有TCP/IP协议 在世界上各地,各种各样的电脑运行着各自不同的操作系统为大家服务,这些电脑在表达同一种信息的时候所使用的方法是千差万别.就好像圣经中上帝打乱了各地人的口音,让他们无法合作一样 ...

  2. ubuntu-12.04.1-desktop-x64下JDK环境的安装与配置

    1.上oracle官网下载最新的JDK.在这里,我的系统是ubuntu-12.04.1-desktop-amd64,目前位置JDK的最新版本位7u9.jdk-for-linux有两种安装包,一种是rp ...

  3. 一些网站的Android客户端

    实际上就是浏览器(WebView),外面包装上了用户体验更好的外壳

  4. [Everyday Mathematics]20150123

    设 $A,B$ 是同阶方阵, 满足 $\rank(AB-BA)=1$. 试证: $(AB-BA)^2=0$.

  5. 新版本ubuntu13.10软件安装

    问题1:如何解决ubunt13.04不能和主机共享文件的问题 . 安装VMware Tools 网上有很多的资料,这里没有给出. . 设置共享文件夹目录 ) 在VMware虚拟机窗口,选择VM-> ...

  6. CSS基础(背景、文本、列表、表格、轮廓)

    CSS 背景属性 属性 描述 background 简写属性,作用是将背景属性设置在一个声明中. background-attachment 背景图像是否固定或者随着页面的其余部分滚动. backgr ...

  7. mysql 查看锁表解锁

    -- 查看那些表锁到了show OPEN TABLES where In_use > 0;-- 查看进程号show processlist;--删除进程 kill 42236:

  8. hadoop2.6.0汇总:新增功能最新编译 32位、64位安装、源码包、API下载及部署文档

    相关内容: hadoop2.5.2汇总:新增功能最新编译 32位.64位安装.源码包.API.eclipse插件下载Hadoop2.5 Eclipse插件制作.连接集群视频.及hadoop-eclip ...

  9. [转]SQL中char、varchar、nvarchar的区别

    char    char是定长的,也就是当你输入的字符小于你指定的数目时,char(8),你输入的字符小于8时,它会再后面补空值.当你输入的字符大于指定的数时,它会截取超出的字符.   nvarcha ...

  10. log4j2使用总结

    一.Log4j有三个主要的组件:Loggers,Appenders和Layouts,这里可简单理解为日志级别,日志要输出的地方和日志格式 1. Logger Logger的日志级别有6级,分别是TRA ...