64,tle求调
查看原帖
64,tle求调
97024
李润淇楼主2025/1/18 10:45
#include <iostream>
#include <set>
#include <vector>
using namespace std;
typedef long long ll;
ll n, m;
ll ans = 0;
vector<ll> vis;
vector<vector<int>> adj;
bool find_edge(int u, int v)
{
    for (int i = 0; i < adj[u].size(); ++i)
    {
        if (adj[u][i] == v)
        {
            return true;
        }
    }
    return false;
}
set<ll> viss;
set<ll> temp;
int av[100010] = {0};
bool b[10010] = {0};
void dfs(int u, int begi)
{
    if (viss.count(begi))
        return;
    //   cout<<begi<<" "<<u<<" "<<temp.size()<<endl;
    if (temp.size() == n)
    {
        viss.insert(begi);
        return;
    }
    for (int i = 0; i < adj[u].size(); ++i)
    {
        if (viss.count(begi))
            return;
        if (viss.count(adj[u][i]))
        {
            viss.insert(begi);
            return;
        }
        if (!temp.count(adj[u][i]))
        {
            temp.insert(adj[u][i]);
            dfs(adj[u][i], begi);
        }
    }
}
int main()
{
    cin >> n >> m;
    adj.resize(n + 1);
    for (int i = 1; i <= m; ++i)
    {
        int u, v;
        cin >> u >> v;
        adj[v].push_back(u);
    }
    for (int i = 1; i <= n; i++)
    {
        temp.clear();
        temp.insert(i);
        dfs(i, i);
        temp.erase(i);
    }
    cout << viss.size();
    return 0;
}
2025/1/18 10:45
加载中...