11/27 PAT(甲级) 训练四

单词组 :

1117 Eddington Number (25 分)

1118 Birds in Forest (25 分)

1120 Friend Numbers (20 分)

1121 Damn Single (25 分)

题型. 已知前序后序求中序是否唯一。

1119 Pre- and Post-order Traversals (30 分)

Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can be determined by a given pair of postorder and inorder traversal sequences, or preorder and inorder traversal sequences. However, if only the postorder and preorder traversal sequences are given, the corresponding tree may no longer be unique.
Now given a pair of postorder and preorder traversal sequences, you are supposed to output the corresponding inorder traversal sequence of the tree. If the tree is not unique, simply output any one of them.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤ 30), the total number of nodes in the binary tree. The second line gives the preorder sequence and the third line gives the postorder sequence. All the numbers in a line are separated by a space.

Output Specification:

For each test case, first printf in a line Yes if the tree is unique, or No if not. Then print in the next line the inorder traversal sequence of the corresponding binary tree. If the solution is not unique, any answer would do. It is guaranteed that at least one solution exists. All the numbers in a line must be separated by exactly one space, and there must be no extra space at the end of the line.

Sample Input 1:

7
1 2 3 4 6 7 5
2 6 7 4 5 3 1

Sample Output 1:

Yes
2 1 6 4 7 3 5

Sample Input 2:

4
1 2 3 4
2 4 3 1

Sample Output 2:

No
2 1 3 4

先序的第一个结点和后序的最后一个结点是根节点,先序遍历根结点往后的第一个是左子树或右子树的结点,后序遍历中寻找该结点,该结点往左包括该结点是根结点的左子树,右边部分是右子树,然后分别进行递归。
当某个父节点只存在一个子节点时,因为不能确定子节点为左子树还是右子树,所以不能确定

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string.h>
#include <cmath>
#include <math.h>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <stack>
using namespace std;
const int MAX=1050;
const int INF=0x3f3f3f3f;
int pre[MAX],in[MAX],post[MAX];
int flag=1,Index=0;
void create(int prel,int prer,int postl,int postr)
{
    if(prel>prer)
        return ;
    if(prel==prer)
    {
        in[Index++]=pre[prel];//中序遍历的叶结点的值
        return ;
    }
    int cnt=0;//左子树序列长
    while(post[postl+cnt]!=pre[prel+1])
    {
        cnt++;
    }
    cnt++;
    if(cnt==1&&prel+1==prer)
    {
        flag=0;
    }
    //递归左子树
    create(prel+1,prel+cnt,postl,postl+cnt-1);
    in[Index++]=pre[prel];//中序遍历的根节点的值
    create(prel+cnt+1,prer,postl+cnt,postr-1);
}
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&pre[i]);
    }
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&post[i]);
    }
    create(1,n,1,n);
    if(flag==0)
    {
        printf("No\n");
    }
    else
        printf("Yes\n");
    for(int i=0;i<Index;i++)
    {
        printf("%d",in[i]);
        if(i!=Index-1)
            printf(" ");
    }
    printf("\n");
   return 0;
}



1122 Hamiltonian Cycle (25 分)

The “Hamilton cycle problem” is to find a simple cycle that contains every vertex in a graph. Such a cycle is called a “Hamiltonian cycle”.In this problem, you are supposed to tell if a given cycle is a Hamiltonian cycle.

Input Specification:

Each input file contains one test case. For each case, the first line contains 2 positive integers N (2<N≤200), the number of vertices, and M, the number of edges in an undirected graph. Then M lines follow, each describes an edge in the format Vertex1 Vertex2, where the vertices are numbered from 1 to N. The next line gives a positive integer K which is the number of queries, followed by K lines of queries, each in the format:n V1 V​2​​ … V​n
​where n is the number of vertices in the list, and V​i​​ 's are the vertices on a path.

Output Specification:

For each query, print in a line YES if the path does form a Hamiltonian cycle, or NO if not.

Sample Input:

6 10
6 2
3 4
1 5
2 5
3 1
4 1
1 6
6 3
1 2
4 5
6
7 5 1 4 3 6 2 5
6 5 1 4 3 6 2
9 6 2 1 6 3 4 5 2 6
4 1 2 5 1
7 6 1 3 4 5 2 6
7 6 1 2 5 4 3 1

Sample Output:

YES
NO
NO
NO
YES
NO

n不等于N+1;
相邻两个结点不相连;
头尾两个结点不一样;
环没有覆盖所有结点

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string.h>
#include <cmath>
#include <math.h>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <stack>
using namespace std;
const int MAX=250;
const int INF=0x3f3f3f3f;
int n,m;
int G[MAX][MAX];
int num[MAX];
set<int>st;
int main()
{
    int n,m,t;
    memset(G,0,sizeof(G));
    scanf("%d%d",&n,&m);
    for(int i=0;i<m;i++)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        G[x][y]=G[y][x]=1;
    }
    scanf("%d",&t);
    for(int i=0;i<t;i++)
    {
        int flag=0;
        st.clear();
        int k;
        scanf("%d",&k);
        for(int j=0;j<k;j++)
        {
            scanf("%d",&num[j]);
            st.insert(num[j]);
        }
        if(k!=n+1||num[0]!=num[k-1])
            flag=1;
        for(int j=0;j<k-1;j++)
        {
            if(G[num[j]][num[j+1]]==0)
                flag=1;
        }
        if(st.size()<n)
            flag=1;
        if(flag==1)
            printf("NO\n");
        else
            printf("YES\n");
    }
   return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值