Test instructions look at me and cry. The English is really poor.
The first line goes without saying ... After each line, the next few numbers are the descendants of the first number, and, finally, a possible ancestor to the last order 、。。。。。。。。。。。
Topological ordering (the WA code for the first example is just past the sample)
#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;
}
The idea is simple,,,,, divided into degrees and out of degrees,,, if the out-of-the-way is 0 is the same,, and then all the others that contain him are removed,,, if not for 0 will be the first to be counted in accordance with the same to be removed ... Maybe there's something wrong in the middle.
This is a very rough and primitive idea, and then take a good look at the topology sort .... (This problem is bound to have no ring ...) )
Code is not my,,,, reference to someone else's: http://m.blog.csdn.net/blog/WangPegasus/10193767 (please contact for any offense)
The principle is this:
See a point the most can walk how many times, Grandpa's offspring certainly more than the father,,,,,, all generations to traverse over,,,, take the deepest one is the final answer .... And then output it out,,, who first output who
#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; Record the time the visit was completed
}
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)//Record node number
Result[f[i]] = i;
for (int i = n; i >= 1; i.)//Reverse Output
printf ("%d", result[i]);
return 0;
}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Ural 1022 Genealogical Tree