比赛 |
20100419 |
评测结果 |
C |
题目名称 |
烟花的寿命 |
最终得分 |
0 |
用户昵称 |
lc |
运行时间 |
0.000 s |
代码语言 |
Pascal |
内存使用 |
0.00 MiB |
提交时间 |
2010-04-19 09:24:50 |
显示代码纯文本
#include<iostream>
#include<cstring>
using namespace std;
const int maxn = 1003;
int Test,N,Maxd,V;
int Lis[maxn][maxn];
int Stack[maxn],Path[maxn];
bool vis[maxn];
void Addedge(int x,int y)
{
Lis[x][++Lis[x][0]] = y;
}
void prep()
{
scanf("%d",&N);
for (int i=1; i<=N; i++) Lis[i][0] = 0;
for (int i=1; i< N; i++)
{
int x,y;
scanf("%d%d",&x,&y);
Addedge(x,y); Addedge(y,x);
}
}
void DFs(int u,int d)
{
if (vis[u]) return;
vis[u] = true; Stack[d] = u;
if (d >Maxd)
{
Maxd = d; V = u;
memcpy(Path,Stack,sizeof(int)*(d+1));
}
for (int i=1; i<=Lis[u][0]; i++)
{
DFs(Lis[u][i],d+1);
}
}
void work()
{
memset(vis,0,sizeof(bool)*(N+1));
Maxd = -1; DFs(1,0);
memset(vis,0,sizeof(bool)*(N+1));
Maxd = -1; DFs(V,0);
printf("%d\n",Maxd);
for (int i=0; i<=Maxd; i++)
printf("%d\n",Path[i]);
}
int main()
{
freopen("firework.in","r",stdin);
freopen("firework.out","w",stdout);
scanf("%d",&Test);
while (Test--)
{
prep();
work();
}
return 0;
}