[UVALive 3486] Cells (dfs序)

本文介绍了解决UVALive3486问题的方法,该问题涉及一个大规模的树结构(最多可达20,000,000个节点),并需要回答多个关于节点间祖先关系的查询。通过使用DFS序和记录子树范围的技术,避免了直接递归导致的栈溢出问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

链接

UVALive 3486


题意

给出一个n个节点的树(n <= 20000000),和m个询问,每个询问给出a、b,问a是否是b的祖先。


题解

跑一遍dfs序,记录每个节点子树的域,如果b是a的后代,则st[a] < st[b] && nd[a] >= st[b],注意输入给的有相同的节点,是个坑点,这种情况是No。
另外,直接跑递归是会爆栈的(注意n的规模),应用循环写dfs。


代码
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
#include <stack>
using namespace std;
#define maxn (20000010)
int st[maxn], nd[maxn];
vector<int> son[maxn];
queue<int> que;
void getg()
{
    while(!que.empty()) que.pop();
    int n, tot = 0;
    cin >> n;
    que.push(0);
    son[0].clear();
    for(int i = 0, a, u = 0; i < n; i++)
    {
        scanf("%d", &a);
        u = que.front(); que.pop();

        while(a--)
        {
            son[u].push_back(++tot);
            que.push(tot);
            son[tot].clear();
        }
    }
}
int cnt;
stack<int> sta;
void dfs(int u)
{
    cnt = 0;
    sta.push(u);
    st[u] = 0;
    while(!sta.empty())
    {
        int x = sta.top();
        if(st[x] != 0)
        {
            nd[x] = cnt;
            sta.pop();
            continue;
        }
        st[x] = ++cnt;
        for(int i = 0; i < son[x].size(); i++)
        {
            sta.push(son[x][i]);
            st[son[x][i]] = 0;
        }
    }
    /*
    st[S[top = 1] = u] = 0;
    while(top > 0)
    {
        int x = S[top];
        if(st[x] != 0)
        {
            nd[x] = cnt;
            top--;
            continue;
        }
        st[x] = ++cnt;
        for(int i = 0; i < son[x].size(); i++)
        {
            st[son[x][i]] = 0;
            S[++top] = son[x][i];
        }
    }
    */
}
int main()
{
    int T, kase = 0;
    cin >> T;
    while(T--)
    {
        getg();
        dfs(0);
        int m, a, b;
        cin >> m;
        printf("Case %d:\n", ++kase);
        while(m--)
        {
            scanf("%d%d", &a, &b);
            if(st[b] > st[a] && st[b] <= nd[a])
                printf("Yes\n");
            else
                printf("No\n");
        }
        if(T) printf("\n");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值