牛客编程巅峰赛S1第7场 - 青铜&白银 牛牛打怪兽 基础dfs

链接:https://ac.nowcoder.com/acm/contest/6630/B
来源:牛客网

题意

身为屯里第一剑士的牛牛来到训练场里闯关,由于过于勤奋,牛牛的宝剑的耐久度降到了 222 ,这意味着牛牛最多只能打倒两只怪兽,否则将会被淘汰。

训练场的地图可以看作一棵以 111 为根节点的树,训练场的终点为这棵树的叶子结点,树上的每个结点最多有一只怪兽,结点与结点间的边上没有怪兽。

每一个有怪兽的结点上牛牛都需要打倒怪兽才算安全,并且牛牛一旦选定好打怪路线之后便不能走回头路。

请问牛牛有多少种到达终点且不被淘汰的路径。
输入

第一个参数为 nnn ,(1≤n≤100,000)(1\leq n\leq 100,000)(1≤n≤100,000)

第二个参数为大小为 n−1n-1n−1 的点对 (ui,vi)(u_i, v_i)(ui​,vi​) 的集合,其中 (ui,vi)(u_i, v_i)(ui​,vi​) 表示结点 uiu_iui​ 与结点 viv_ivi​ 之间有一条边,1≤ui,vi≤n1\leq u_i, v_i \leq n1≤ui​,vi​≤n

第三个参数为大小为 nnn 的 0/10/10/1 序列 fff ,若 fif_ifi​ 为 000 表示i-1结点没有怪兽,否则表示 i-1 结点有怪兽。
返回

一个整数,表示牛牛能到达终点且不被淘汰的路径数。
示例1
输入
复制

7,[(7,2),(6,1),(5,2),(1,2),(4,6),(6,3)],[0,0,1,0,1,0,0]

输出
复制

4

说明

样例中的四条路径分别为: (1 - 2 - 7), (1 - 2 - 5) , (1 - 6 - 3), (1 - 6 - 4)


这是一道基础的dfs题目,两个星期不碰题目,居然调了很久…
一日不书百日荒, 以后再忙也要每日ac才行,

#define debug
#ifdef debug
#include <time.h>
#endif

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

#define ll long long int
#define INF (0x7f7f7f7f)
#define fori(lef, rig) for(int i=lef; i<=rig; i++)
#define forj(lef, rig) for(int j=lef; j<=rig; j++)
#define fork(lef, rig) for(int k=lef; k<=rig; k++)
#define QAQ (0)

using namespace std;

#define show(x...) \
    do { \
       cout << "   [ " << #x << " -> "; \
       err(x); \
    } while (0)

void err() { cout << " ]" << endl; }
template<typename T, typename... A>
void err(T a, A... x) { cout << a << ' '; err(x...); }

namespace FastIO{

    char print_f[105];
    void read() {}
    void print() { putchar('\n'); }

    template <typename T, typename... T2>
       inline void read(T &x, T2 &... oth) {
           x = 0;
           char ch = getchar();
           ll f = 1;
           while (!isdigit(ch)) {
               if (ch == '-') f *= -1; 
               ch = getchar();
           }
           while (isdigit(ch)) {
               x = x * 10 + ch - 48;
               ch = getchar();
           }
           x *= f;
           read(oth...);
       }
    template <typename T, typename... T2>
       inline void print(T x, T2... oth) {
           ll p3=-1;
           if(x<0) putchar('-'), x=-x;
           do{
                print_f[++p3] = x%10 + 48;
           } while(x/=10);
           while(p3>=0) putchar(print_f[p3--]);
           putchar(' ');
           print(oth...);
       }
} // namespace FastIO
using FastIO::print;
using FastIO::read;

int n, m, Q, K;

typedef unsigned long long ull;

#define MAXN (int(1e5+7))
#ifdef debug
struct Point {
    int x;
    int y;
};
#endif

int ans;
int w[MAXN];
vector<int> G[MAXN];

inline void init() {
    for(int i=1; i<MAXN; i++) G[i].clear(), w[i] = 0;
    ans = 0;
}

class Solution {
public:
    void dfs(int u, int fa, int cnt) {
        if(w[u]) cnt --;
        if(cnt < 0) return ;
        int son = 0;
        for(auto v : G[u]) {
            if(v != fa) dfs(v, u, (cnt)), son ++;
        }
        if(!son) ans ++;
    } 

    int solve(int n, vector<Point>& Edge, vector<int>& f) {
        // write code here
        init();
        for(auto ed : Edge) {
            G[ed.x].push_back(ed.y);
            G[ed.y].push_back(ed.x);
        }

        for(int i=1; i<=n; i++)
            w[i] = f[i-1];
        dfs(1, -1, 2);
        return ans;
    }
};

#ifdef debug
signed main() {
    clock_t stime = clock();
    Solution s;
    vector<Point> vec = {
        { 7, 2 }, { 6, 1 }, { 5, 2 }, { 1, 2 }, { 4, 6 }, { 6, 3 }
    };
    vector<int> tmp = { 0, 0, 1, 0, 1, 0, 0 };
    int ans = s.solve(7, vec, tmp);
    printf("%d\n", ans);

    return 0;
}


#endif

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值