luogu P1262 间谍网络 题解

Posted misakaazusa

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了luogu P1262 间谍网络 题解相关的知识,希望对你有一定的参考价值。

题目链接:https://www.luogu.org/problemnew/show/P1262
注意:
1.缩点时计算出入度是在缩完点的图上用color计算。不要在原来的点上计算。
2.枚举出入度时是在缩完点的图上计算。枚举范围到num。

#include <stack>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 10000 + 10;
struct edge{
    int from, next, to, len;
}e[maxn<<2];
int head[maxn], cnt;
int dfn[maxn], low[maxn], tim, color[maxn], num, rudu[maxn];
stack<int> s;
bool vis[maxn], okbuy[maxn], flag[maxn];
int n, p, m, money[maxn], minpay[maxn], ans;
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;
    s.push(x); vis[x] = 1;
    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(low[x] == dfn[x])
    {
        color[x] = ++num;
        vis[x] = 0;
        minpay[num] = min(minpay[num], money[x]);
        while(s.top() != x)
        {
            color[s.top()] = num;
            vis[s.top()] = 0;
            minpay[num] = min(minpay[num], money[s.top()]);
            s.pop();
        }
        s.pop();
    }
}
int main()
{
    memset(head, -1, sizeof(head));
    scanf("%d%d",&n,&p);
    for(int i = 1; i <= n; i++) minpay[i] = 0x7fffffff, money[i] = 0x7fffffff;
    for(int i = 1; i <= p; i++)
    {
        int u, val;
        scanf("%d%d",&u,&val);
        okbuy[u] = 1;
        money[u] = val;
    }
    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] && okbuy[i] == 1) tarjan(i);
    for(int i = 1; i <= n; i++)
        if(!dfn[i])
        {
            printf("NO
%d",i);
            return 0;
        }
    for(int i = 1; i <= n; i++)
        for(int j = head[i]; j != -1; j = e[j].next)
        {
            int v = e[j].to;
            if(color[v] != color[i])
            {
                rudu[color[v]]++;
            }
        }   
    for(int i = 1; i <= num; i++)
    {
        if(rudu[i] == 0)
        ans += minpay[i];
    }
    printf("YES
%d
",ans);
    return 0;   
}

以上是关于luogu P1262 间谍网络 题解的主要内容,如果未能解决你的问题,请参考以下文章

luogu P1262 间谍网络

洛谷——P1262 间谍网络

洛谷 P1262 间谍网络

洛谷 P1262 间谍网络==Codevs 4093 EZ的间谍网络

洛谷P1262 间谍网络

洛谷 P1262 间谍网络