数据结构(陈越 、何钦铭)--第五周编程作业

05-树7 堆中的路径 (25分)

将一系列给定数字插入一个初始为空的小顶堆H[]。随后对任意给定的下标i,打印从H[i]到根结点的路径。

输入格式:
每组测试第1行包含2个正整数N和M(≤1000),分别是插入元素的个数、以及需要打印的路径条数。下一行给出区间[-10000, 10000]内的N个要被插入一个初始为空的小顶堆的整数。最后一行给出M个下标。

输出格式:
对输入中给出的每个下标i,在一行中输出从H[i]到根结点的路径上的数据。数字间以1个空格分隔,行末不得有多余空格。

输入样例:
5 3
46 23 26 24 10
5 4 3
输出样例:
24 23 10
46 23 10
26 10

#include <stdio.h>
#define MAXSIZE 1001

int H[MAXSIZE];

void CreatS(int N, int H[]);
void Output(int M, int H[]);

int main()
{
    int N, M;
    scanf("%d %d", &N, &M);

    CreatS(N, H);
    Output(M, H);

    return 0;
}

void CreatS(int N, int H[])
{
    H[0] = -10001;
    int i, j, item;

    for(i = 1; i <= N; i++){
        scanf("%d", &item);
        for(j = i; H[j/2] > item; j/=2){
            H[j] = H[j/2];
        }
        H[j] = item;
    }
}

void Output(int M, int H[])
{
    int i, item, j;
    for(i = 0; i < M; i++){
        int flag = 0;
        scanf("%d", &item);
        for(j = item; j > 0; j/=2){
            if(flag){
                printf(" ");
            }
            printf("%d", H[j]);
            flag = 1;
        }
        printf("\n");
    }
}

05-树8 File Transfer (25分)

We have a network of computers and a list of bi-directional connections. Each of these connections allows a file transfer from one computer to another. Is it possible to send a file from any computer on the network to any other?

Input Specification:
Each input file contains one test case. For each test case, the first line contains N (2≤N≤10^4), the total number of computers in a network. Each computer in the network is then represented by a positive integer between 1 and N. Then in the following lines, the input is given in the format:

I c1 c2
where I stands for inputting a connection between c1 and c2; or

C c1 c2
where C stands for checking if it is possible to transfer files between c1 and c2; or

S
where S stands for stopping this case.

Output Specification:
For each C case, print in one line the word “yes” or “no” if it is possible or impossible to transfer files between c1 and c2, respectively. At the end of each case, print in one line “The network is connected.” if there is a path between any pair of computers; or “There are k components.” where k is the number of connected components in this network.

Sample Input 1:

5
C 3 2
I 3 2
C 1 5
I 4 5
I 2 4
C 3 5
S

Sample Output 1:

no
no
yes
There are 2 components.

Sample Input 2:

5
C 3 2
I 3 2
C 1 5
I 4 5
I 2 4
C 3 5
I 1 3
C 1 5
S

Sample Output 2:

no
no
yes
yes
The network is connected.
#include <stdio.h>
#define MAXSIZE 10000

typedef int ElementType;
typedef int SetName;

void ConnectCom(ElementType Group[]);
void CheckConnection(ElementType Group[]);
SetName FindRoot(SetName a, ElementType Group[]);

int main()
{
    ElementType Group[MAXSIZE];
    int N, i;
    char in;
    scanf("%d", &N);
    for(i = 0; i < N; i++){
        Group[i] = -1;
    }

    do{
        scanf("%c", &in);
        switch(in)
        {
            case 'I':
                ConnectCom(Group);    break;
            case 'C':
                CheckConnection(Group);    break;
        }
    }while(in != 'S');

    int min = 0;
    for(i = 0; i < N; i++){
        if(Group[i] < 0)    min++;
    }
    if(min == 1){
        printf("The network is connected.");
    }else{
        printf("There are %d components.", min);
    }

    return 0;
}

SetName FindRoot(SetName a, ElementType Group[])
{
    if(Group[a] < 0)
        return a;
    else
        return Group[a] = FindRoot(Group[a], Group);
}

void ConnectCom(ElementType Group[])
{
    SetName a, b;
    scanf("%d %d", &a, &b);
    a = FindRoot(a, Group);
    b = FindRoot(b, Group);
    if(Group[a] <= Group[b]){
        Group[a] += Group[b];
        Group[b] = a;
    }else{
        Group[b] += Group[a];
        Group[a] = b;
    }
}

void CheckConnection(ElementType Group[])
{
    SetName a, b;
    scanf("%d %d", &a, &b);
    a = FindRoot(a, Group);
    b = FindRoot(b, Group);
    if(a == b)    printf("yes\n");
    else    printf("no\n");
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

比巧克力巧

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值