zoukankan      html  css  js  c++  java
  • P2002 消息扩散[SCC缩点]

    题目描述

    有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;
    }
    
  • 相关阅读:
    VS.NET2005中的源代码管理
    IE6升级后需要激活ActiveX控件的解决办法
    SQL Server的数据库开发工具
    今天更新了ActiveSync4.2
    永远等你先挂电话
    这回软设考试通过了!
    在Windows2003中FSO组件不能使用的问题
    七天的假期好长哟!
    发现博客园的一个Bug 存为草稿后就找不到了
    MySQL服务不能启动的解决方法
  • 原文地址:https://www.cnblogs.com/DarkValkyrie/p/11302469.html
Copyright © 2011-2022 走看看