2020-PTA总决赛-L3-1 那就别担心了(记忆化搜索)

思路:

读懂题到28分花了十分钟左右,做的时候就感觉可能要超时,因为结点稍微有点多
但是还是继续硬着头皮写下去了,果不其然,最后一个测试点超时,那么就要开dp数组了
题目大意就是找到A到B有几条路径,且是否无论走哪条路都能通向B

28分Code:

#include<bits/stdc++.h>
using namespace std;
vector<int> graph[510];
int cnt = 0; //路径数
bool flag = true; //默认逻辑自洽
void dfs(int be, int end){
    if(be == end){
        cnt++;
        return;
    }else if(graph[be].size() == 0){
        flag = false;
        return;
    }

    for(int i = 0; i < graph[be].size(); i++){
        dfs(graph[be][i], end);
    }
}

int main(){
    int N, M;
    cin >> N >> M;
    while(M--){
        int s1, s2;
        cin >> s1 >> s2;
        graph[s1].push_back(s2);
    }
    int x, y;
    cin >> x >> y;
    dfs(x, y);
    cout << cnt << ' ';
    if(flag) cout << "Yes";
    else cout << "No";
    return 0;
}

改天再改bug

posted @ 2025-03-08 18:39  Yuhhhhh  阅读(21)  评论(0)    收藏  举报