ural 1022 Genealogical Tree

题意看哭我。。。。。。英语真的差太多了。。。

第一行不用说了。。。。。。之后每一行后面几个数是第一个数的后代,,,,最后求一条可以的祖宗到末代的顺序、。。。。。。。。。。。

拓扑排序(最开始的只过了样例的WA代码)

#include <iostream>

#include <cstdio>

#include <algorithm>

#include <cstring>

#include <queue>

#include<bits/stdc++.h>

#include <string.h>

using namespace std;

int a[2][101][101]={0};

int ans[101];

int main()

{

int m;

scanf("%d",&m);

for(int i=0;i<m;i++)

{

int q;

scanf("%d",&q);

if(q==0) continue;

int w;

while(scanf("%d",&w)&&w!=0)

{

if(i!=0)

{

for(int e=0;e<100;e++)

{

if(a[1][q][e]==0) {a[1][q][e]=w;break;}

}

for(int e=0;e<100;e++)

{

if(a[0][w][e]==0) {a[0][w][e]=q;break;}

}

}

}

}

int t=0;

int qwe=100;

while(qwe--)

{

int flag=0;

for(int i=1;i<=m;i++)

{

if(a[0][i][0]==0)

{

flag=1;

ans[t++]=i;

a[0][i][0]=1000;

//printf("%d**\n",ans[t-1]);

if(t==m) break;

for(int j=1;j<=m;j++)

{

for(int er=0;a[1][j][er]!=0;er++)

{

if(a[1][j][er]==i)

{

for(int ew=0;a[1][j][er+ew]!=0;ew++)

{

a[1][j][er+ew]=a[1][j][er+ew+1];

}

}

if(a[0][j][er]==i)

{

for(int ew=0;a[0][j][er+ew]!=0;ew++)

{

a[0][j][er+ew]=a[0][j][er+ew+1];

}

}

}

}

}

}

if(flag==0)

{

for(int i=1;i<=m;i++)

{

if(a[0][i][0]<1000)

{

ans[t++]=i;

a[0][i][0]=1000;

//printf("%d**\n",ans[t-1]);

if(t==m-1) break;

for(int j=1;j<=m;j++)

{

for(int er=0;a[1][j][er]!=0;er++)

{

if(a[1][j][er]==i)

{

for(int ew=0;a[1][j][er+ew]!=0;ew++)

{

a[1][j][er+ew]=a[1][j][er+ew+1];

}

}

if(a[0][j][er]==i)

{

for(int ew=0;a[0][j][er+ew]!=0;ew++)

{

a[0][j][er+ew]=a[0][j][er+ew+1];

}

}

}

}

}

if(t==m) break;

}

}

if(t==m) break;

}

for(int i=0;i<m;i++)

{

printf("%d ",ans[i]);

}

return 0;

}

思路很简单,,,,,,分为入度和出度,,,,如果有出度是0的就是符合,,,然后将其他的所有包含他的都去掉,,,,如果没有为0的就将第一个算作符合同样要去掉。。。。。。中间可能哪里有问题吧。。。。。

这就是很粗糙的最原始的想法吧,,,然后好好看了一下拓扑排序。。。。(此题必然无环。。。)

代码不是我的,,,,借鉴的别人的: http://m.blog.csdn.net/blog/WangPegasus/10193767(有冒犯请联系)

原理就是这样的:

看一个点最能走多少次,爷爷的后代肯定比爸爸多,,,,,,把所有代都遍历一遍,,,,,取最深的那个就是最后的答案了。。。。。。。然后再输出出来,,,谁大先输出谁

#include <iostream>

#include <cstdio>

using namespace std;

bool map[110][110], vis[110];

int f[110];

int ftime, n;

void dfs(int x)

{

vis[x] = 1;

for (int v = 1; v <= n; ++v)

if (map[x][v] && !vis[v])

dfs(v);

++ftime;

f[x] =ftime; //记录完成访问的时间

}

int main()

{

scanf("%d", &n);

int t;

for (int i = 1; i <= n; ++i)

while (scanf("%d", &t) && t)

map[i][t] = 1;

for (int i = 1; i <= n; ++i)

if (!vis[i])

dfs(i);

int result[110];

for (int i = 1; i <= n; ++i)   //记录结点编号

result[f[i]] = i;

for (int i = n; i >= 1; --i)      //逆序输出

printf("%d ", result[i]);

return 0;

}



版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-11-06 04:20:52

ural 1022 Genealogical Tree的相关文章

timus 1022 Genealogical Tree(拓扑排序)

Genealogical Tree Time limit: 1.0 secondMemory limit: 64 MB Background 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

poj 2367 Genealogical tree(拓扑)

Genealogical tree Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3685   Accepted: 2453   Special Judge Description The system of Martians' blood relations is confusing enough. Actually, Martians bud when they want and where they want. T

【拓扑排序】Genealogical tree

[POJ2367]Genealogical tree Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5696   Accepted: 3729   Special Judge Description The system of Martians' blood relations is confusing enough. Actually, Martians bud when they want and where the

POJ 2367 Genealogical tree(拓扑排序)

Genealogical tree Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3332   Accepted: 2233   Special Judge Description The system of Martians' blood relations is confusing enough. Actually, Martians bud when they want and where they want. T

POJ 2367 Genealogical tree 拓扑排序入门题

Genealogical tree Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8003   Accepted: 5184   Special Judge Description The system of Martians' blood relations is confusing enough. Actually, Martians bud when they want and where they want. T

Genealogical tree POJ 2367【拓扑排序】

http://poj.org/problem?id=2367 Genealogical tree Special Judge Problem 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 th

poj 2367 Genealogical tree【拓扑排序输出可行解】

Genealogical tree Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3674   Accepted: 2445   Special Judge Description The system of Martians' blood relations is confusing enough. Actually, Martians bud when they want and where they want. T

POJ 2367 Genealogical tree 拓扑题解

一条标准的拓扑题解. 我这里的做法就是: 保存单亲节点作为邻接表的邻接点,这样就很方便可以查找到那些点是没有单亲的节点,那么就可以输出该节点了. 具体实现的方法有很多种的,比如记录每个节点的入度,输出一个节点之后,把这个节点对于其他节点的入度去掉,然后继续查找入度为零的点输出.这个是一般的做法了,效果和我的程序一样的. 有兴趣的也可以参考下我这种做法. #include <stdio.h> #include <string.h> #include <vector> us

POJ 2367:Genealogical tree(拓扑排序)

Genealogical tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2738 Accepted: 1838 Special Judge Description The system of Martians' blood relations is confusing enough. Actually, Martians bud when they want and where they want. They ga