【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的长方形去填格子的空缺; 如果有填满的方案且方案 ...
随机推荐
- Tomcat优化和JVM分析工具
Tomcat的常见优化和JVM常见分析工具 Tomcat的常用优化配置 (1) 内存空间: /etc/sysconfig/tomcat JAVA_OPTS="-server -Xms32g ...
- hdu4292 Food 最大流模板题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4292 题意:水和饮料,建图跑最大流模板. 我用的是学长的模板,最然我还没有仔细理解,不过这都不重要直接 ...
- ubuntu 12.04不能mount nfs目录与挂载后只读不能写问题 (转载)
转自:http://blog.chinaunix.net/uid-20680966-id-3810455.html 服务器用的是fedora 12 以前在helper2416开发板上挂载服务器上的n ...
- IDEA 激活方式
最新的IDEA激活方式 使用网上传统的那种输入网址的方式激活不了,使用http://idea.lanyus.com/这个网站提供的工具进行 1.进入hosts文件中:C:\Windows\System ...
- 二分搜索poj106
Cable master Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 49944 Accepted: 10493 De ...
- SQL中CRUD C——create 添加数据 R——read 读取数据 U——update 修改数据 D——delete 删除数据
在SQL server中对数据库的操作: 删除表:drop table 表名修改表:alter table 表名 添加列add 列名 列类型alter table 表名 drop column 列名 ...
- JSP&&EL&&JSTL
JSP = JAVA + HTML + JSP自身的东西 JSP的脚本 <%! %> :翻译成Servlet中的成员内容. 定义变量,方法,类. -- ...
- python自动化--语言基础线程、生产者消费者示例
进程与线程的区别:进程不共享空间,线程共享地址空间 线程共享空间优缺点:优点:多线程给用户的体验好些,打开时占用的内存比进程少缺点:共享地址空间会相互干扰,甚至有影响 import threading ...
- 梦想CAD控件图层COM接口知识点
梦想CAD控件图层COM接口知识点 一.新建图层 主要用到函数说明: _DMxDrawX::AddLayer 增加新的图层.详细说明如下: 参数 说明 BSTR pszName 图层名 c#中实现代码 ...
- Android studio 开发一个用户登录界面
Android studio 开发一个用户登录界面 activity_main.xml <?xml version="1.0" encoding="utf-8&qu ...