P4645 [COCI2006-2007#3] BICIKLI
P4645 [COCI2006-2007#3] BICIKLI
题意:求一张
首先考虑不在
然后考虑什么时候会有无数条满足条件的路径。发现当且仅当路径上有环时,可以经过任意次环以产生无数条路径。
剩下的情况就是
复杂度
点击查看代码
#include <bits/stdc++.h> using namespace std; #define ll long long const ll SIZE = 200005; const ll mod = 1e9; ll n, m; ll head[SIZE], ver[SIZE*2], nxt[SIZE*2], tot; vector<ll> e[SIZE]; bool c1[SIZE], c[SIZE]; ll d[SIZE]; ll ans[SIZE]; inline ll rd(){ ll f = 1, x = 0; char ch = getchar(); while(ch < '0' || ch > '9'){ if(ch == '-') f = -1; ch = getchar(); } while(ch >= '0' && ch <= '9'){ x = (x << 1) + (x << 3) + (ch ^ 48); ch = getchar(); } return f*x; } void add(ll x, ll y){ ver[++tot] = y, nxt[tot] = head[x]; head[x] = tot; } void dfs1(ll x){ c1[x] = 1; for(int i = head[x]; i; i = nxt[i]){ ll y = ver[i]; if(c1[y]) continue; dfs1(y); } } void dfs2(ll x){ c[x] = 1; for(int i = 0; i < e[x].size(); i++){ ll y = e[x][i]; if(c[y]) continue; dfs2(y); } } struct E{ ll x, y; }ee[SIZE]; int main(){ n = rd(), m = rd(); assert(n <= 100000 && m <= 100000); for(int i = 1; i <= m; i++){ ll x = rd(), y = rd(); ee[i].x = x, ee[i].y = y; add(x, y); e[y].push_back(x); } dfs1(1); dfs2(2); ll cnt = 0; for(int i = 1; i <= n; i++) c[i] &= c1[i], cnt += c[i]; for(int i = 1; i <= m; i++){ if(!c[ee[i].x] || !c[ee[i].y]) continue; d[ee[i].y]++; } if(d[1] != 0){ printf("inf"); return 0; } queue<int> q; q.push(1); ll z = 0; ans[1] = 1; while(q.size()){ ll x = q.front(); q.pop(); z++; for(int i = head[x]; i; i = nxt[i]){ ll y = ver[i]; d[y]--; ans[y] = (ans[y] + ans[x]) % mod; if(d[y] == 0){ q.push(y); } } } if(z < cnt) printf("inf"); else printf("%lld", ans[2]); return 0; }
本文作者:Semorius
本文链接:https://www.cnblogs.com/Semorius/p/17538325.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步