1. Handle the Unicom block beginning with $s$
2. Handle the Unicom block at the beginning of the zero entry point
3, processing ring of the Unicom block, at this time to note that there may be ring string, so to allow label coverage, otherwise it will be remembered, because it is the cause of the ring, can not be treated like a 2 to do from scratch. This is the case (2,3,4 is the label of the ring)
However, 1 of the ring can only be traversed from 1, so 1 of the ring can not be overwritten, all points to 1 of the edge simply do not build.
CODE:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <set >using namespace Std;const int maxn=5005;struct point{int to;int NXT;} Edge[maxn];int n,tot,m,s,cnt;int head[maxn];int in[maxn],vis[maxn];set<int> S;inline void Add (int u,int v) {tot++ ; Edge[tot].nxt=head[u];edge[tot].to=v;head[u]=tot;} inline void dfs (int x) {vis[x]=cnt;for (int i=head[x];i;i=edge[i].nxt) {int v=edge[i].to;if (vis[v]==cnt | | v==s) continue ;d FS (v);}} int main () {scanf ("%d%d%d", &n,&m,&s), for (int i=1;i<=m;i++) {int u,v;scanf ("%d%d", &u,&v); Add ( U,V); in[v]++;} Cnt=1;dfs (s); for (int i=1;i<=n;i++) if (!vis[i] && in[i]==0) {cnt++;d fs (i);} for (int i=1;i<=n;i++) if (!vis[i]) {cnt++;d fs (i);} for (int i=1;i<=n;i++) S.insert (vis[i]);p rintf ("%d", S.size ()-1); return 0;}
cf999e reachability from the capital