Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- #include <iomanip>
- #include <cmath>
- #include <set>
- #include <vector>
- #include <fstream>
- #define pb push_back
- using namespace std;
- typedef long long ll;
- typedef long double ld;
- const int mod = 1e9 + 7;
- int pr[300000], d[300000], h, t, q[1000000];
- bool used[300000];
- vector<set<int> > g(200001);
- int main()
- {
- //freopen("input.txt", "r", stdin);
- //freopen("output.txt", "w", stdout);
- int n, m, a, b, v;
- cin >> n>> m;
- for(int i = 0; i < m; i++)
- {
- cin >> a >> b;
- a--;
- b--;
- g[a].insert(b);
- if(g[b].find(a) != g[b].end()) d[a] = d[b] = -1;
- }
- d[0] = 1;
- q[h++] = 0;
- for(;h != t;)
- {
- v = q[t];
- t = (t + 1) % 1000000;
- for(set<int>::iterator i = g[v].begin(); i != g[v].end(); i++)
- {
- if(d[*i] != -1 && (!pr[*i] || pr[*i] == v))
- {
- d[*i] = (d[*i] + d[v]) % mod;
- q[h] = *i;
- h = (h + 1) % 1000000;
- pr[*i] = v;
- }
- else d[*i] = -1;
- }
- }
- for(int i = 0; i < n; i++)
- if(i) cout << d[i] << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement