Genealogical tree(拓扑排序问题——基础)

Genealogical tree

Time Limit: 1000ms
Memory Limit: 65536KB
64-bit integer IO format: %lld      Java class name: Main
The system of Martians' blood relations is confusing enough. Actually, Martians bud when they want and where they want. They gather together in different groups, so that a Martian can have one parent as well as ten. Nobody will be surprised by a hundred of children. Martians have got used to this and their style of life seems to them natural.
And in the Planetary Council the confusing genealogical system leads to some embarrassment. There meet the worthiest of Martians, and therefore in order to offend nobody in all of the discussions it is used first to give the floor to the old Martians, than to the younger ones and only than to the most young childless assessors. However, the maintenance of this order really is not a trivial task. Not always Martian knows all of his parents (and there's nothing to tell about his grandparents!). But if by a mistake first speak a grandson and only than his young appearing great-grandfather, this is a real scandal.
Your task is to write a program, which would define once and for all, an order that would guarantee that every member of the Council takes the floor earlier than each of his descendants.

Input

The first line of the standard input contains an only number N, 1 <= N <= 100 — a number of members of the Martian Planetary Council. According to the centuries-old tradition members of the Council are enumerated with the natural numbers from 1 up to N. Further, there are exactly N lines, moreover, the I-th line contains a list of I-th member's children. The list of children is a sequence of serial numbers of children in a arbitrary order separated by spaces. The list of children may be empty. The list (even if it is empty) ends with 0.

Output

The standard output should contain in its only line a sequence of speakers' numbers, separated by spaces. If several sequences satisfy the conditions of the problem, you are to write to the standard output any of them. At least one such sequence always exists.

Sample Input

5
0
4 5 1 0
1 0
5 3 0
3 0

Sample Output

2 4 5 3 1


题目很长。。大意大概就是 火星人根我们人类不一样 他们会不管血缘关系 胡乱交配。。。

input里 第一行代表 有N个火星人    后面的N行每一行都分别代表 第i个火星人的孩子

output是让我们按照火星人按我们人类的辈分大小输出一串辈分从大到小的序列

当然,输出的序列不唯一

思路:

把父母和孩子的关系用一个有向图表示,然后用topo排序,从起点(辈分最大)往终点(辈分最小)方向输出答案


关于topo排序,总结了四个需要注意的关键点:

1. 把所有点的入度用  in[size] 储存;

2.找出所有入度为0的点,作为答案储存在ans[size] 中

3.入度为0的点应该先作一个储存  可以储存在队列中(也可以是栈中)

4.一一输出队列中的点,并同时把新的入度为0的点 “push”到队列中

#include <cstdio>
#include <vector>
#include <queue>
#include <string.h>
using namespace std;
int in[1000];       //对应每个点的入度
int ans[1000];      //答案保存在这里
int n;
queue<int> q;       //用来保存入度为0的点
vector<int> v[20];      //用来保存输入值

void toposort()
{
    int k = 1;
    for(int i =1; i <= n; i++ ){
        if(in[i] == 0)
            q.push(i);
    }
    while(!q.empty()){
        int t = q.front();
        int s = v[t].size();
        ans[k++] = t;
        q.pop();
        for(int i = 0; i < s; i++ ){
            int p = v[t][i];
            in[p]--;
            if(in[p] == 0)
                q.push(p);
        }
    }
}

int main()
{
    memset(in, 0, sizeof(in));
    scanf("%d", &n);
    for(int i = 1; i <= n; i++){
        while(1){
            int t;
            scanf("%d", &t);
            if(t == 0)  break;
            v[i].push_back(t);
            in[t]++;
        }
    }
    toposort();
    for(int i = 1; i <= n; i++ )
        printf("%d ", ans[i]);
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
问题描述: 大学的每个专业都要制定教学计划。假设任何专业都有固定的学习年限,每学年含两学期,每学期的时间长度和学分上限值均相等。每个专业开设 课程都是确定的,而且课程在开设时间的安排必须满足先修关系。每门课程有哪些先修课程是确定的,可以有任意多门,也可以没有。每门课恰好占一个学期。试在这样的前提下设计一个教学计划编制程序。 基本要求: (1) 输入参数包括:学期总数,课程总数,一学期的学分上限,每门课的课程号(固定占3位的字母数字串)、学分和直接先修课的课程号。 (2) 允许用户指定下列两种编排策略之一:一是使学生在各学期中的学习负担尽量均匀;二是使课程尽可能地集中在前几个学期中。 (3) 若根据给定的条件问题无解,则报告适当的信息;否则将教学计划输出到用户指定的文件中。计划的表格格式自行设计。 [测试数据] 学期总数:6;学分上限:10;该专业共开设12门课,课程号从C01到C12,学分顺序为2,3,4,3,2,3,4,4,7,5,2,3。课程的先修关系如下表。 课程编号 课程名称 先决条件 C01 程序设计基础 无 C02 离散数学 C1 C03 数据结构 C1,C2 C04 汇编语言 C1 C05 语言的设计和分析 C3,C4 C06 计算机原理 C11 C07 编译原理 C5,C3 C08 操作系统 C3,C6 C09 高等数学 无 C10 线性代数 C9 C11 普通物理 C9 C12 数值分析 C9,C10,C1 实现提示: 可设学期总数不超过12,课程总数不超过100。如果输入的先修课程号不在该专业开设的课程序列中,则作为错误处理。应建立内部课程号与课程号之间的对应关系。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值