2021-2022 ACM-ICPC Brazil Subregional Programming Contest M. Monarchy in Vertigo

原题链接
很容易得出要求的是dfs序,因为我们首先要求的是亲儿子,所有儿子都没了然后才是兄弟,而且不是从祖宗开始找兄弟,而是在上一代人找兄弟。链式前向星在这里用不了,只能用vector,因为链式前向星是后来的先遍历。最后用队列搞一下就行了。

#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <math.h>
#include <map>
#include <set>
#include <queue>

using namespace std;
#define endl '\n'
const int maxn = 1e5 + 5;
const int maxm = maxn << 1;
int head[maxn],nex[maxm],v[maxm],w[maxm],cnt,dis[maxn],vis[maxn],idx = 1,dfn[maxn],t;
vector<int> g[maxn];
void add(int x,int y){
    nex[++cnt] = head[x];
    head[x] = cnt;
    v[cnt] = y;
}
struct node{
    int x,y;
}a[maxn];
void dfs(int node,int fa){
    dfn[++t] = node;
    for (auto k : g[node]) {
        if (k == fa) continue;
        dfs(k,node);
    }
}
signed main(){
    int n;
    cin >> n;
    for (int i = 1; i <= n; ++i) {
        cin >> a[i].x >> a[i].y;
        if (a[i].x == 1){
            g[a[i].y].push_back(++idx);
        }
    }
    dfs(1,0);
    queue<int> q;
    for (int i = 1; i <= t; ++i) {
        q.push(dfn[i]);
    }
    for (int i = 1; i <= n; ++i) {
        if (a[i].x == 2){
            vis[a[i].y] = 1;
            while (vis[q.front()]) q.pop();
            cout << q.front() << endl;
        }
    }
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值