Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- #define int long long
- #define dd second
- #define mp make_pair
- #define pb push_back
- #define ff first
- #define dd second
- #define pp pair<int,int>
- #define maxx 200005
- using namespace std;
- int n;
- int mod = 1e9+7;
- int ans = 1;
- vector<int> graph[maxx];
- vector<int> graph2[maxx];
- vector<int> kol;
- int ozn[maxx];
- int bylem[maxx];
- void dfs(int i)
- {
- bylem[i] = 1;
- for(auto &x:graph[i])
- {
- if(bylem[x] == 1)
- {
- ozn[i] = 1;
- }
- if(bylem[x] == 0)
- {
- dfs(x);
- }
- if(ozn[x]) ozn[i] = 1;
- }
- bylem[i] = 2;
- kol.pb(i);
- }
- void dfs3(int i)
- {
- bylem[i] = 1;
- for(auto &x:graph2[i])
- {
- if(bylem[x] == 0)
- {
- dfs3(x);
- }
- }
- kol.pb(i);
- }
- int dfs2(int i)
- {
- bylem[i] = 1;
- int res = 1;
- for(auto &x:graph2[i])
- {
- if(bylem[x] == 0) res += dfs2(x);
- }
- return res;
- }
- int dfs4(int i)
- {
- //cout << i << " " << graph2[i].size() << endl;
- int res = 1;
- bylem[i] = 1;
- for(int x : graph2[i])
- {
- if(bylem[x] == 0 and ozn[x] == 0) res += dfs4(x);
- }
- return res;
- }
- main()
- {
- ios_base::sync_with_stdio(false);
- cin >> n;
- for(int i = 0; i < maxx; ++i) bylem[i] = ozn[i] =0;
- for(int i = 0; i < n; ++i)
- {
- int a,b; cin >> a >> b;
- if(a != b)graph[a].pb(b); if(a != b)graph2[b].pb(a);
- }
- for(int i = 1; i <=2*n; ++i)
- {
- if(bylem[i] == 0)
- {
- dfs(i);
- }
- }
- reverse(kol.begin(), kol.end());
- for(int i = 0; i < maxx; ++i) bylem[i] = 0;
- for(int i:kol)
- {
- if(bylem[i] == 0)
- {
- int wyn = dfs2(i);
- // cout << i << " " << wyn << endl;
- if(wyn > 1) {ans *= 2; ans %= mod;}
- }
- }
- for(int i = 0; i < maxx; ++i) bylem[i] = 0;
- kol.resize(0);
- for(int i = 1; i <=2*n; ++i)
- {
- if(bylem[i] == 0)
- {
- dfs3(i);
- }
- }
- reverse(kol.begin(), kol.end());
- for(int i = 0; i < maxx; ++i) bylem[i] = 0;
- for(int i : kol)
- {
- if(bylem[i] == 0 and ozn[i] == 0)
- {
- int temp = dfs4(i);
- //cout << i << " " << temp << " 2 " << endl;
- if(temp > 1) {ans *= temp; ans %= mod;}
- }
- }
- cout << ans << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement