我找不出自己的代码哪里错了你知道吗
2017-03-26 10:35
288 查看
1118. Birds in Forest (25)
时间限制150 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
Some scientists took pictures of thousands of birds in a forest. Assume that all the birds appear in the same picture belong to the same tree. You are supposed to help the scientists to count the maximum number of trees in the forest, and for any pair of birds,
tell if they are on the same tree.
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive number N (<= 104) which is the number of pictures. Then N lines follow, each describes a picture in the format:
K B1 B2 ... BK
where K is the number of birds in this picture, and Bi's are the indices of birds. It is guaranteed that the birds in all the pictures are numbered continuously from 1 to some number that is no more than 104.
After the pictures there is a positive number Q (<= 104) which is the number of queries. Then Q lines follow, each contains the indices of two birds.
Output Specification:
For each test case, first output in a line the maximum possible number of trees and the number of birds. Then for each query, print in a line "Yes" if the two birds belong to the same tree, or "No" if not.
Sample Input:
4 3 10 1 2 2 3 4 4 1 5 7 8 3 9 6 4 2 10 5 3 7
Sample Output:
2 10 YesNo
我自己写的代码,我自己和我同学找了半天也没找到错误在哪,只得了17分,感觉很蓝瘦。以后闲着没事在来找吧。
#include<bits/stdc++.h>
#define MAXN 10001
using namespace std;
int N,K,Q;
int pre[MAXN];
int a[MAXN];
int book[MAXN];
set<int> st;
int find(int n)
{
int r=n;
while(pre[r]!=r) r=pre[r];
int x=n,y;
while(pre[x]!=r)
{
y=pre[x];
pre[x]=r;
x=y;
}
return r;
}
void union_(int x,int y)
{
int a=find(x);
int b=find(y);
if(a!=b)
pre[a]=b;
}
int main()
{
cin>>N;
int i,j,treenum=0;
for(i=1;i<=MAXN;i++)
pre[i]=i;
memset(book,0,sizeof(book));
st.clear();
for(i=0;i<N;i++)
{
cin>>K;
for(j=0;j<K;j++)
{
cin>>a[j];
st.insert(a[j]);
}
for(j=0;j<K;j++)
find(a[j]);
int flag=0,tag;
for(j=0;j<K;j++)
{
if(book[a[j]]==1) //已经在一棵树里了
{
flag=1;
tag=j;
break;
}
}
if(flag==0)
{
treenum++;
book[a[0]]=1;
for(j=1;j<K;j++)
{
book[a[j]]=1;
union_(a[0],a[j]);
}
}
else
{
for(j=0;j<K;j++)
{
book[a[j]]=1;
union_(a[j],a[tag]);
}
}
}
cout<<treenum<<' '<<st.size()<<endl;
cin>>Q;
int sx,sy;
while(Q--)
{
cin>>sx>>sy;
if(find(sx)==find(sy)) cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
return 0;
}
下面贴的是别人的代码,其实我对于并查集现在来说有一个很大的认识,就是对于Union_这个操作尽量用父亲节点进行操作。
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = 10000 + 10;
bool flag[maxn] = { false };
int N, K, B;
int father[maxn];
int findFather(int x)
{
if (x == father[x])
return x;
else
{
int F = findFather(father[x]);
father[x] = F;
return F;
}
}
int main()
{
for (int i = 0; i < maxn; i++)
{
father[i] = i;
}
scanf("%d", &N); int count = 0;
for (int i = 0; i < N; i++)
{
scanf("%d", &K);
int bf;
for (int j = 0; j < K; j++)
{
scanf("%d", &B);
if (!flag[B])
{
count++;
flag[B] = true;
}
if (j == 0)
{
bf = findFather(B);
}
else if(findFather(B) != findFather(bf))
{
father[findFather(B)] = findFather(bf);
}
}
}
int trees = 0;
for (int i = 1; i <= count; i++)
{
if (father[i] == i)
{
trees++;
}
}
printf("%d %d\n", trees, count);
int Q;
scanf("%d", &Q);
int a, b;
for (int i = 0; i < Q; i++)
{
scanf("%d %d", &a, &b);
if (findFather(a) == findFather(b))
printf("Yes\n");
else
printf("No\n");
}
return 0;
}
相关文章推荐
- 怎么知道自己的代码或者循环执行时候,同时有几个功能单元在并行
- HDU Today(自己的代码不知道哪里错了一直没A抄袭大神的)
- 有人知道吗?发个后台管理,那个弹出消息的层又自己消失的代码??
- 知道自己在哪里,就不会迷失
- 知道自己要去哪里,全世界都会为你让路
- java 实现.Z解压(代码是忘记在哪里找到的,自己整合了一下)
- 知道你的代码哪里出了问题吗?——由控件实例化引发的运行崩溃
- 如果你知道自己要去哪里,整个世界会为你让路
- 学校自己挂的题目。。。我也不知道是哪里的。。。
- 代码加密:加密Java源代码,保护自己的版权!
- 怎样写好自己的程序代码
- 刚把过去一些vc共3G多参考的代码恢复到系统中,可惜自己写的有几个无法恢复了
- 《水晶之约》的完整代码以及一些思想很值得学习---优秀程序的代码固然能更提高自己
- 买开发板后突然觉得自己知道的很少
- 你知道java每一个版本都有自己的名字吗?
- 常用的一些代码,自己觉得比较好的
- 在我们人生的大道上,肯定会遇到许许多多的困难。但我们是不是都知道,在前进的道路上,搬开别人脚下的绊脚石,有时恰恰是为自己铺路?
- 自己修改后求1到1000的完数代码。
- 自己写的第一段AOP代码。
- 在水晶报表中动态的添加自己所需要的的字段的代码