Description

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

直接套模板

 #include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int maxn = 1e5 + ; int n, du[maxn], head[maxn], tot;
struct node {
int v, next;
} edge[maxn];
queue<int>q;
void add(int u, int v) {
edge[tot].v = v;
edge[tot].next = head[u];
head[u] = tot++;
}
void init() {
tot = ;
memset(du, , sizeof(du));
memset(head, -, sizeof(head));
}
void solve() {
while(!q.empty()) {
int u = q.front();
q.pop();
printf("%d ", u);
for (int i = head[u] ; i != - ; i = edge[i].next) {
du[edge[i].v]--;
if (!du[edge[i].v]) q.push(edge[i].v);
}
}
}
int main() {
scanf("%d", &n);
init();
for (int i = ; i <= n ; i++) {
int x;
while(scanf("%d", &x), x != ) {
add(i, x);
du[x]++;
}
}
for (int i = ; i <= n ; i++)
if (!du[i]) q.push(i);
solve();
return ;
}

最新文章

  1. SVN如何查看修改的文件记录] 来源:Linux社区 作者:frogoscar
  2. Git-图文教程
  3. 在Android工程中运行main函数
  4. C#后台如何获取客户端访问系统型号
  5. C++11 并发指南五(std::condition_variable 详解)
  6. Android 手机配office365邮箱
  7. 微信分享朋友圈监听(PHP)
  8. 封装鼠标滚轮事件_mousewheel
  9. Linux下卸载和安装MySQL[rpm包]
  10. Android源代码编译——下载
  11. c#代码规范和质量检查工具这点事
  12. thymeleaf模板引擎入门
  13. 第9章 设备授权端点(Device Authorization Endpoint) - IdentityModel 中文文档(v1.0.0)
  14. Linux CentOS设置定时重启:crontab
  15. graph easy绘制ascii简易流程图
  16. Confluence 6 附件存储文件系统的分级
  17. Update openssh7.9 on centos6
  18. Qt__主窗口、菜单和工具条(QMainWindow,QMenu,QToolBar)
  19. springmvc配置文件&lt;context:component-scan&gt;
  20. HTTP基本原理(转)

热门文章

  1. cordforce 495 补题 &lt;未完&gt;
  2. Laravel系列 Web
  3. java多线程批量读取文件( 八)--读写分离
  4. Mysql 索引 简介
  5. startActivityForResult 请求码不正确
  6. Java重写与重载
  7. shuffle的过程分析
  8. 如何将自己写的代码上传到github上
  9. Ubuntu系列问题
  10. Day1 Toast/Menu/Intent传递数据