【Codeforces 427C】Checkposts
【链接】  我是链接,点我呀:) 
 【题意】
环里面的点只需要一个点就能全都保护
问你最少需要多少花费以及最少的点才能将所有的点都保护
【题解】
有向图的强连通分量求出所有的联通分量
显然每个联通分量里面只需选择最小那个点就好
如果有多个最小的点,那么这个环就有多个选择。
每个环的最小点个数连乘一下就是方案数了
然后每个联通分量的最小值再全部都加起来就ok
【代码】
import java.io.*;
import java.util.*;
public class Main {
    static InputReader in;
    static PrintWriter out;
    public static void main(String[] args) throws IOException{
        //InputStream ins = new FileInputStream("E:\\rush.txt");
        InputStream ins = System.in;
        in = new InputReader(ins);
        out = new PrintWriter(System.out);
        //code start from here
        new Task().solve(in, out);
        out.close();
    }
    static int N = (int)1e5;
    static class Task{
        int n,m;
        ArrayList g[] = new ArrayList[N+10],g1[] = new ArrayList[N+10];
        int cost[] = new int[N+10];
        int dfn[] = new int[N+10],low[] = new int[N+10];
        boolean in[] = new boolean[N+10];
        int mystack[] = new int[N+10];
        int tot = 0,top = 0,totn = 0;
        void dfs(int x) {
        	dfn[x] = low[x] = ++tot;
        	mystack[++top] = x;
        	in[x] = true;
        	int len = g[x].size();
        	for (int i = 0;i < len;i++) {
        		int y = (int)g[x].get(i);
        		if (dfn[y]==0) {
        			dfs(y);
        			low[x] = Math.min(low[x],low[y]);
        		}else
        			if (in[y] && dfn[y]<low[x]){
        				low[x] = dfn[y];
        			}
        	}
        	if (low[x]==dfn[x]) {
        		int v = 0;
        		totn++;
        		while (v!=x) {
        			v = mystack[top];
        			in[v] = false;
        			g1[totn].add(v);
        			top--;
        		}
        	}
        }
        public void solve(InputReader in,PrintWriter out) {
        	for (int i = 1;i <= N;i++) {
        		g[i] = new ArrayList();
        		g1[i] = new ArrayList();
        	}
        	n = in.nextInt();
        	for (int i = 1;i <= n;i++) cost[i] = in.nextInt();
        	m = in.nextInt();
        	for (int i = 1;i <= m;i++) {
        		int x,y;
        		x = in.nextInt();y = in.nextInt();
        		g[x].add(y);
        	}
        	for (int i = 1;i <= n;i++)
        		if (dfn[i]==0) dfs(i);
        	long ans = 0;
        	long way = 1;
        	for (int i = 1;i <= totn;i++) {
        		long mi = cost[(int)g1[i].get(0)];
        		long cnt = 1;
        		for (int j = 1;j < (int)g1[i].size();j++) {
        			int x = (int)g1[i].get(j);
        			x = cost[x];
        			if (x<mi) {
        				mi = x;
        				cnt = 1;
        			}else if (x==mi) cnt++;
        		}
        		ans = ans + mi;
        		way = (way*cnt)%((int)1e9 + 7);
        	}
        	out.println(ans+" "+way);
        }
    }
    static class InputReader{
        public BufferedReader br;
        public StringTokenizer tokenizer;
        public InputReader(InputStream ins) {
            br = new BufferedReader(new InputStreamReader(ins));
            tokenizer = null;
        }
        public String next(){
            while (tokenizer==null || !tokenizer.hasMoreTokens()) {
                try {
                tokenizer = new StringTokenizer(br.readLine());
                }catch(IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }
        public int nextInt() {
            return Integer.parseInt(next());
        }
    }
}
												
											【Codeforces 427C】Checkposts的更多相关文章
- 【codeforces 415D】Mashmokh and ACM(普通dp)
		
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
 - 【codeforces 707E】Garlands
		
[题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...
 - 【codeforces 707C】Pythagorean Triples
		
[题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...
 - 【codeforces 709D】Recover the String
		
[题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...
 - 【codeforces 709B】Checkpoints
		
[题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...
 - 【codeforces 709C】Letters Cyclic Shift
		
[题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...
 - 【Codeforces 429D】 Tricky Function
		
[题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...
 - 【Codeforces 670C】 Cinema
		
[题目链接] http://codeforces.com/contest/670/problem/C [算法] 离散化 [代码] #include<bits/stdc++.h> using ...
 - 【codeforces 515D】Drazil and Tiles
		
[题目链接]:http://codeforces.com/contest/515/problem/D [题意] 给你一个n*m的格子; 然后让你用1*2的长方形去填格子的空缺; 如果有填满的方案且方案 ...
 
随机推荐
- nodejs实现验证码
			
http://www.9958.pw/post/nodejs_lesson http://www.9958.pw/post/nodejscapp
 - mysql通用分页存储过程遇到的问题(转载)
			
mysql通用分页存储过程遇到的问题(转载) http://www.cnblogs.com/daoxuebao/archive/2015/02/09/4281980.html
 - CodeForces 632C Grandma Laura and Apples (模拟)
			
题意:有n个人买苹果,当苹果剩余偶数时买走一半,当苹果剩余奇数时,先买走一半,再用半价买走一个苹果,最终苹果恰好卖完.农民收入为多少. 析:反向模拟. 代码如下: #pragma comment(li ...
 - 关于Webpack的的认识及傻瓜式教学
			
刚学习了Webpack,ememememememememem就赶脚是一个打包工具,将js.css.json.img等等通通打包为最终的文件,最后渲染为一个页面. 也是终于捋清了Webpack的思路,在 ...
 - Windows8.1进入IIS管理器的方法
			
以前在本机的Windows8.1操作系统中安装了IIS,很久没有使用过,今天在安装IBM Http Server的时候启动失败,才想起来IIS占用了80端口,需要把IIS服务停止掉.找了半天才找到进入 ...
 - SQL数据库基础知识——抽象类
			
抽象类,只为继承而出现,不定义具体的内容,只规定该有哪些东西:一般抽象类中只放置抽象方法,只规定了返回类型和参数:比如: 人 - 有吃饭,睡觉方法: 男人 - 继承人抽象类,必须实现吃饭,睡觉的方法主 ...
 - Mui使用jquery并且使用点击跳转新窗口
			
网上好多朋友是这样做的: 全局插入了js代码 mui('body').on('tap', 'a', function () { document.location.href = this.href; ...
 - opencv3.3+vs2015调用笔记本摄像头成功
			
先上代码 成功图片如下: #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp&g ...
 - IDEA打可执行jar包
			
流程: 1. File ->Project Structure -> Artifacts -> + -> JAR -> From modules with depende ...
 - 如何在网页中浏览和编辑DWG文件 梦想CAD控件
			
如何在网页中浏览和编辑DWG文件 梦想CAD控件 www.mxdraw.com 梦想绘图控件5.2 是国内最强,最专业的CAD开发组件(控件),不需要AutoCAD就能独立运行.控件使用VC 201 ...