poj 2367 Genealogical tree【拓扑排序输出可行解】
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 3674 | Accepted: 2445 | Special Judge |
Description
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
Output
Sample Input
- 5
- 0
- 4 5 1 0
- 1 0
- 5 3 0
- 3 0
Sample Output
- 2 4 5 3 1
- 题意:给一个数字n接下来有n行数据,每行数据以输入0为结束标志,第i行表示第i行的所有点连在点i上(如第二行的4 5 1表示2->4 2->5 2->1)让你任意输出一组排好序之后的序列
题解:反向拓扑输出可行解
- #include<stdio.h>
- #include<string.h>
- #include<queue>
- #include<vector>
- using namespace std;
- int n;
- int vis[110];
- vector<int>map[110];
- int b[110];
- void getmap()
- {
- int i,j;
- memset(vis,0,sizeof(vis));
- for(i=1;i<=n;i++)
- map[i].clear();
- for(i=1;i<=n;i++)
- {
- int a;
- while(scanf("%d",&a),a)
- {
- map[a].push_back(i);
- vis[i]++;
- }
- }
- }
- void tuopu()
- {
- int i,j;
- queue<int>q;
- memset(b,0,sizeof(b));
- while(!q.empty())
- q.pop();
- for(i=1;i<=n;i++)
- if(vis[i]==0)
- q.push(i);
- int u,v;
- int ans=0;
- while(!q.empty())
- {
- u=q.front();
- q.pop();
- b[ans++]=u;
- for(i=0;i<map[u].size();i++)
- {
- v=map[u][i];
- vis[v]--;
- if(vis[v]==0)
- q.push(v);
- }
- }
- for(i=ans-1;i>=1;i--)
- printf("%d ",b[i]);
- printf("%d\n",b[0]);
- }
- int main()
- {
- while(scanf("%d",&n)!=EOF)
- {
- getmap();
- tuopu();
- }
- return 0;
- }
poj 2367 Genealogical tree【拓扑排序输出可行解】的更多相关文章
- POJ 2367 Genealogical tree 拓扑排序入门题
Genealogical tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8003 Accepted: 5184 ...
- Poj 2367 Genealogical tree(拓扑排序)
题目:火星人的血缘关系,简单拓扑排序.很久没用邻接表了,这里复习一下. import java.util.Scanner; class edge { int val; edge next; } pub ...
- POJ 2367 Genealogical tree 拓扑题解
一条标准的拓扑题解. 我这里的做法就是: 保存单亲节点作为邻接表的邻接点,这样就非常方便能够查找到那些点是没有单亲的节点,那么就能够输出该节点了. 详细实现的方法有非常多种的,比方记录每一个节点的入度 ...
- poj 2367 Genealogical tree
题目连接 http://poj.org/problem?id=2367 Genealogical tree Description The system of Martians' blood rela ...
- 图论之拓扑排序 poj 2367 Genealogical tree
题目链接 http://poj.org/problem?id=2367 题意就是给定一系列关系,按这些关系拓扑排序. #include<cstdio> #include<cstrin ...
- poj 2367 Genealogical tree (拓扑排序)
火星人的血缘关系很奇怪,一个人可以有很多父亲,当然一个人也可以有很多孩子.有些时候分不清辈分会产生一些尴尬.所以写个程序来让n个人排序,长辈排在晚辈前面. 输入:N 代表n个人 1~n 接下来n行 第 ...
- POJ 2367 Genealogical tree【拓扑排序/记录路径】
Genealogical tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7101 Accepted: 4585 Spe ...
- POJ 2367 Genealogical tree【拓扑排序】
题意:大概意思是--有一个家族聚集在一起,现在由家族里面的人讲话,辈分高的人先讲话.现在给出n,然后再给出n行数 第i行输入的数表示的意思是第i行的子孙是哪些数,然后这些数排在i的后面. 比如样例 5 ...
- POJ 2367 (裸拓扑排序)
http://poj.org/problem?id=2367 题意:给你n个数,从第一个数到第n个数,每一行的数字代表排在这个行数的后面的数字,直到0. 这是一个特别裸的拓扑排序的一个题目,拓扑排序我 ...
随机推荐
- 使用methodSignatureForSelector与forwardInvocation实现消息转发 (转)
转自:http://blog.sina.com.cn/s/blog_8c87ba3b0102v006.html 在给程序添加消息转发功能以前,必须覆盖两个方法,即methodSignatureForS ...
- #define和#undefine的用法
#undef将保持已定义状态且在 作用域内,直到程序结束或者使用#undef 指令取消定义. 预处理器 在此程序中,我们将取消在先前程序中对预处理器的定义. 1 2 3 4 5 6 7 8 9 10 ...
- URL 路由访问报错
错误: 错误分析: 控制器的文件名命名有问题(index.php) 在TP中控制器命名规范(IndexController.class.php) 相信许多PHP开发者在使用ThinkPHP ...
- 及其简易的js 倒计时插件
网上虽然有很多漂亮的且很实用的倒计时插件,但是,对于需要自己定制的倒计时来讲确实一个不小的障碍.最近我们的英语在线教育产品,在线考试模块需要用到一个计时器,所以顺势开发了一个自己的及时器. http: ...
- Python 关于正负无穷float(‘inf’)的一些用法
Python中可以用如下方式表示正负无穷: float("inf"), float("-inf") 利用 inf 做简单加.乘算术运算仍会得到 inf > ...
- codevs 1066 引水入城
传送门 题目描述 Description 在一个遥远的国度,一侧是风景秀美的湖泊,另一侧则是漫无边际的沙漠.该国的行政 区划十分特殊,刚好构成一个N行M列的矩形,如上图所示,其中每个格子都代表一座城 ...
- SpringSecurity数据库中存储用户、角色、资源
这几天项目中用到了SpringSecurity做登陆安全.所以在这写一下也许可以帮助一下其他人,自己也熟悉一下 SpringSecurity配置文件如下: <beans:beans xmlns= ...
- C#反射(一) 【转】
在还不太熟悉反射的昨天,以为反射很神秘,在网上到处找答案.今天找了段代码敲了一下,茅塞顿开!其实反射也就那么简单的一回事! 反射是一种机制,通过这种机制我们可以知道一个未知类型的类型信息.比如, ...
- net.sf.json在处理json对象转换为普通java实体对象时的问题和解决方案
我使用的net.sf.json是json-lib-2.4-jdk15.jar,把json对象转换为普通java实体对象时候有个问题,josn对象转换为java对象之后,json串里面的那几个小数点的值 ...
- Redis监控
首先判断客户端和服务器连接是否正常 # 客户端和服务器连接正常,返回PONG redis> PING PONG # 客户端和服务器连接不正常(网络不正常或服务器未能正常运行),返回连接异常 re ...