zoukankan      html  css  js  c++  java
  • 【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;
    }
    
  • 相关阅读:
    PHP读取xlsx Excel 文件
    mysql无法创建外键问题
    MySQL数据库远程连接
    CentOS 配置防火墙操作实例(启、停、开、闭端口)
    linux实时查看更新日志命令
    国家/地区 语言缩写代码 查询备用
    php 滑动验证码
    php dirname(__FILE__) 获取当前文件的绝对路径
    PHP cannoy modify header information
    Mysql中用SQL增加、删除、修改(包括字段长度/注释/字段名)总结
  • 原文地址:https://www.cnblogs.com/MisakaAzusa/p/9379671.html
Copyright © 2011-2022 走看看