标签:
3 3
1 2
2 3
1 3
2
#include<iostream> #include<cstdio> #include<string> #include<cstring> #include<algorithm> #include<vector> using namespace std; const int maxn = 100005,mod = 11129; vector<int> g[maxn]; int n,m,f[maxn],topo[maxn],cnt,ans; bool vis[maxn],ind[maxn]; void input(){ scanf("%d%d",&n,&m); int a,b; for(int i = 1;i <= m;i++){ scanf("%d%d",&a,&b); g[a].push_back(b); ind[b] = true; } } void topo_dfs(int x){ vis[x] = true; for(int i = 0;i < g[x].size();i++){ if(!vis[g[x][i]]) topo_dfs(g[x][i]); } topo[cnt--] = x; } void topo_sort(){ cnt = n; for(int i = 1;i <= n;i++){ if(!vis[i]) topo_dfs(i); } } void dfs(int x){ if(!g[x].size()){ f[x] = 1; return; } for(int i = 0;i < g[x].size();i++){ if(!f[g[x][i]]) dfs(g[x][i]); f[x] = (f[x] + f[g[x][i]]) % mod; } } void dp(){ for(int i = 1;i <= n;i++){ if(!f[topo[i]]) dfs(topo[i]); if(!ind[topo[i]]) ans = (ans + f[topo[i]]) % mod; } cout<<ans; } int main(){ input(); topo_sort(); dp(); return 0; }
标签:
原文地址:http://www.cnblogs.com/hyfer/p/5754644.html