两题都是水题,1236第一问求缩点后入度为0的点数,第二问即至少添加多少条边使全图强连通,属于经典做法,具体可以看白书
POJ2186即求缩点后出度为0的那个唯一的点所包含的点数(即SCC里有多少点)
//poj1236
#include<iostream>
#include<cstdio>
#include<string.h>
#define maxn 6000
int now=0,next[maxn],head[maxn],point[maxn],num=0,dfn[maxn],low[maxn],instack[maxn];
int stack[maxn],belong[maxn],top,count,in[maxn],out[maxn];
int min(int a,int b)
{
    if (a<b)return a;else return b;
}
int max(int a,int b)
{
    if (a>b)return a;else return b;
}
void add(int x,int y)
{
    next[++now]=head[x];
    head[x]=now;
    point[now]=y;
}
void tarjan(int k)
{
    int u;
    instack[k]=true;
    dfn[k]=low[k]=++num;stack[++top]=k;
    for(int i=head[k];i!=0;i=next[i])
    {
        u=point[i];
        if (dfn[u]==0)
        {
            tarjan(u);
            low[k]=min(low[k],low[u]);
        }
        else if (instack[u])low[k]=min(dfn[u],low[k]);
    }
    if (low[k]==dfn[k])
    {
        ++count;
        do
        {
            u=stack[top--];
            instack[u]=false;
            belong[u]=count;
        }while(u!=k);
    }
}
int main()
{
    int n,t;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)while (1)
    {
        scanf("%d",&t);
        if (t==0)break;
        else add(i,t);
    }
    for(int i=1;i<=n;i++)if (dfn[i]==0)tarjan(i);
    for(int i=1;i<=n;i++)
    {
        for(int j=head[i];j!=0;j=next[j])
        {
            int u=point[j];
            if (belong[i]!=belong[u])
            {
                out[belong[i]]++;
                in[belong[u]]++;
            }
        }
    }
    int countout=0,countin=0;
    for(int i=1;i<=count;i++)
    {
        if (out[i]==0)countout++;
        if (in[i]==0)countin++;
    }
    if (count==1)printf("1\n0\n");
    else printf("%d\n%d\n",countin,max(countout,countin));
    return 0;
}
 

//poj2186

#include<iostream>

#include<cstdio>

#include<string.h>

#define maxn 50010

using namespace std;

intnow=0,next[maxn],head[maxn],point[maxn],num=0,dfn[maxn],low[maxn],instack[maxn];

int stack[maxn],belong[maxn],top,count,out[maxn],p[maxn];

void add(int x,int y)

{

next[++now]=head[x];

head[x]=now;

point[now]=y;

}

void tarjan(int k)

{

instack[k]=1;

stack[++top]=k;

low[k]=dfn[k]=++num;

int u;

for(inti=head[k];i!=0;i=next[i])

{

u=point[i];

if (dfn[u]==0)

{

tarjan(u);

low[k]=min(low[u],low[k]);

}

else if (instack[u]==1)low[k]=min(dfn[u],low[k]);

}

if (low[k]==dfn[k])

{

++count;

do

{

u=stack[top--];

instack[u]=0;

belong[u]=count;

p[count]++;

}while(u!=k);

}

}

int main()

{

int n,m,x,y,ans=0;

scanf("%d%d",&n,&m);

for(int i=1;i<=m;i++)

{

scanf("%d%d",&x,&y);

add(x,y);

}

for(int i=1;i<=n;i++)

if (dfn[i]==0)tarjan(i);

for(int i=1;i<=n;i++)

{

for(int j=head[i];j!=0;j=next[j])

{

int u=point[j];

if(belong[i]!=belong[u])out[belong[i]]++;

}

}

int flag=0;

for(inti=1;i<=count;i++)

{

if (ans!=0 &&out[i]==0){flag=1;break;}

if(out[i]==0)ans=p[i];

}

if(flag==0)printf("%d\n",ans);else printf("0\n");

return 0;

}

最近切的两题SCC的tarjan POJ1236 POJ2186的更多相关文章

  1. MT【249】离心率两题

    椭圆$\dfrac{x^2}{a^2}+\dfrac{y^2}{b^2}=1,(a>b>0)$的一个焦点为$F$,过$F$的直线交椭圆于$A,B$两点,$M$是点$A$关于原点的对称点.若 ...

  2. 清橙A1206.小Z的袜子 && CF 86D(莫队两题)

    清橙A1206.小Z的袜子 && CF 86D(莫队两题) 在网上看了一些别人写的关于莫队算法的介绍,我认为,莫队与其说是一种算法,不如说是一种思想,他通过先分块再排序来优化离线查询问 ...

  3. 2-SAT两题

    看了大白书,学习了一下two-sat,很有意思的算法.题目就是大白书上的两题. 仅仅放一下代码作为以后的模板参考. #include <stdio.h> #include <algo ...

  4. 「刷题笔记」Tarjan

    贴一个讲得非常详细的\(tarjan\)入门教程 信息传递 讲个笑话:我之前用并查集求最小环过的这题,然后看见题目上有个\(tarjan\)标签 留下了深刻的印象:\(tarjan\)就是并查集求最小 ...

  5. 类似区间计数的种类并查集两题--HDU 3038 & POJ 1733

    1.POJ 1733 Parity game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5744   Accepted: ...

  6. Codeforces Round #197 (Div. 2) C,D两题

    开了个小号做,C题一开始看错范围,D题看了半小时才看懂,居然也升到了div1,囧. C - Xenia and Weights 给出一串字符串,第i位如果是1的话,表示有重量为i的砝码,如果有该种砝码 ...

  7. Educational Codeforces Round 58 (Rated for Div. 2) (前两题题解)

    感慨 这次比较昏迷最近算法有点飘,都在玩pygame...做出第一题让人hack了,第二题还昏迷想错了 A Minimum Integer(数学) 水题,上来就能做出来但是让人hack成了tle,所以 ...

  8. noip2016 小结(ac两题+学习总结)

    NOIP2016考试小结 DAY 1 T1 题目描述 小南有一套可爱的玩具小人, 它们各有不同的职业. 有一天, 这些玩具小人把小南的眼镜藏了起来. 小南发现玩具小人们围成了一个圈,它们有的面朝圈内, ...

  9. <每日一题> Day5:简单递推两题

    原题链接 参考代码: #include <iostream> using namespace std; typedef long long ll; + ; ll dp[maxn]; int ...

随机推荐

  1. 分享几个自己喜欢的前端UI框架

    http://www.layui.com/ http://element-cn.eleme.io/#/zh-CN/component/installation

  2. Springboot 1.X 在Weblogic 中的发布

    springboot在tomcat中的兼容性很好,但是如果要把Springboot项目发布在weblogic,尤其是老版本的Weblogic就会出现各种问题.经过本人的不懈努力及查询资料,终于将Spr ...

  3. SSM-WebMVC(三)

    SSM-WebMVC(三) 一.Annotated Controllers ​ 应用程序控制器 handlerMethod(处理方法) ㈠方法入参 ​ (springmvc针对于在controller ...

  4. Macbook air 上打开cocoscreator出错

    Error: EROFS: read-only file system, open '/Volumes/Cocos Creator/CocosCreator.app/Contents/Resource ...

  5. javascript动态添加、修改、删除对象的属性与方法

    在其他语言中,对象一旦生成,就不可更改了,要为一个对象添加修改成员必须要在对应的类中修改,并重新实例化,而且程序必须经过重新编译.JavaScript 中却非如此,它提供了灵活的机制来修改对象的行为, ...

  6. python * urllib_urlopen( )

    python * urllib_urlopen( ) Python urllib 库提供了一个从指定的 URL 地址获取网页数据,然后对其进行分析处理,获取想要的数据. 一.urllib模块urlop ...

  7. Python 语言规范

    Python 语言规范 pychecker  对你的代码运行pychecker 定义: pychecker 是一个在Python 源代码中查找bug 的工具. 对于C 和C++这样的不那 么动态的( ...

  8. 事件捕获 & 事件冒泡

    <body> <div id="div1"> <div id="div2"> <div id="div3&q ...

  9. js文件下载代码 import downloadjs from 'downloadjs'

    function logDownload(contribution_id) { setTimeout(function () { $.ajax({ url: "/ajax/Wallpaper ...

  10. Java集合(六)--ArrayList、LinkedList和Vector对比

    在前两篇博客,学习了ArrayList和LinkedList的源码,地址在这: Java集合(五)--LinkedList源码解读 Java集合(四)--基于JDK1.8的ArrayList源码解读 ...