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 ...
随机推荐
- Java-泛型编程-使用通配符? extends 和 ? super
原文地址:http://blog.csdn.net/fw0124/article/details/42296283 泛型中使用通配符有两种形式:子类型限定<? extends xxx>和超 ...
- android之AlarmManager 全局定时器
AlarmManager实质是一个全局的定时器,是Android中常用的一种系统级别的提示服务,在指定时间或周期性启动其它组件(包括Activity,Service,BroadcastReceiver ...
- 【html】页面制作规范文档
每天都在写html/css/js代码,总结的一些页面制作的规范 文件命名规范 1) 文件目录.文件名称统一用小写的英文字母.数字.下划线组合,文件名要与表现的内容相近,不到万不得已不要以拼音作为名称, ...
- matlab 学习
http://blog.sina.com.cn/s/blog_7086379501012pc5.html <a href = "http://blog.sina.com.cn/s/bl ...
- javascript 面向对象制作坦克大战 (一)
PS:这个坦克大战是在网上下的一段源码之后,自己进行的重写. 写这个的目的是为了巩固自己这段时间对js的学习.整理到博客上,算是对自己近端时间学习js的一个整理. 同时也希望可以帮助到学习js的园 ...
- 【暑假】[实用数据结构]UVAlive 3135 Argus
UVAlive 3135 Argus Argus Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %l ...
- Tkinter教程之Button篇(1)
本文转载自:http://blog.csdn.net/jcodeer/article/details/1811298 #Tkinter教程之Button篇(1)#Button功能触发事件'''1.一个 ...
- Javascript手记-垃圾收集
如果有人问.net的垃圾回收,大家会马上想到gc,那如果有人问你javascript如何进行内存管理的呢?挠挠头,一口香瓜,听我细细道来! javascript具有自动垃圾收集机制,执行环境会负责管理 ...
- MapReduce TopK统计加排序
Hadoop技术内幕中指出Top K算法有两步,一是统计词频,二是找出词频最高的前K个词.在网上找了很多MapReduce的Top K案例,这些案例都只有排序功能,所以自己写了个案例. 这个案例分两个 ...
- Another mysql daemon already running with the same unix socket
在国外网站发现的解决方法. 原因多个Mysql进程使用了同一个socket. 两个方法解决: 第一个是立即关机 使用命令 shutdown -h now 关机,关机后在启动,进程就停止了. 第二个直接 ...