Advertisement
Guest User

Untitled

a guest
Sep 16th, 2017
323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.41 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #define int long long
  3. #define dd second
  4. #define mp make_pair
  5. #define pb push_back
  6. #define ff first
  7. #define dd second
  8. #define pp pair<int,int>
  9. #define maxx 200005
  10. using namespace std;
  11. int n;
  12. int mod = 1e9+7;
  13. int ans = 1;
  14. vector<int> graph[maxx];
  15. vector<int> graph2[maxx];
  16. vector<int> kol;
  17. int ozn[maxx];
  18. int bylem[maxx];
  19.  
  20. void dfs(int i)
  21. {
  22.     bylem[i] = 1;
  23.     for(auto &x:graph[i])
  24.     {
  25.         if(bylem[x] == 1)
  26.         {
  27.             ozn[i] = 1;
  28.         }
  29.         if(bylem[x] == 0)
  30.         {
  31.             dfs(x);
  32.         }
  33.         if(ozn[x]) ozn[i] = 1;
  34.     }
  35.     bylem[i] = 2;
  36.     kol.pb(i);
  37. }
  38.  
  39. void dfs3(int i)
  40. {
  41.     bylem[i] = 1;
  42.     for(auto &x:graph2[i])
  43.     {
  44.         if(bylem[x] == 0)
  45.         {
  46.             dfs3(x);
  47.         }
  48.     }
  49.     kol.pb(i);
  50. }
  51.  
  52. int dfs2(int i)
  53. {
  54.     bylem[i] = 1;
  55.     int res = 1;
  56.     for(auto &x:graph2[i])
  57.     {
  58.         if(bylem[x] == 0) res += dfs2(x);
  59.     }
  60.     return res;
  61. }
  62.  
  63. int dfs4(int i)
  64. {
  65.     //cout << i <<  " " << graph2[i].size() << endl;
  66.     int res = 1;
  67.     bylem[i] = 1;
  68.     for(int x : graph2[i])
  69.     {
  70.         if(bylem[x] == 0 and ozn[x] == 0) res += dfs4(x);
  71.     }
  72.     return res;
  73. }
  74.  
  75. main()
  76. {
  77.     ios_base::sync_with_stdio(false);
  78.     cin >> n;
  79.     for(int i = 0; i < maxx; ++i) bylem[i] = ozn[i] =0;
  80.     for(int i = 0; i < n; ++i)
  81.     {
  82.         int a,b; cin >> a >> b;
  83.         if(a != b)graph[a].pb(b); if(a != b)graph2[b].pb(a);
  84.     }
  85.     for(int i = 1; i <=2*n; ++i)
  86.     {
  87.         if(bylem[i] == 0)
  88.         {
  89.             dfs(i);
  90.         }
  91.     }
  92.     reverse(kol.begin(), kol.end());
  93.     for(int i = 0; i < maxx; ++i) bylem[i] = 0;
  94.     for(int i:kol)
  95.     {
  96.         if(bylem[i] == 0)
  97.         {
  98.             int wyn = dfs2(i);
  99.            // cout << i << " " << wyn << endl;
  100.             if(wyn > 1) {ans *= 2; ans %= mod;}
  101.         }
  102.     }
  103.     for(int i = 0; i < maxx; ++i) bylem[i] = 0;
  104.     kol.resize(0);
  105.     for(int i = 1; i <=2*n; ++i)
  106.     {
  107.         if(bylem[i] == 0)
  108.         {
  109.             dfs3(i);
  110.         }
  111.     }
  112.     reverse(kol.begin(), kol.end());
  113.     for(int i = 0; i < maxx; ++i) bylem[i] = 0;
  114.     for(int i : kol)
  115.     {
  116.         if(bylem[i] == 0 and ozn[i] == 0)
  117.         {
  118.             int temp = dfs4(i);
  119.             //cout << i << " " << temp <<  " 2 " << endl;
  120.             if(temp > 1) {ans *= temp; ans %= mod;}
  121.         }
  122.     }
  123.  
  124.     cout << ans << endl;
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement