Skip to content

Commit

Permalink
jobdu
Browse files Browse the repository at this point in the history
  • Loading branch information
yellowbee686 committed Dec 7, 2013
1 parent 9044511 commit e6ea532
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
51 changes: 51 additions & 0 deletions 1367.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
递归验证某数组是否为BST的后序遍历 需要注意的是当一棵树的节点数<=3时 无论顺序为何 都肯定是BST
*/
#include <cstdio>
using namespace std;
const int MAX=10005;
int a[MAX];

bool ifPost(int low, int up)
{
bool ret=true;
int i,idx=up-1;
if(up-low>2)
{
for (i=low;i<up;++i)
{
if(a[i]>a[up])
{
idx=i;
break;
}
}
for (i=i+1;i<up;++i)
{
if (a[i]<a[up])
{
ret=false;
break;
}
}
if(ret)
ret=ifPost(low,idx-1)&&ifPost(idx,up-1);
}
return ret;
}

int main()
{
int n;
while (scanf("%d",&n)!=EOF)
{
for (int i=0;i<n;++i)
scanf("%d",&a[i]);
bool ans=ifPost(0, n-1);
if(ans)
printf("Yes\n");
else
printf("No\n");
}
return 0;
}
53 changes: 53 additions & 0 deletions 1368.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
类似于pat1053 比它还简单...
*/
#include <cstdio>
#include <vector>
using namespace std;
const int MAX=10005;
int w[MAX];
int a[MAX][2];

void findWay(int node, int weight, vector<int> v)
{
weight-=w[node];
v.push_back(node);
int left=a[node][0], right=a[node][1];
if (weight==0&&left==-1&&right==-1)
{
printf("A path is found:");
for(int i=0;i<v.size();++i)
printf(" %d",v[i]);
printf("\n");
}
else
{
if (left!=-1&&weight>0)
findWay(left,weight,v);
if (right!=-1&&weight>0)
findWay(right,weight,v);
}
}

int main()
{
int n,k,tmp;
while (scanf("%d%d",&n,&k)!=EOF)
{
for(int i=1;i<=n;++i)
{
scanf("%d%d%d",&w[i],&a[i][0],&a[i][1]);
//题目中没说是BST 为了字典序输出要把小的放前 而且因为没有路径用-1表示 因此要验证两个都>0
if (a[i][0]>0&&a[i][1]>0&&a[i][0]>a[i][1])
{
tmp=a[i][0];
a[i][0]=a[i][1];
a[i][1]=tmp;
}
}
vector<int> v;
printf("result:\n");
findWay(1,k,v);
}
return 0;
}

0 comments on commit e6ea532

Please sign in to comment.