码迷,mamicode.com
首页 > 其他好文 > 详细

P2002 消息扩散[SCC缩点]

时间:2019-08-05 14:01:42      阅读:86      评论:0      收藏:0      [点我收藏+]

标签:struct   bool   需要   之间   ++i   for   while   map   turn   

题目描述

有n个城市,中间有单向道路连接,消息会沿着道路扩散,现在给出n个城市及其之间的道路,问至少需要在几个城市发布消息才能让这所有n个城市都得到消息。

输入格式

第一行两个整数n,m表示n个城市,m条单向道路。

以下m行,每行两个整数b,e表示有一条从b到e的道路,道路可以重复或存在自环。

输出格式

一行一个整数,表示至少要在几个城市中发布消息。

输入输出样例

输入 #1复制

输出 #1复制

说明/提示

【数据范围】

对于20%的数据,n≤200;

对于40%的数据,n≤2,000;

对于100%的数据,n≤100,000,m≤500,000.

【限制】

时间限制:1s,内存限制:256M

【注释】

样例中在4,5号城市中发布消息。

解析

这题比板子还简单。。。

板子好歹有个dp吧,这个判断个入度就完事了233。

参考代码

#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<ctime>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<set>
#include<map>
#define N 500010
using namespace std;
inline int read()
{
    int f=1,x=0;char c=getchar();
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
    return x*f;
}
struct rec{
    int next,ver;
}g[N],G[N];
int head[N],tot,headG[N],totG,idt,n,m;
int stack[N],top,dfn[N],low[N],cnt,ing[N],c[N];
bool ins[N];
inline void add(int x,int y)
{
    g[++tot].ver=y;
    g[tot].next=head[x],head[x]=tot;
}
inline void addG(int x,int y)
{
    G[++totG].ver=y;
    G[totG].next=headG[x],headG[x]=totG;
    ing[y]++;
}
inline void tarjan(int x)
{
    dfn[x]=low[x]=++cnt;
    stack[++top]=x,ins[x]=1;
    for(int i=head[x];i;i=g[i].next){
        int y=g[i].ver;
        if(!dfn[y]){
            tarjan(y);
            low[x]=min(low[x],low[y]);
        }
        else if(ins[y]) low[x]=min(dfn[y],low[x]);
    }
    if(dfn[x]==low[x]){
        idt++;int y;
        do{
            y=stack[top--];ins[y]=0;
            c[y]=idt;
        }while(x!=y);
    }
}
int main()
{
    n=read(),m=read();
    for(int i=1;i<=m;++i){
        int u,v;
        u=read(),v=read();
        add(u,v);
    }
    for(int i=1;i<=n;++i)
        if(!dfn[i]) tarjan(i);
    for(int x=1;x<=n;++x)
        for(int i=head[x];i;i=g[i].next){
            int y=g[i].ver;
            if(c[y]==c[x]) continue;
            addG(c[x],c[y]);
        }
    int ans=0;
    for(int i=1;i<=idt;++i)
        if(ing[i]==0) ans++;
    cout<<ans<<endl;
    return 0;
}

P2002 消息扩散[SCC缩点]

标签:struct   bool   需要   之间   ++i   for   while   map   turn   

原文地址:https://www.cnblogs.com/DarkValkyrie/p/11302469.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!