Codeforces 350B Resort(bfs)

感觉是不是很久没做水题了,最近开始各种坑,不论是训练还是CF。。。

加之要去长春赛区,感觉中大出题有点偏向CF风格?所以最近打算把CF上tags 带graphs的都做做吧。

这个题挺水的,直接根据题意,枚举起点然后bfs找最长路就行了。讽刺的是,当时做这场DIV2的时候题都没看懂。。。

#include<algorithm>
#include<iostream>
#include<cstring>
#include<fstream>
#include<sstream>
#include<vector>
#include<string>
#include<cstdio>
#include<bitset>
#include<queue>
#include<stack>
#include<cmath>
#include<map>
#include<set>
#define FF(i, a, b) for(int i=a; i<b; i++)
#define FD(i, a, b) for(int i=a; i>=b; i--)
#define REP(i, n) for(int i=0; i<n; i++)
#define CLR(a, b) memset(a, b, sizeof(a))
#define debug puts("**debug**")
#define LL long long
#define PB push_back
#define MP make_pair
#define eps 1e-8
using namespace std;

const int maxn = 1e5 + 10;
int n, c[maxn], fa[maxn], d[maxn], out[maxn];
vector<int> G[maxn];
int ans, max_len, t, ss, tt;

void bfs(int x)
{
    queue<int> q; q.push(x);
    CLR(d, -1);  d[x] = max_len = 0;
    fa[x] = -1; t = x;
    while(!q.empty())
    {
        int u = q.front(); q.pop();
        REP(i, G[u].size())
        {
            int v = G[u][i];

            //按题意要求走
            if(d[v] != -1 || c[v] == 1) continue;
            if(out[v] != 1) continue;

            //记录
            d[v] = d[u] + 1;
            fa[v] = u;
            q.push(v);
            if(d[v] > max_len) max_len = d[v], t = v;
        }
    }
}

int main()
{
    scanf("%d", &n);
    FF(i, 1, n+1) scanf("%d", &c[i]);
    FF(i, 1, n+1)
    {
        scanf("%d", &t);
        if(t) G[i].PB(t), G[t].PB(i), out[t]++;
    }
    ans = -1;
    FF(i, 1, n+1) if(c[i])
    {
        bfs(i);
        if(d[t] > ans) ans = d[t], ss = i, tt = t;
    }
    bfs(ss);
    printf("%d\n", ans+1);
    while(tt != -1)
    {
        printf("%d ", tt);
        tt = fa[tt];
    }
    return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值