Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.79 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <queue>
  5. #include <cmath>
  6. #include <stack>
  7. #include <iomanip>
  8. #include <bitset>
  9.  
  10. #define szybcior ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  11.  
  12. using namespace std;
  13. typedef long long ll;
  14. typedef long double ld;
  15. const ll inf = 10e17;
  16. const ll M = 1000000007;
  17. const ll ile = 100008;
  18. ll n,m;
  19. ll koszt[ile];
  20. ll post[ile];
  21. vector<ll> v[ile];
  22. vector<ll> v1[ile];
  23. bitset<ile> bo;
  24. bitset<ile> bo1;
  25. ll czas = 0;
  26. pair<ll,ll> ans = {0,1}; // koszst | ilosc mozliwosci
  27.  
  28.  
  29. void dfs(ll start)
  30. {
  31.       bo[start] = true;
  32.       // cout << "wierzcholek " << start << "\n";
  33.       for(auto it: v[start])
  34.             if(!bo[it])
  35.                   dfs(it);
  36.  
  37.       post[++czas] = start;
  38. }
  39.  
  40. // ll dfs1(ll start)
  41. // {
  42. //       bo1[start] = true;
  43. //       ll wartosc = koszt[start];
  44. //       // cout << "wierzcholek " << start << "\n";
  45. //       for(auto it: v1[start])
  46. //             if(!bo1[it])
  47. //             {
  48. //                   ll pomoc = dfs1(it);
  49. //                   if(pomoc == wartosc) ilosc++;
  50. //                   else if(pomoc < wartosc)
  51. //                   {
  52. //                         cout << "ZMIEJSZANIE " << wartosc << " na " << pomoc << " ";
  53. //                         ilosc = 1;
  54. //                         wartosc = pomoc;
  55. //                   }
  56. //                   ilosc %= M;
  57. //             }
  58.  
  59. //       cout << start << ": " << ilosc << " wartosc: " << wartosc << "\n";
  60. //       return wartosc;
  61. // }
  62.  
  63. void dfs1(ll v, ll& cand, ll& cnt) {
  64.       bo1[v] = true;
  65.       cnt %= M;
  66.       if(koszt[v] < cand)
  67.             cand=koszt[v], cnt=0;
  68.       for(auto && it: v1[v])
  69.             if(!bo1[it])
  70.                   dfs1(it, cand, cnt);
  71.       if(koszt[v] ==cand)
  72.             cnt++, cnt %= M;
  73.       cnt %= M;
  74. }
  75. int main()
  76. {
  77.       szybcior;
  78.       cin >> n;
  79.       for(int i = 1; i <= n; i++)
  80.             cin >> koszt[i];
  81.      
  82.       cin >> m;
  83.       for(int i = 0; i < m; i++)
  84.       {
  85.             ll a,b;
  86.             cin >> a >> b;
  87.             v[a].push_back(b);
  88.             v1[b].push_back(a);
  89.       }
  90.      
  91.       for(int i = 1; i <= n; i++)
  92.             if(!bo[i])
  93.                   dfs(i); // ustawienie post orderu
  94.  
  95.  
  96.       // for(int i = 1; i <= n; i++)   cout << i << ": " << post[i] << "\n";      
  97.       for(int i = n; i > 0; i--)
  98.       {
  99.             // cout << 'x';
  100.             if(!bo1[post[i]])
  101.             {
  102.                   ll ile =-1, mini = inf ;
  103.                   dfs1(post[i], mini, ile);
  104.                   // cout << mini << " " << ile << "\n";
  105.                   ans.first +=  mini;
  106.                   ans.second *= (ile % M);
  107.                   ans.second %= M;
  108.             }
  109.            
  110.       }
  111.       cout << ans.first << " " << ans.second << " ";
  112.       return 0;
  113.  
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement