【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的长方形去填格子的空缺; 如果有填满的方案且方案 ...
随机推荐
- J20170527-ts
足場 立脚点.脚手架 scaffold ハイパーリンク 超链接 hyperlink アンカータグ 锚标签 でしゃばり 多嘴.多事.多管闲事的人,好出风头的人 でしゃばる 多管闲事 節介 ...
- win7安装oracle
1.下载 2.安装 主目录关键重要
- Hdu 4738 Caocao's Bridges (连通图+桥)
题目链接: Hdu 4738 Caocao's Bridges 题目描述: 有n个岛屿,m个桥,问是否可以去掉一个花费最小的桥,使得岛屿边的不连通? 解题思路: 去掉一个边使得岛屿不连通,那么去掉的这 ...
- 《从Paxos到ZooKeeper 分布式一致性原理与实践》阅读【Leader选举】
从3.4.0版本开始,zookeeper废弃了0.1.2这3种Leader选举算法,只保留了TCP版本的FastLeaderElection选举算法. 当ZooKeeper集群中的一台服务器出现以下两 ...
- 手机页面操作栏的创建及WebFont的使用
一.手机界面底部操作栏的创建. <style> .opers{ position:absolute; bottom:0px; left:0px; right:0px; height:3re ...
- Spark学习之键值对(pair RDD)操作(3)
Spark学习之键值对(pair RDD)操作(3) 1. 我们通常从一个RDD中提取某些字段(如代表事件时间.用户ID或者其他标识符的字段),并使用这些字段为pair RDD操作中的键. 2. 创建 ...
- RecylerView为item添加点击事件
RecyclerView侧重的是布局的灵活性,虽说可以替代ListView但是连基本的点击事件都没有,这篇文章就来详细讲解如何为RecyclerView的item添加点击事件. 1 原理: 为Recy ...
- (转)金蝶KIS迷你版、标准版在查询数量金额明细账时提示“发生未知错误,系统当前操作被取消,请与金蝶公司联系”
金蝶KIS迷你版.标准版在查询数量金额明细账时提示“发生未知错误,系统当前操作被取消,请与金蝶公司联系” 2013-07-10 12:17:51| 分类: 金蝶专题|举报|字号 订阅 金 ...
- form表单清空、重置
form_live为formID <input type="button" value="重置" onclick="$('#form_live' ...
- Change the color of a link in an NSMutableAttributedString
Swift Updated for Swift 3 Use with a textView.linkTextAttributes = [NSForegroundColorAttributeName: ...