Delivery message

https://loj.ac/problem/10094

Title Description

  One-way message passing, how many people seek at least to make everyone know the news.

Thinking

  For strongly connected components apparent in each reachable point, regardless, then we point reduction for the DAG, as long as the message to the person to 0 degree, others can certainly get the message through the channel.

Code

#include <bits/stdc++.h>
using namespace std;
const int N=1100,M=1e6+10;

struct Edge
{
    int x,y;
}e[M];

int nxt[M],head[N],to[M],tot;
void add_edge(int x,int y)
{
    nxt[++tot]=head[x];
    head[x]=tot;
    to[tot]=y;
    e [tot] .x = x; e [tot] .y = Y;
}

int read()
{
    int res=0,w=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){res=(res<<3)+(res<<1)+ch-'0';ch=getchar();}
    return res*w;
}
int dfn[N],low[N],st[N],top,col,co[N],idx;
void tarjan(int u)
{
    dfn[u]=low[u]=++idx;
    st[++top]=u;
    for(int i=head[u];i;i=nxt[i])
    {
        int v=to[i];
        if(!dfn[v])
        {
            Tarjan (v);
            low[u]=min(low[u],low[v]);
        }
        else if(!co[v])
            low[u]=min(low[u],dfn[v]);
    }
    if(low[u]==dfn[u])
    {
        co[u]=++col;
        while(st[top]!=u)
        {
            co[st[top]]=col;
            --top;
        }
        --top;
    }
}
int  in [N];
int main ()
{
    int n=read();
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
        {
            int x=read();
            if(x==1)add_edge(i,j);
        }
    for(int i=1;i<=n;i++)
        if(!dfn[i])tarjan(i);
    for(int i=1;i<=tot;i++)
    {
        int u=co[e[i].x],v=co[e[i].y];
        if(u!=v)
            in[v]++;
    }
    int ans=0;
    for(int i=1;i<=col;i++)
        if(!in[i])ans++;
    printf("%d",ans);
}

 

Guess you like

Origin www.cnblogs.com/fangbozhen/p/11728447.html