您的位置:首页 > 编程语言 > C#

BC#62C题求二叉树不同节点数的子树的个数

2015-11-01 19:57 561 查看
这次只做了俩道题,而且A题WA了2发(然后突然发现仅仅是数组开小了,hash表要开到2000),B题WA了四发(考虑清楚各种特判就行了),室友和我一起做的,

他以前是搞acm的,但好久不搞了,虽然比赛的时候也只做出俩题,但是排名在我

前面,而且比赛后还做出了第三题没有看题解,这难道就是智商压制么,汗颜啊。

C题其实只要从二叉树最后面往前递归,把数组加入set,即可实现判重,注意每层的节点数目,和这层前面的节点总数目关系,便于递归。

A题代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<set>
#include<cmath>
#include<climits>
#include<vector>
#include<cfloat>
#include<queue>
#include<cctype>
#include<cstdlib>
#include<string>
#define LL long long
using namespace std;

int a[3000],b[110];
int main()
{
int n;
while(cin>>n)
{
memset(a,0,sizeof(a));
for(int i=0;i<n;i++)
{
scanf("%d",&b[i]);
a[b[i]]++;
}
int flag=0;
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
a[b[i]]--;
a[b[j]]--;
int tem=b[i]+b[j];
if(a[tem])
{
flag=1;
break;
}
a[b[i]]++;
a[b[j]]++;
}
if(flag)
break;
}
if(flag)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
return 0;
}


B提代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<set>
#include<cmath>
#include<climits>
#include<vector>
#include<cfloat>
#include<queue>
#include<cctype>
#include<cstdlib>
#include<string>
#define LL long long
using namespace std;

int main()
{
int n,s,t;
while(cin>>n>>s>>t)
{
if(n==1&&s==1&&t==1)
{
cout<<0<<endl;
continue;
}
if(s==t)
cout<<-1<<endl;
else if((s==1&&t==n)||(s==n&&t==1))
cout<<0<<endl;
else if(abs(s-t)==1)
cout<<1<<endl;
else if(t>1&&t<n&&(s==1||s==n))
cout<<1<<endl;
else
cout<<2<<endl;
}
return 0;
}


C题代码,

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<set>
#include<cmath>
#include<climits>
#include<vector>
#include<cfloat>
#include<queue>
#include<cctype>
#include<cstdlib>
#include<string>
#define LL long long
using namespace std;

set<LL> s;
void get(LL n)
{
if(n&&!s.count(n))
{
s.insert(n);
int ans=0;
LL t=0;
while((t*2+1)<=n)
t=t*2+1;
LL r=n-t;
if(r<=(t+1)/2)
{
get((t-1)/2+r);
get((t-1)/2);
}
else
{
get((t-1)/2+(t+1)/2);
get((t-1)/2+r-(t+1)/2);
}
}
}
int main()
{
LL n;
while(cin>>n)
{
s.clear();
get(n);
cout<<s.size()<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: