Advertisement
Guest User

Untitled

a guest
Apr 1st, 2015
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.03 KB | None | 0 0
  1. import java.util.Map;
  2. import java.util.Scanner;
  3. import java.util.HashMap;
  4. import java.util.LinkedList;
  5. import java.util.Collections;
  6. import java.util.Stack;
  7. public class main
  8. {
  9.     public static int mod = 1000000007;
  10.     public static int visited[];
  11.     public static HashMap<Integer,LinkedList<Integer>> graph = new HashMap<Integer,LinkedList<Integer>>();
  12.     public static HashMap<Integer,LinkedList<Integer>> graph2 = new HashMap<Integer,LinkedList<Integer>>();
  13.     public static Stack<Integer> stack = new Stack<Integer>();
  14.     public static LinkedList<Integer> l = new LinkedList<Integer>();
  15.     public static int cost[];
  16.     public static void main(String args[])
  17.     {
  18.         Scanner input = new Scanner(System.in);
  19.         int n = input.nextInt();
  20.         visited = new int[n+1];
  21.         cost =  new int[n];
  22.         for(int i=0;i<n;i++)
  23.         {
  24.             cost[i] = input.nextInt();
  25.         }
  26.         int m = input.nextInt();
  27.         for(int i=0;i<m;i++)
  28.         {
  29.             int a = input.nextInt();
  30.             int b = input.nextInt();
  31.             if(!graph.containsKey(a))
  32.             {
  33.                 graph.put(a,null);
  34.             }
  35.             if(!graph.containsKey(b))
  36.             {
  37.                 graph.put(b,null);
  38.             }
  39.             LinkedList<Integer> list = graph.get(a);
  40.             if(list == null)
  41.             {
  42.                 list = new LinkedList<Integer>();
  43.             }
  44.             list.add(b);
  45.             Collections.sort(list);
  46.             graph.put(a,list);
  47.         }
  48.         scc();      
  49.     }
  50.     public static void getTransponse()
  51.     {
  52.         for (Map.Entry<Integer, LinkedList<Integer>> entry : graph.entrySet())
  53.                 {
  54.                         int key = entry.getKey();
  55.             if(!graph2.containsKey(key))
  56.             {
  57.                 graph2.put(key,null);
  58.             }
  59.                         LinkedList<Integer> value = entry.getValue();
  60.             if(value!=null)
  61.             {
  62.                             for(int i=0;i<value.size();i++)
  63.                 {
  64.                     int x = value.get(i);
  65.                     {
  66.  
  67.                     }
  68.                     if(!graph2.containsKey(x))
  69.                     {
  70.                         graph2.put(x,null);
  71.                     }
  72.                     LinkedList<Integer> list = graph2.get(x);
  73.                     if(list == null)
  74.                     {
  75.                         list = new LinkedList<Integer>();
  76.                     }
  77.                     list.add(key);  
  78.                     Collections.sort(list);
  79.                     graph2.put(x,list);
  80.                 }
  81.             }
  82.                 }
  83.     }
  84.     public static void dfsAndStack()
  85.     {
  86.         for(int i=1;i<visited.length;i++)
  87.                 {
  88.                         if(visited[i] == 0)
  89.                         {
  90.                                 dfsVisit2(i);
  91.                         }
  92.                 }
  93.     }
  94.     public static void dfsVisit2(int i)
  95.         {
  96.                 visited[i] = 1;
  97.                 LinkedList<Integer> list = graph.get(i);
  98.                 if(list != null)
  99.                 {
  100.                         for(int j=0;j<list.size();j++)
  101.                         {
  102.                                 int p = list.get(j);
  103.                                 if(visited[p]==0)
  104.                                         dfsVisit2(p);
  105.                         }
  106.                 }
  107.         stack.push(i);
  108.         }
  109.     public static int min = Integer.MAX_VALUE;
  110.     public static long totalCost = 0;
  111.     public static long count = 0;
  112.     public static long ways = 1;
  113.     public static void scc()
  114.     {
  115.         dfsAndStack();
  116. //      System.out.println(stack);
  117.         getTransponse();
  118. //      System.out.println(graph2);
  119.        
  120.         for(int i=1;i<visited.length;i++)
  121.         {
  122.             visited[i] = 0;
  123.         }
  124.        
  125.         int xy = 0;
  126.         while(!stack.isEmpty())
  127.         {
  128.             int i = stack.pop();
  129.             if(visited[i] == 0)
  130.             {
  131.                 xy = 1;
  132.                 dfsVisit(i);
  133.                 totalCost = (totalCost + min);
  134.                 ways = (ways%mod * count%mod)%mod;
  135.             }
  136. //          if(xy == 0)
  137.             min = Integer.MAX_VALUE;
  138.             count = 0;
  139.         }
  140.         System.out.println(totalCost+" "+ways);
  141.     }
  142.     public static void dfs()
  143.     {
  144.         for(int i=1;i<visited.length;i++)
  145.         {
  146.             if(visited[i] == 0)
  147.             {
  148.                 dfsVisit(i);
  149.             }
  150.         }
  151.     }
  152.     public static void dfsVisit(int i)
  153.     {
  154.         if(cost[i-1] == min)
  155.         {
  156.             count++;
  157.         }
  158.         if(cost[i-1]<min)
  159.         {
  160.             min = cost[i-1];
  161.             count = 1;
  162.         }
  163.         visited[i] = 1;
  164.         LinkedList<Integer> list = graph2.get(i);
  165.         if(list != null)
  166.         {
  167.             for(int j=0;j<list.size();j++)
  168.             {
  169.                 int p = list.get(j);
  170.                 if(visited[p]==0)
  171.                     dfsVisit(p);
  172.             }
  173.         }
  174.     }
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement