首页 技术 正文
技术 2022年11月8日
0 收藏 952 点赞 1,119 浏览 1411 个字

题目链接:https://www.luogu.org/problemnew/show/P2194

第一问:缩点并且统计其强连通分量里的最小耗费。把所有强连通分量的最小耗费加起来。

第二问:统计在每个强连通分量里与最小耗费相同的点数。乘法原理统计所有强连通分量答案。

#include <stack>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 300000 + 10;
const int inf = 0x7fffffff;
const int mod = 1e9 + 7;
struct edge{
int from, to, next;
}e[maxn<<2];
int head[maxn], cnt;
int n, m, ans1, ans2 = 1, dfn[maxn], low[maxn], tim, color[maxn], num, val[maxn], minpay[maxn], tot[maxn];
bool vis[maxn];
stack<int> s;
void add(int u, int v)
{
e[++cnt].from = u;
e[cnt].next = head[u];
e[cnt].to = v;
head[u] = cnt;
}
void tarjan(int x)
{
dfn[x] = low[x] = ++tim;
vis[x] = 1; s.push(x);
for(int i = head[x]; i != -1; i = e[i].next)
{
int v = e[i].to;
if(!dfn[v])
{
tarjan(v);
low[x] = min(low[x], low[v]);
}
else if(vis[v])
{
low[x] = min(low[x], low[v]);
}
}
if(dfn[x] == low[x])
{
color[x] = ++num;
vis[x] = 0;
minpay[num] = min(minpay[num], val[x]);
while(s.top() != x)
{
color[s.top()] = num;
vis[s.top()] = 0;
minpay[num] = min(minpay[num], val[s.top()]);
s.pop();
}
s.pop();
}
}
int main()
{
memset(head, -1, sizeof(head));
scanf("%d",&n);
for(int i = 1; i <= n; i++) minpay[i] = inf;
for(int i = 1; i <= n; i++) scanf("%d",&val[i]);
scanf("%d",&m);
for(int i = 1; i <= m; i++)
{
int u, v;
scanf("%d%d",&u,&v);
add(u,v);
}
for(int i = 1; i <= n; i++)
if(!dfn[i]) tarjan(i);
//for(int i = 1; i <= n; i++) cout<<minpay[i];
for(int i = 1; i <= num; i++) ans1 += minpay[i];
cout<<ans1<<" ";
for(int i = 1; i <= n; i++)
{
if(val[i] == minpay[color[i]])
tot[color[i]]++;
}
for(int i = 1; i <= num; i++) ans2 = (ans2*tot[i])%mod;
cout<<ans2<<endl;
return 0;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,129
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,601
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,444
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,218
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,852
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,940