poj 2367 Genealogical tree
来源:互联网 发布:软件购销合同书范本 编辑:程序博客网 时间:2024/06/09 22:03
题目链接:http://poj.org/problem?id=2367View Code
题意:拓扑排序。
分析:题目中说必定存在一种排列满足,所以直接做,最后也不用判断是否全为0。(邻接表存起来,后者入度,然后度为零输出,入队列,队列中利用邻接表遍历。)
代码:
#include<cstdio>#include<cmath>#include<cstring>#include<queue>#include<stack>#include<cstdlib>#include<string>#include<vector>#include<map>#include<string>#include<iostream>#include<algorithm>using namespace std;#define INF 0x3f3f3f3ftypedef long long ll;#define Max(a,b) (a>b)?a:b#define lowbit(x) x&(-x)struct ss{ int a,next;} e[100005];int head[100005],hx;int main(){ hx=0; memset(head,-1,sizeof(head)); int n,du[105]= {0}; scanf("%d",&n);int t; for(int i=1; i<=n; i++) { while(~scanf("%d",&t)&&t) { e[hx].a=t; e[hx].next=head[i]; head[i]=hx++; du[t]++; } } queue<int>q; int flag=1; for(int i=1; i<=n; i++) { if(du[i]==0) { if(flag==1) flag=0; else printf(" "); printf("%d",i); q.push(i); } } while(!q.empty()) { t=q.front();// printf("**%d**",t); q.pop(); for(int i=head[t]; i!=-1; i=e[i].next) { t=e[i].a; du[t]--; if(du[t]==0) { printf(" %d",t); q.push(t); } } } puts("");}
0 0
- poj 2367 Genealogical tree
- poj 2367 Genealogical tree
- poj 2367 Genealogical tree
- POJ 2367 Genealogical tree
- poj 2367 Genealogical tree
- poj 2367 Genealogical tree
- POJ 2367 Genealogical tree
- POJ 2367 Genealogical tree
- POJ--2367--Genealogical tree
- poj-2367 Genealogical tree
- poj 2367 Genealogical tree
- POJ 2367 Genealogical tree
- poj 2367 Genealogical tree
- POJ 2367 Genealogical tree
- 【POJ 2367 Genealogical tree】
- POJ 2367Genealogical tree
- 【POJ】[2367]Genealogical tree
- poj 2367 Genealogical tree
- HDU 1028 Ignatius and the Princess III
- BZOJ 3173: [Tjoi2013]最长上升子序列
- poj 2365 Rope
- Hive分析窗口函数(五) GROUPING SETS,GROUPING__ID,CUBE,ROLLUP
- poj 2366 Sacrament of the sum
- poj 2367 Genealogical tree
- Apple Mach-O Linker (ld) Error Group
- poj 2368 Buttons
- poj 2369 Permutations
- poj 2370 Democracy in danger
- jmeter通过jmeter-plugins、jconsole对系统资源进行监控
- poj 2371 Questions and answers
- poj 2372 D++ Again
- HDU 5857 Median (2016 多校训#10 1001)