返回顶部

A. Copil Copac Draws Trees

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();
}
posted @ 2023-11-02 23:34  bhxyry  阅读(14)  评论(0编辑  收藏  举报