Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int d[300000], q[(int)(1e7 * 1.1)], h, t, pr[300000];
- vector<vector<pair<int, pair<bool, bool> > > > g(200001);// , g1(200001);
- vector<bool> debt(N), used(N);
- set<pair<int, int> > us;
- int add(int a, int b)
- {
- if (a == -1 || b == -1) return -1;
- else return ((a + b) % mod);
- }
- int main()
- {
- d[0] = 1;
- int n, m, a, b, v;
- cin >> n >> m;
- for (int i = 0; i < m; i++)
- {
- scanf("%d%d", &a, &b);
- a--;
- b--;
- g[a].pb(mp(b, mp(0, 0)));
- pr[b]++;
- }
- q[h++] = 0;
- used[0] = 1;
- for (; h != t;)
- {
- v = q[t++];
- t %= md;
- for (vector<pair<int, pair<bool, bool> > >::iterator i = g[v].begin(); i != g[v].end(); i++)
- {
- if (i->yy.xx)
- {
- d[i->xx] = d[v] = pr[i->xx] = pr[v] = -1;
- debt[v] = 1;
- continue;
- }
- else i->yy.xx = 1;
- if (pr[v] <= 0)
- {
- d[i->xx] = add(d[i->xx], d[v]);
- pr[i->xx]--;
- //debt[v] = 1;
- if (!used[i->xx])
- {
- q[h++] = i->xx;
- h %= md;
- used[i->xx] = 1;
- debt[v] = 0;
- }
- }
- else
- {
- debt[v] = 1;
- q[h++] = i->xx;
- h %= md;
- }
- }
- }
- h = t = 0;
- q[h++] = 0;
- used[0] = 1;
- for (; h != t;)
- {
- v = q[t++];
- t %= md;
- if (pr[v] > 0)
- {
- q[h++] = v;
- h %= md;
- continue;
- }
- for (vector<pair<int, pair<bool, bool> > >::iterator i = g[v].begin(); i != g[v].end(); i++)
- {
- if (debt[v]) d[i->xx] = add(d[i->xx], d[v]), pr[i -> xx]--;
- if (!(i->yy.yy))
- {
- q[h++] = i->xx;
- h %= md;
- i->yy.yy = 1;
- pr[i->xx]--;
- }
- }
- debt[v] = 0;
- }
- for (int i = 1; i < n; i++) printf("%d\n", d[i]);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement