A. Copil Copac Draws Trees
题目大意:
给出一个树边序列,要求你从1号节点建树,对于每条边只有两个端点中有一个绘制了才可以绘制此边
思路:
这题思路不难,但以前写图太少,遍历被卡,给每个边按序列编号,dfs如果该边的编号大于上条边\(ans++\)
code:
int n;
vector<pii> a[N];
int ans[N] = {0};
void dfs(int res, int fa)
{
for (auto [x, id] : a[fa])
{
if (id == res)
continue;
ans[x] = ans[fa] + (id < res);
dfs(id, x);
}
}
void solved()
{
cin >> n;
for (int i = 1; i < n; ++i)
{
ans[i] = 0;
int tot, op;
cin >> tot >> op;
a[tot].push_back(make_pair(op, i));
a[op].push_back(make_pair(tot, i));
}
ans[1] = 1;
dfs(0, 1);
cout << *max_element(ans + 1, ans + n + 1) << endl;
for (int i = 1; i <= n; ++i)
a[i].clear();
}
标签:Copil,int,tot,Copac,dfs,ans,Draws,id,op
From: https://www.cnblogs.com/bhxyry/p/17806693.html