Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Map;
- import java.util.Scanner;
- import java.util.HashMap;
- import java.util.LinkedList;
- import java.util.Collections;
- import java.util.Stack;
- public class main
- {
- public static int mod = 1000000007;
- public static int visited[];
- public static HashMap<Integer,LinkedList<Integer>> graph = new HashMap<Integer,LinkedList<Integer>>();
- public static HashMap<Integer,LinkedList<Integer>> graph2 = new HashMap<Integer,LinkedList<Integer>>();
- public static Stack<Integer> stack = new Stack<Integer>();
- public static LinkedList<Integer> l = new LinkedList<Integer>();
- public static int cost[];
- public static void main(String args[])
- {
- Scanner input = new Scanner(System.in);
- int n = input.nextInt();
- visited = new int[n+1];
- cost = new int[n];
- for(int i=0;i<n;i++)
- {
- cost[i] = input.nextInt();
- }
- int m = input.nextInt();
- for(int i=0;i<m;i++)
- {
- int a = input.nextInt();
- int b = input.nextInt();
- if(!graph.containsKey(a))
- {
- graph.put(a,null);
- }
- if(!graph.containsKey(b))
- {
- graph.put(b,null);
- }
- LinkedList<Integer> list = graph.get(a);
- if(list == null)
- {
- list = new LinkedList<Integer>();
- }
- list.add(b);
- Collections.sort(list);
- graph.put(a,list);
- }
- scc();
- }
- public static void getTransponse()
- {
- for (Map.Entry<Integer, LinkedList<Integer>> entry : graph.entrySet())
- {
- int key = entry.getKey();
- if(!graph2.containsKey(key))
- {
- graph2.put(key,null);
- }
- LinkedList<Integer> value = entry.getValue();
- if(value!=null)
- {
- for(int i=0;i<value.size();i++)
- {
- int x = value.get(i);
- {
- }
- if(!graph2.containsKey(x))
- {
- graph2.put(x,null);
- }
- LinkedList<Integer> list = graph2.get(x);
- if(list == null)
- {
- list = new LinkedList<Integer>();
- }
- list.add(key);
- Collections.sort(list);
- graph2.put(x,list);
- }
- }
- }
- }
- public static void dfsAndStack()
- {
- for(int i=1;i<visited.length;i++)
- {
- if(visited[i] == 0)
- {
- dfsVisit2(i);
- }
- }
- }
- public static void dfsVisit2(int i)
- {
- visited[i] = 1;
- LinkedList<Integer> list = graph.get(i);
- if(list != null)
- {
- for(int j=0;j<list.size();j++)
- {
- int p = list.get(j);
- if(visited[p]==0)
- dfsVisit2(p);
- }
- }
- stack.push(i);
- }
- public static int min = Integer.MAX_VALUE;
- public static long totalCost = 0;
- public static long count = 0;
- public static long ways = 1;
- public static void scc()
- {
- dfsAndStack();
- // System.out.println(stack);
- getTransponse();
- // System.out.println(graph2);
- for(int i=1;i<visited.length;i++)
- {
- visited[i] = 0;
- }
- int xy = 0;
- while(!stack.isEmpty())
- {
- int i = stack.pop();
- if(visited[i] == 0)
- {
- xy = 1;
- dfsVisit(i);
- totalCost = (totalCost + min);
- ways = (ways%mod * count%mod)%mod;
- }
- // if(xy == 0)
- min = Integer.MAX_VALUE;
- count = 0;
- }
- System.out.println(totalCost+" "+ways);
- }
- public static void dfs()
- {
- for(int i=1;i<visited.length;i++)
- {
- if(visited[i] == 0)
- {
- dfsVisit(i);
- }
- }
- }
- public static void dfsVisit(int i)
- {
- if(cost[i-1] == min)
- {
- count++;
- }
- if(cost[i-1]<min)
- {
- min = cost[i-1];
- count = 1;
- }
- visited[i] = 1;
- LinkedList<Integer> list = graph2.get(i);
- if(list != null)
- {
- for(int j=0;j<list.size();j++)
- {
- int p = list.get(j);
- if(visited[p]==0)
- dfsVisit(p);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement