Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- //#include <bits/stdc++.h>
- #include <stack>
- #include <vector>
- #include <cstring>
- #include <iostream>
- using namespace std;
- vector<int>graph[500000];
- vector<int>vsuma[100005];
- int niza[100005];
- int najmal=2e9;
- vector<int>rev_graph[500000];
- int suma[100005];
- bool visited[500000];
- int brojac1=0;
- stack<int>s;
- void dfs(int node){
- visited[node]=true;
- for(int i=0; i<graph[node].size(); i++){
- int sosed=graph[node][i];
- if(!visited[sosed]){
- dfs(sosed);
- }
- }
- s.push(node);
- }
- void rev_dfs(int node){
- visited[node]=true;
- if(suma[node]<najmal){
- najmal=suma[node];
- }
- ////cout<<node<<" ";
- for(int i=0; i<rev_graph[node].size(); i++){
- int rev_sosed=rev_graph[node][i];
- if(!visited[rev_sosed]){
- rev_dfs(rev_sosed);
- }
- }
- }
- bool visited2[500005];
- void rev_dfs1(int node){
- visited2[node] = true;
- if(suma[node]==najmal){
- brojac1+=1;
- }
- // cout << najmal << " " << node + 1 << endl;
- for(int i=0; i<rev_graph[node].size(); i++){
- int sosed=rev_graph[node][i];
- if(!visited2[sosed])
- rev_dfs1(sosed);
- }
- }
- int main()
- {
- int a, b;
- long long brojac=0;
- cin>>a;
- vector<int>v;
- for(int i=0; i<a; i++){
- cin>>suma[i];
- }
- cin>>b;
- memset(visited, false, sizeof visited);
- for(int i=0; i<b; i++){
- int k, k1;
- cin>>k>>k1;
- k1--;
- k--;
- graph[k].push_back(k1);
- rev_graph[k1].push_back(k);
- }
- for(int i=0; i<a; i++){
- if(!visited[i]){
- dfs(i);
- }
- }
- memset(visited2, false, sizeof visited2);
- memset(visited, false, sizeof visited);
- while(!s.empty()){
- int node=s.top();
- if(!visited[node]){
- najmal = 2e9;
- brojac1 = 0;
- rev_dfs(node);
- brojac+=najmal;
- rev_dfs1(node);
- v.push_back(brojac1);
- }
- s.pop();
- }
- //cout<<brojac<<" ";
- long long brojac2=1;
- long long MOD = 1e9 + 7;
- for(int i=0; i<v.size(); i++){
- // cout<<v[i]<<endl;
- brojac2*=v[i];
- brojac2 %= MOD;
- }
- cout<<brojac << " " << brojac2 << endl;;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement