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. 关于逆波兰式的c++实现

    正常的表达式 逆波兰表达式 a+b ---> a,b,+ a+(b-c) ---> a,b,c,-,+ a+(b-c)*d ---> a,b,c,-,d,*,+ a+d*(b-c)- ...

  2. USB 设备插拔事件处理

            Windows 系统下,设备连接至电脑或从电脑移除,系统会广播一条 WM_DEVICECHANGE 消息到所有应用程序,在程序的消息处理函数中可以对事件进行相应. 1: class C ...

  3. Python 之 geturl 学习

    geturl为response对象的方法,由于有时候得到的网站url并不是真正的初始url而是通过重定向获得的,所以可以通过geturl方法获取真实的url.测试代码如下: from urllib2 ...

  4. memset用法总结

    memset是计算机中C/C++语言函数.将s所指向的某一块内存中的前n个 字节的内容全部设置为ch指定的ASCII值, 第一个值为指定的内存地址,块的大小由第三个参数指定,这个函数通常为新申请的内存 ...

  5. 正确使用Core Data多线程的3种方式

    在#Pragma Conference 2015会议上,Marcus Zarra,撰写过关于Core Data和Core Animation的书,叙述了三种在多线程环境下使用Core Data的方法并 ...

  6. <meta http-equiv="X-UA-Compatible" content="IE=Edge">

    1.X-UA-Compatible X-UA-Compatible是IE8的一个专有<meta>属性,它告诉IE8采用何种IE版本去渲染网页,在html的<head>标签中使用 ...

  7. 分析函数 over用法 之row_number() runk_number

    分析函数用于计算基于组的某种聚合值,每个组返回多个行,而聚合函数每个组只返回一个行 表: create table TB_SCORE ( id NUMBER(10), class VARCHAR2(1 ...

  8. Minigui3.0 自定义遥控输入引擎

    本人最近在从事minigui的开发工作,使用的gui版本为最新的3.0.12,平台环境为海思的HI3515. 在历经千辛万苦,终于将gui成功的移植到了开发板上,这里不多赘述,没有移植成功的朋友可以点 ...

  9. Codeforces 691D Swaps in Permutation

    Time Limit:5000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Prac ...

  10. mysql编码设置 [http://blog.knowsky.com/254652.htm]

    MYSQL 2009-09-11 15:37 阅读73 评论1 字号: 大大 中中 小小mysql> SHOW VARIABLES LIKE 'character_set_%';+------- ...