某病毒可以人传人,且传染能力极强,只要与已感染该病毒的人发生接触即刻感染。
现给定一些感染该病毒的人员接触关系,要求你找出其中最早最长的一条传染链。
输入格式:
输入在第一行中给出一个正整数 N(N≤104),即感染病毒的人员总数,从 0 到 N−1 进行编号。
随后N 行按照编号顺序给出人员接触信息,每行按以下格式描述该人员的接触对象:
k 接触人员1 …… 接触人员k
其中 k 是该编号人员接触的其他人总数,后面按照时间先后给出所有接触的人员编号。题目保证传染源头有且仅有一个,且已被感染人员不会与另一个感染人员再接触。
输出格式:
第一行输出从源头开始的最早最长传染链长度。
第二行输出从源头开始的最早最长传染链,编号之间以1个空格分隔,行首尾不得有多余空格。这里的最早最长传染链是指从源头开始的传染链上的人数最多,且被感染的时间最早。
所谓时间最早指的两个长度相等的传染链{a1,a2,…,an}和{b1,b2,…,bn},存在1≤k<n,对于所有的i (1≤i<k)都满足ai=bi,且ak被感染的时间早于bk被感染的时间。
输入样例:
10
0
3 3 4 7
2 1 9
1 6
1 5
0
0
0
2 6 0
1 8
输出样例:
4
2 1 3 6
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
#include<stdio.h>
#include<malloc.h>
struct tree{
int f;
int *bady;
struct tree *badytree[100];
int longs;
};
struct zan{
int data[200];
int top;
};
struct zan find(struct tree *bingtree,struct zan z){
struct zan text,max;
text.top=-1;
max.top = -1;
if(bingtree->longs!=0){
int i;
for(i=0;i<bingtree->longs;i++){
z.top++;
z.data[z.top] = bingtree->bady[i];
text = find(bingtree->badytree[i],z);
z.top--;
if(max.top<text.top){
max = text;
}
}
return max;
}
else{
return z;
}
}
int main(){
int cnt,i,j;
scanf("%d",&cnt);
struct tree bingtree [cnt];
for(i=0;i<cnt;i++){
int num;
scanf("%d",&num);
bingtree[i].bady = (int*)malloc(sizeof(int)*num);
bingtree[i].longs = num;
for(j=0;j<num;j++){
scanf("%d",&bingtree[i].bady[j]);
bingtree[i].badytree[j] = &bingtree[bingtree[i].bady[j]];
}
bingtree[i].badytree[j] = NULL;
}
struct zan text,max,z;
text.top=-1;
max.top = -1;
z.top = -1;
for(i=0;i<cnt;i++){
z.top++;
z.data[z.top] = i;
text = find(&bingtree[i],z);
z.top--;
if(max.top<text.top){
max = text;
}
}printf("%d\n",max.top+1);
for(i=0;i<=max.top;i++){
if(i==0){
printf("%d",max.data[i]);
}
else{
printf(" %d",max.data[i]);
}
}
return 0;
}