AcWing 3465. 病毒溯源--y总原版代码
原题链接
中等
作者:
小染
,
2021-05-05 14:21:31
,
所有人可见
,
阅读 1908
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 10010, M = 10010;
int n;
int h[N], e[M], ne[M], idx;
int son[N];
bool st[N];
void add(int a, int b) // 添加一条边a->b
{
e[idx] = b, ne[idx] = h[a], h[a] = idx ++ ;
}
int dfs(int u)
{
int res = 0;
son[u] = -1;
for (int i = h[u]; ~i; i = ne[i])
{
int j = e[i];
int d = dfs(j);
if (res < d) res = d, son[u] = j;
else if (res == d) son[u] = min(son[u], j);
}
return res + 1;
}
int main()
{
memset(h, -1, sizeof h);
scanf("%d", &n);
for (int i = 0; i < n; i ++ )
{
int cnt;
scanf("%d", &cnt);
while (cnt -- )
{
int x;
scanf("%d", &x);
add(i, x);
st[x] = true;
}
}
int root = 0;
while (st[root]) root ++ ;
printf("%d\n", dfs(root));
printf("%d", root);
while (son[root] != -1)
{
root = son[root];
printf(" %d", root);
}
return 0;
}
欸,不对莫名其妙又a了
嗯,没问题的
数据应该是加强了,这个会tle了
for(int i=h[u];~i;i=ne[i])中的~i是什么意思?
取反
意思就是 i!=-1,因为 -1 取反后是 0 跳出循环