HDU1213-How Many Tables (并查集)

来源:互联网 发布:手机淘宝下载安装2017免费下载 编辑:程序博客网 时间:2024/06/01 18:56

Trouble

Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6166 Accepted Submission(s): 1722

Problem Description

Hassan is in trouble. His mathematics teacher has given him a very difficult problem called 5-sum. Please help him.
The 5-sum problem is defined as follows: Given 5 sets S_1,…,S_5 of n integer numbers each, is there a_1 in S_1,…,a_5 in S_5 such that a_1+…+a_5=0?

Input

First line of input contains a single integer N (1≤N≤50). N test-cases follow. First line of each test-case contains a single integer n (1<=n<=200). 5 lines follow each containing n integer numbers in range [-10^15, 1 0^15]. I-th line denotes set S_i for 1<=i<=5.

Output

For each test-case output “Yes” (without quotes) if there are a_1 in S_1,…,a_5 in S_5 such that a_1+…+a_5=0, otherwise output “No”.

Sample Input

2
2
1 -1
1 -1
1 -1
1 -1
1 -1
3
1 2 3
-1 -2 -3
4 5 6
-1 3 2
-4 -10 -1

Sample Output

No
Yes

Source

2012 Multi-University Training Contest 4

Recommend

zhoujiaqi2010

题意:

求联通快个数
裸并查集

#include<cstdio>#include<cstring> using namespace std;const int maxn=3000+5;int fa[maxn],n,m,x,y,size;inline int get(int u){  if(fa[u]==u)return u;  return fa[u]=get(fa[u]);}int main(){  int T;  scanf("%d",&T);   while(T--)  {    scanf("%d%d",&n,&m);    for(int i=1;i<=n;++i)fa[i]=i;    size=n;    for(int i=1;i<=m;++i)    {      scanf("%d%d",&x,&y);      int fx=get(x),fy=get(y);      if(fx!=fy)      {        fa[fy]=fx;        size--;      }    }    printf("%d\n",size);  }  return 0;}