Tajan BFs of Strongly Connected Components in Luogu P1262 Spy Network

Posted by Stegs on Thu, 08 Aug 2019 15:57:31 +0200

Topic link:

https://www.luogu.org/problem/P1262

Explain:

It's particularly strange that my code really feels right and submits ac, but when I run Sample 2 on cb, it fails. Maybe it's wrong.

Ideas:

1:tarjan contraction point

2: After the reduction point of step 1, each node represents a strongly connected component. At this time, the nodes are chained with no rings.

3: Firstly, we deal with the strongly connected component of the spy that can be counterfeited. If there are more than one counterfeited spy in a strongly connected component, we will take the least cost.

int en[cnt+1][2];
    for(int i=1;i<=cnt;i++)
    {
        en[i][1]=1e9;
    }
    for(int i=1;i<=p;i++)
    {
        en[color[money[i][0]]][1]=min(en[color[money[i][0]]][1],money[i][1]);
    }

en[i][0] has a strong connected component number, that is, the node number of the reconstructed graph, and en[i][1] has the minimum cost that the strong connected component can be counteracted.

4: Regarding the reconstructed graph, we should understand that if every node can be traversed, then we must ensure that all nodes with a degree of zero can be counteracted.

1: If all nodes with zero entrance can be counteracted, the cost of counteracting these nodes and the minimum cost will be achieved.

2: As long as it is found that a node with zero entrance can not be countered, it means that all nodes can not be countered, then run bfs on all nodes that can be counteracted, and then traverse all nodes sequentially, and find that the first strongly connected component has not been traversed. A node is the smallest spy number in an uncontrollable spy.

int sum=0;
    for(int i=1;i<=cnt;i++)
    {
        if(id[i]==0)
        {
            if(en[i][1]!=1e9)
            {
                bfs(i);
                sum+=en[i][1];
            }
            else
            {
                for(int j=1;j<=cnt;j++)
                {
                    if(en[j][1]!=1e9)
                    {
                        bfs(j);
                    }
                }
                for(int j=1;j<=n;j++)
                {
                         if(colorcnt[color[j]]==0)
                         {
                             cout<<"NO"<<endl<<j<<endl;
                             break;
                         }
                }
                return 0;
            }
        }
    }
    cout<<"YES"<<endl<<sum<<endl;
    return 0;
#include <bits/stdc++.h>

using namespace std;
const int maxn=3e3+1;
vector<int>e[maxn];
set<int>ee[maxn];
int dfn[maxn],low[maxn],ins[maxn],color[maxn],timing,cnt,n,p,m,id[maxn],colorcnt[maxn];
stack<int>s;
vector<pair<int,int> >mapp[maxn];
int ing[maxn];
int money[maxn][2];

void tarjan(int x)
{
    low[x]=dfn[x]=++timing;
    s.push(x);
    ins[x]=1;
    for(int i=0;i<e[x].size();i++)
    {
        int v=e[x][i];
        if(!dfn[v])
        {
            tarjan(v);
            low[x]=min(low[x],low[v]);
        }
        else if(ins[v])
        {
            low[x]=min(low[x],dfn[v]);
        }
    }
    if(dfn[x]==low[x])
    {
        cnt++;
        while(s.top()!=x)
        {
            color[s.top()]=cnt;
            ins[s.top()]=0;
            s.pop();
        }
        color[s.top()]=cnt;
        ins[s.top()]=0;
        s.pop();
    }
}

int bfs(int x)
{
    queue<int>q;
    q.push(x);
    ing[x]=1;
    while(!q.empty())
    {
        int now=q.front();
        colorcnt[now]=1;
        q.pop();
        ing[now]=0;
        for(int i=0;i<mapp[now].size();i++)
        {
                int v=mapp[now][i].first;
                if(ing[v])
                    continue;
                q.push(v);
                ing[v]=1;
        }
    }
}

int main()
{
    ios::sync_with_stdio(0);
    cin>>n>>p;
    for(int i=1;i<=p;i++)
    {
        cin>>money[i][0]>>money[i][1];
    }
    cin>>m;
    for(int i=1;i<=m;i++)
    {
        int a,b;
        cin>>a>>b;
        e[a].push_back(b);
    }
    for(int i=1;i<=n;i++)
    {
        if(!dfn[i])
            tarjan(i);
    }
    int en[cnt+1][2];
    for(int i=1;i<=cnt;i++)
    {
        en[i][1]=1e9;
    }
    for(int i=1;i<=p;i++)
    {
        en[color[money[i][0]]][1]=min(en[color[money[i][0]]][1],money[i][1]);
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=0;j<e[i].size();j++)
        {
            if(color[i]!=color[e[i][j]]&&ee[color[i]].find(color[e[i][j]])==ee[color[i]].end())
            {
                ee[color[i]].insert(color[e[i][j]]);
                id[color[e[i][j]]]++;
            }
        }
    }
    int sum=0;
    for(int i=1;i<=cnt;i++)
    {
        if(id[i]==0)
        {
            if(en[i][1]!=1e9)
            {
                bfs(i);
                sum+=en[i][1];
            }
            else
            {
                for(int j=1;j<=cnt;j++)
                {
                    if(en[j][1]!=1e9)
                    {
                        bfs(j);
                    }
                }
                for(int j=1;j<=n;j++)
                {
                         if(colorcnt[color[j]]==0)
                         {
                             cout<<"NO"<<endl<<j<<endl;
                             break;
                         }
                }
                return 0;
            }
        }
    }
    cout<<"YES"<<endl<<sum<<endl;
    return 0;
}

 

Topics: iOS