AT-AGC004D-Teleporter
Teleporter-AT , Teleporter-LG
思路
一道思维题...
可以推出一号节点的边 一定 指向自己:每个点到 1 的路径只有一条,1 也一样,所以到 1 的距离一定是 1 本身到自己的距离 +n 就不可能等于 k 。
由此一来,只需要每个点到 1 的距离 \(\leq\) 即可。
这个时候就可以贪心了,dfs 的时候从底向上,每 \(k\) 个数就截断,接到 1 节点上去。这个在 dfs 的时候做点处理就好。
$Code$
//1的边一定指向自己
#include
using namespace std;
const int N=2e5+5;
int fa[N];
vector son[N];
int n,k,ans,ma;
int f[N];
int dfs(int x,int dep){
int tmp=dep;
for(int i=0;i>fa[1];
for(int i=2;i<=n;++i) scanf("%d",&fa[i]),son[fa[i]].push_back(i);
ans+=(fa[1]==1?0:1);fa[1]=1;
dfs(1,1);
cout<