【luogu P2194 HXY烧情侣】 题解

题目链接: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;
}

原文地址:https://www.cnblogs.com/MisakaAzusa/p/9379671.html

时间: 2024-07-31 18:12:00

【luogu P2194 HXY烧情侣】 题解的相关文章

洛谷 P2194 HXY烧情侣【Tarjan缩点】 分析+题解代码

洛谷 P2194 HXY烧情侣[Tarjan缩点] 分析+题解代码 题目描述: 众所周知,HXY已经加入了FFF团.现在她要开始喜(sang)闻(xin)乐(bing)见(kuang)地烧情侣了.这里有n座电影院,n对情侣分别在每座电影院里,然后电影院里都有汽油,但是要使用它需要一定的费用.m条单向通道连接相邻的两对情侣所在电影院.然后HXY有个绝技,如果她能从一个点开始烧,最后回到这个点,那么烧这条回路上的情侣的费用只需要该点的汽油费即可.并且每对情侣只需烧一遍,电影院可以重复去.然后她想花尽

洛谷 P2194 HXY烧情侣

P2194 HXY烧情侣 裸tarjan 1 #include<algorithm> 2 #include<iostream> 3 #include<cstring> 4 #include<cstdio> 5 #include<cmath> 6 #include<queue> 7 using namespace std; 8 #define LL long long 9 #define maxn 1000000 10 #define

HXY烧情侣(洛谷 2194)

题目描述 众所周知,HXY已经加入了FFF团.现在她要开始喜(sang)闻(xin)乐(bing)见(kuang)地烧情侣了.这里有n座电影院,n对情侣分别在每座电影院里,然后电影院里都有汽油,但是要使用它需要一定的费用.m条单向通道连接相邻的两对情侣所在电影院.然后HXY有个绝技,如果她能从一个点开始烧,最后回到这个点,那么烧这条回路上的情侣的费用只需要该点的汽油费即可.并且每对情侣只需烧一遍,电影院可以重复去.然后她想花尽可能少的费用烧掉所有的情侣.问最少需要多少费用,并且当费用最少时的方案

HXY烧情侣

题目描述 众所周知,HXY已经加入了FFF团.现在她要开始喜(sang)闻(xin)乐(bing)见(kuang)地烧情侣了.这里有n座电影院,n对情侣分别在每座电影院里,然后电影院里都有汽油,但是要使用它需要一定的费用.m条单向通道连接相邻的两对情侣所在电影院.然后HXY有个绝技,如果她能从一个点开始烧,最后回到这个点,那么烧这条回路上的情侣的费用只需要该点的汽油费即可.并且每对情侣只需烧一遍,电影院可以重复去.然后她想花尽可能少的费用烧掉所有的情侣.问最少需要多少费用,并且当费用最少时的方案

【luogu P1455 搭配购买】 题解

题目链接:https://www.luogu.org/problemnew/show/P1455 一句话题目做法:并查集合并+01背包 启示:要每次再find一遍.路径压缩会快.因为合并的时候如果是1连3,3连2,4连2,最后也不能保证一步就能连到fa上去. 结果会是fa[2] = fa[3] = fa[4] = 2. fa[1] = 3. 1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm>

【luogu P2455 [SDOI2006]线性方程组】 题解

题目链接:https://www.luogu.org/problemnew/show/P2455 嗯...在消元过程中不要直接拿矩阵元素自己消,会把自己消成0. 1 #include <algorithm> 2 #include <cstdio> 3 #include <cmath> 4 #include <iostream> 5 using namespace std; 6 const int maxn = 200; 7 const double eps

【luogu P4568 [JLOI2011]飞行路线】 题解

题目链接:https://www.luogu.org/problemnew/show/P4568 卡了一晚上,算是分层图最短路的模板.注意卡SPFA,所以我写了个SLF优化. 同时 AC400祭!~ #include <queue> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define ri register using name

【luogu P1262 间谍网络】 题解

题目链接:https://www.luogu.org/problemnew/show/P1262 注意: 1.缩点时计算出入度是在缩完点的图上用color计算.不要在原来的点上计算. 2.枚举出入度时是在缩完点的图上计算.枚举范围到num. #include <stack> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using nam

【luogu P1456 Monkey King】 题解

题目链接:https://www.luogu.org/problemnew/show/P1456 左偏树并查集不加路径压缩吧... #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; const int maxn = 100000 + 10; struct Left_Tree{ int val, fa, son[2