xtu summer individual 6 D - Checkposts
Checkposts
This problem will be judged on CodeForces. Original ID: 427C
64-bit integer IO format: %I64d Java class name: (Any)
To ensure the security, you have to build some police checkposts. Checkposts can only be built in a junction. A checkpost at junction i can protect junction j if either i = j or the police patrol car can go to j from i and then come back to i.
Building checkposts costs some money. As some areas of the city are more expensive than others, building checkpost at some junctions might cost more money than other junctions.
You have to determine the minimum possible money needed to ensure the security of all the junctions. Also you have to find the number of ways to ensure the security in minimum price and in addition in minimum number of checkposts. Two ways are different if any of the junctions contains a checkpost in one of them and do not contain in the other.
Input
In the first line, you will be given an integer n, number of junctions (1 ≤ n ≤ 105). In the next line, n space-separated integers will be given. The ith integer is the cost of building checkpost at the ith junction (costs will be non-negative and will not exceed 109).
The next line will contain an integer m (0 ≤ m ≤ 3·105). And each of the next m lines contains two integers ui and vi (1 ≤ ui, vi ≤ n; u ≠ v). A pair ui, vi means, that there is a one-way road which goes from ui to vi. There will not be more than one road between two nodes in the same direction.
Output
Print two integers separated by spaces. The first one is the minimum possible money needed to ensure the security of all the junctions. And the second one is the number of ways you can ensure the security modulo 1000000007 (109 + 7).
Sample Input
3
1 2 3
3
1 2
2 3
3 2
3 1
5
2 8 0 6 0
6
1 4
1 3
2 4
3 4
4 5
5 1
8 2
10
1 3 2 2 1 3 1 4 10 10
12
1 2
2 3
3 1
3 4
4 5
5 6
5 7
6 4
7 3
8 9
9 10
10 9
15 6
2
7 91
2
1 2
2 1
7 1
Source
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
const LL mod = ;
vector<int>g[maxn];
stack<int>s;
int w[maxn],dfn[maxn],low[maxn],n,m,id,scc;
bool instack[maxn];
LL cnt[maxn],sum,ways;
void tarjan(int u){
dfn[u] = low[u] = ++id;
instack[u] = true;
s.push(u);
int v;
for(v = ; v < g[u].size(); v++){
if(!dfn[g[u][v]]){
tarjan(g[u][v]);
low[u] = min(low[u],low[g[u][v]]);
}else if(instack[g[u][v]] && low[u] > dfn[g[u][v]])
low[u] = dfn[g[u][v]];
}
if(dfn[u] == low[u]){
int theMin = INF;
do{
v = s.top();
s.pop();
if(w[v] < theMin){
theMin = w[v];
cnt[scc] = ;
}else if(w[v] == theMin){
cnt[scc]++;
}
instack[v] = false;
}while(v != u);
scc++;
sum += theMin;
}
}
int main() {
int i,j,u,v;
while(~scanf("%d",&n)){
for(i = ; i <= n; i++)
scanf("%d",w+i);
for(i = ; i <= n; i++){
g[i].clear();
instack[i] = false;
low[i] = dfn[i] = ;
cnt[i] = ;
}
scanf("%d",&m);
for(i = ; i < m; i++){
scanf("%d %d",&u,&v);
g[u].push_back(v);
}
while(!s.empty()) s.pop();
sum = scc = id = ;
for(i = ; i <= n; i++)
if(!dfn[i]) tarjan(i);
for(ways = ,i = ; i < scc; i++){
ways = ((ways%mod)*(cnt[i]%mod))%mod;
}
printf("%I64d %I64d\n",sum,ways);
}
return ;
}
xtu summer individual 6 D - Checkposts的更多相关文章
- xtu summer individual 4 C - Dancing Lessons
Dancing Lessons Time Limit: 5000ms Memory Limit: 262144KB This problem will be judged on CodeForces. ...
- xtu summer individual 3 C.Infinite Maze
B. Infinite Maze time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- xtu summer individual 2 E - Double Profiles
Double Profiles Time Limit: 3000ms Memory Limit: 262144KB This problem will be judged on CodeForces. ...
- xtu summer individual 2 C - Hometask
Hometask Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Origin ...
- xtu summer individual 1 A - An interesting mobile game
An interesting mobile game Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on H ...
- xtu summer individual 2 D - Colliders
Colliders Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Origi ...
- xtu summer individual 1 C - Design the city
C - Design the city Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu D ...
- xtu summer individual 1 E - Palindromic Numbers
E - Palindromic Numbers Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %l ...
- xtu summer individual 1 D - Round Numbers
D - Round Numbers Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u D ...
随机推荐
- codeforces 555B Case of Fugitive
题目连接: http://codeforces.com/problemset/problem/555/B 题目大意: 有n个岛屿(岛屿在一列上,可以看做是线性的,用来描述岛屿位置的是起点与终点),m个 ...
- Tree CodeForces -932D
错误记录:如下注释语句 #include<cstdio> #include<algorithm> using namespace std; typedef long long ...
- 递推DP UVA 590 Always on the run
题目传送门 题意:题意难懂,就是一个小偷在m天内从城市1飞到城市n最小花费,输入的是每个城市飞到其他城市的航班. 分析:dp[i][j] 表示小偷第i天在城市j的最小花费.状态转移方程:dp[i][j ...
- multiset || 线段树 HDOJ 4302 Holedox Eating
题目传送门 题意:一个长度L的管子,起点在0.n次操作,0 p表示在p的位置放上蛋糕,1表示去吃掉最近的蛋糕(如果左右都有蛋糕且距离相同,那么吃同方向的蛋糕),问最终走了多少路程 分析:用multis ...
- vijos P1426兴奋剂检查 多维费用背包问题的hash
https://vijos.org/p/1426 这是个好题,容易想到用dp[i][v1][v2][v3][v4][v5]表示在前i个物品中,各种东西的容量是那个的时候,能产生的最大价值. 时间不会T ...
- Objective - c Foundation 框架详解2
Objective - c Foundation 框架详解2 Collection Agency Cocoa provides a number of collection classes such ...
- 解决重置PostgreSQL 9.6密码的问题
一.PostgreSql9.6重置密码的方法: 1.打开windows服务管理器,找到“postgresql-x64-9.6”服务,停止服务. 2.找到PostgreSQL9.6的安装目录(以我的E盘 ...
- vue2.0 v-model指令
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 洛谷 P2801 教主的魔法
题目描述 教主最近学会了一种神奇的魔法,能够使人长高.于是他准备演示给XMYZ信息组每个英雄看.于是N个英雄们又一次聚集在了一起,这次他们排成了一列,被编号为1.2.…….N. 每个人的身高一开始都是 ...
- java web 学习笔记 - servlet03
1.Servlet可以分为三种类型 普通Servlet,需要在基本程序架构中体现. Servlet过滤器,在web容器启动时初始化,不需要手动调用. Servlet 监听器. 2. Servlet过 ...