Square

Time Limit : 10000/5000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 88   Accepted Submission(s) : 37

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

Problem Description

Given a set of sticks of various lengths, is it possible to join them end-to-end to form a square?

Input

The first line of input contains N, the number of test cases. Each test case begins with an integer 4 <= M <= 20, the number of sticks. M integers follow; each gives the length of a stick - an integer between 1 and 10,000.

Output

For each case, output a line containing "yes" if is is possible to form a square; otherwise output "no".

Sample Input

3
4 1 1 1 1
5 10 20 30 40 50
8 1 7 2 6 4 4 3 5

Sample Output

yes
no
yes

Source

University of Waterloo Local Contest 2002.09.21

#include <iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int t,i,j,n,m,flag,sum,ans;
int vis[],a[];
int cmp(int a,int b)
{
return a>b;
}
void dfs(int k,int l,int edge)
{
if(edge==)
{
flag=;
return;
}
// if(flag) return;
for(int i=k;i<=n;i++)
if (!vis[i])
if (l+a[i]<=sum)
{
vis[i]=;
if (l+a[i]==sum) dfs(,,edge+);
else dfs(i+,l+a[i],edge);
if(flag) return;
vis[i]=;
}
return;
}
int main()
{ scanf("%d",&t);
for(;t>;t--)
{
scanf("%d",&n);
sum=;
for(i=;i<=n;i++)
{
scanf("%d",&a[i]);
sum+=a[i];
} if (sum%!=)
{
printf("no\n");
continue;
} sort(a+,a++n,cmp);
sum=sum/; if (a[]>sum)
{
printf("no\n");
continue;
}
memset(vis,,sizeof(vis));
flag=;
dfs(,,);
if(flag) printf("yes\n");
else printf("no\n");
}
return ;
}

HDU1518:Square(DFS)的更多相关文章

  1. HDU1518 Square(DFS,剪枝是关键呀)

    Square Time Limit : 10000/5000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submi ...

  2. HDU-1518 Square(DFS)

    Square Time Limit : 10000/5000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submi ...

  3. HDU1518 Square(DFS)

    Square Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Su ...

  4. HDU1518 Square(DFS) 2016-07-24 15:08 49人阅读 评论(0) 收藏

    Square Problem Description Given a set of sticks of various lengths, is it possible to join them end ...

  5. 1317: Square(DFS+剪枝)

    Description Given a set of sticks of various lengths, is it possible to join them end-to-end to form ...

  6. hdu Square DFS

    Square Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  7. HDU1518 Square 【剪枝】

    Square Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Su ...

  8. HDU1518 Square

    #include<stdio.h> #include<string.h> #include<math.h> #include<stdlib.h> #de ...

  9. POJ 2362 Square DFS

    传送门:http://poj.org/problem?id=2362 题目大意: 给一些不同长度的棍棒,问是否可能组成正方形. 学习了写得很好的dfs 赶紧去玩博饼了.....晚上三个地方有约.... ...

随机推荐

  1. javascript显式类型的转换

    显式类型转换目的:为了使代码变得清晰易读,而做显示类型的转换常使用的函数:Boolean(),String(),Number()或Object()如:Nunber(5) //5String(true) ...

  2. SQL STUFF函数 拼接字符串

    今日看到一篇文章,是关于和并列的,也研究了下,还是不错的 要这种效果. create table tb(idint, value varchar(10)) insert into tbvalues(1 ...

  3. (转)用JMX监测JVM的运行参数

    翻译自http://java.sun.com/javase/6/docs/technotes/guides/management/agent.html  用JMX管理你的JVMJMX是Java管理扩展 ...

  4. Windows进程间通信(中)

    二.文件映射 文件映射(Memory-Mapped Files)能使进程把文件内容当作进程地址区间一块内存那样来对待.因此,进程不必使用文件I/O操作,只需简单的指针操作就可读取和修改文件的内容. W ...

  5. Python logging模块使用记录

    1.简单的将日志打印到屏幕 import logging logging.debug('This is debug message') logging.info('This is info messa ...

  6. leetcode415---字符串大数相加

    Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. ...

  7. 注解 @ 或者 Alt+/ 不提示 或者提示 no default propsals 解决方案

  8. hdu_2446_Shell Pyramid(数学,二分)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2446 题意:题面很大,有用的就那么几句,意思就是用自然数来堆它画的那个金字塔,比如第一个金字塔的第一个 ...

  9. 运行出第一个程序Hello World、第二个程序网页浏览器

    很长时间没有发博客记录我的奋斗历程了,原因不外乎遇到了对我而言“巨大的”困难. 经历了长期的找不到合适的教材.找不到Xcode资源.运行不出例程的痛苦以后,近日终于走上正轨. 现在补发一个过去2个月来 ...

  10. springboot 打包

    springboot 打包 先clean 然后 maven package 通过命令java -jar target/GoshenWepPro-0.1.0.jar运行程序