最后三个tle,求求大佬帮忙看看
查看原帖
最后三个tle,求求大佬帮忙看看
291779
A_SOUL_Ava楼主2023/2/20 20:19
#include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
typedef long long ll;
typedef pair<int, int> PII;
const int N = 1e5+10;
int h[4*N],e[2*N],ne[2*N],idx;
bool o[N];
int f[N],n,m,ans;

void add (int a,int b)
{
    ne[idx]=h[a],e[idx]=b,h[a]=idx++;
}

void bfs (int x)
{
  if (h[x]==-1)
  {
    //cout << 1 << endl;
    ans++;
    return;
  }
  for (int i=h[x];i!=-1;i=ne[i])
  {
    //cout << i <<endl;
    bfs (e[i]);
  }
}

void solve()
{
  memset (h,-1,sizeof h);
  cin >> n >> m;
  int x,y;
  while (m--)
  {
    cin >> x >> y;
    add (x,y);
    o[y]=true;
  }
  for (int i=1;i<=n;i++)
    if (h[i]!=-1 && !o[i])
      bfs (i);
  cout << ans <<endl;
}

int main()
{
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  //int T;
  //cin >> T;
  //while (T--)
    solve();
  return 0;
}
2023/2/20 20:19
加载中...