为什么第一个点WA了求助大佬!谢谢大佬!
查看原帖
为什么第一个点WA了求助大佬!谢谢大佬!
853953
ljx_gkx楼主2023/4/1 15:06
#include<iostream>
#include<cstring>
#include<algorithm>

using namespace std;

const int N = 5e3 + 10, M = 5e5 + 10;
int h[N], ne[M], e[M], idx;
int n, m, res;
bool father[N];
bool son[N];
int target;
void add (int a, int b)
{
    e[idx] = b, ne[idx] = h[a], h[a] = idx ++;
}

void dfs(int u)
{
    for (int i=h[u]; ~i; i = ne[i])
    {
        int v = e[i];
        dfs(v);
    }
    if (target == u)
        res ++;
    return ; 
}

int main()
{
    cin >> n >> m;
    memset(h, -1, sizeof(h));
    while (m --)
    {
        int a, b;
        cin >> a >> b;
        add (b, a);
        father[a] = true;
        son[b] = true;  //表示它有儿子!
    }

    int root = 1;
    while (father[root]) root ++;
    target=1;
    while (son[target]) target ++;
    dfs (root);
    cout << res << endl;
    return 0;
}
2023/4/1 15:06
加载中...