Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //#include "stdafx.h"
- #include <algorithm>
- #include <iostream>
- #include <string>
- #include <cstring>
- #include <fstream>
- #include <cmath>
- #include <vector>
- #include <utility>
- #include <cstdlib>
- #include <deque>
- #include <queue>
- #include <iomanip>
- #include <numeric>
- #include <stack>
- #include <map>
- #include <set>
- #include <cmath>
- #include <list>
- #define mp make_pair
- #define pb push_back
- #define xx first
- #define yy second
- #define preturn(a) return cout << a << endl, 0
- using namespace std;
- typedef long long ll;
- typedef unsigned long long ull;
- typedef long double ld;
- typedef unsigned int uint;
- const int N = (int)2500000, md = 1e7;
- const ld eps = 1e-16, eps1 = 1e-10, OOO = 1e200, PI = 3.14159265358979323846;
- const ull OOU = 1e19 * 1.7;
- const ll OO = 1e18 * 8, mod = 1e9 + 7;
- 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()
- {
- //ios_base::sync_with_stdio(0);
- //freopen("easy1.txt", "r", stdin);
- //freopen("output.txt", "w", stdout);
- 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]++;
- //if (g[b].find(a) != g[b].end()) d[a] = d[b] = pr[a] = pr[b] = -1;
- }
- //cout << clock() << endl;
- 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;
- //cout << clock() << endl;
- //fill(used.begin(), used.end(), 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;
- }
- //cout << clock() << endl;
- for (int i = 1; i < n; i++) printf("%d\n", d[i]);
- //cout << endl << clock();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement