• poj2186


    题意:给出一个有向图,求一共有多少个点,满足这样的条件:所有其它的点都可以到达这个点。

    分析:强连通分支+缩点,然后统计每个强连通分支的出度,如果只有一个为0,则输出其内部点的个数,如果有多个为0,说明没有答案。

    View Code
    #include <iostream>
    #include
    <cstdlib>
    #include
    <cstring>
    #include
    <cstdio>
    usingnamespace std;

    #define maxn 10005
    #define maxm 50005

    struct Edge
    {
    int v, next;
    } edge[maxm], opedge[maxm];
    int n, m, head[maxn], ophead[maxn], ncount, nowtime, pos[maxn *2], sig[maxn], signnum, in[maxn];
    int pointnum[maxn];
    bool flag[maxn];
    void addedge(int a, int b)
    {
    edge[ncount].v
    = b;
    edge[ncount].next
    = head[a];
    head[a]
    = ncount;
    opedge[ncount].v
    = a;
    opedge[ncount].next
    = ophead[b];
    ophead[b]
    = ncount;
    ncount
    ++;
    }
    void input()
    {
    scanf(
    "%d%d", &n, &m);
    for (int i =0; i < m; i++)
    {
    int a, b;
    scanf(
    "%d%d", &a, &b);
    a
    --;
    b
    --;
    addedge(a, b);
    }
    }
    void dfs(int a)
    {
    flag[a]
    =true;
    pos[nowtime]
    = a;
    nowtime
    ++;
    for (int i = head[a]; i !=-1; i = edge[i].next)
    if (!flag[edge[i].v])
    dfs(edge[i].v);
    pos[nowtime]
    = a;
    nowtime
    ++;
    }
    void rdfs(int a)
    {
    flag[a]
    =true;
    sig[a]
    = signnum;
    pointnum[sig[a]]
    ++;
    for (int i = ophead[a]; i !=-1; i = opedge[i].next)
    if (!flag[opedge[i].v])
    rdfs(opedge[i].v);
    }

    int main()
    {
    //freopen("t.txt", "r", stdin);
    ncount =0;
    memset(head,
    -1, sizeof(head));
    memset(ophead,
    -1, sizeof(ophead));
    memset(flag,
    0, sizeof(flag));
    input();
    nowtime
    =1;
    for (int i =0; i < n; i++)
    if (!flag[i])
    dfs(i);
    memset(flag,
    0, sizeof(flag));
    memset(pointnum,
    0, sizeof(pointnum));
    signnum
    =0;
    for (int i =2* n; i >0; i--)
    if (!flag[pos[i]])
    {
    rdfs(pos[i]);
    signnum
    ++;
    }
    memset(
    in, 0, sizeof(in));
    ncount
    =0;
    for (int i =0; i < n; i++)
    for (int j = head[i]; j !=-1; j = edge[j].next)
    if (sig[i] != sig[edge[j].v])
    in[sig[i]]++;
    int ans =-1;
    bool ok =true;
    for (int i =0; i < signnum; i++)
    if (in[i] ==0)
    {
    if (ans !=-1)
    {
    ok
    =false;
    break;
    }
    ans
    = i;
    }
    if (ok)
    printf(
    "%d\n", pointnum[ans]);
    else
    printf(
    "0\n");
    return0;
    }
  • 相关阅读:
    element-ui upload 上传图片之前压缩
    字符串截取substring放法传参不同返回不同
    vue中对于图片是否正常加载的思考
    前端图片合成并下载
    vue中图相对路径引用本地图片
    js计算精度
    vue-cli定义全局过滤器
    js加减乘除运算丢失精度 前端计算金额带小数点精度丢失问题
    鼠标样式大全
    js两小时倒计时,剩余时间倒计时,倒计时
  • 原文地址:https://www.cnblogs.com/rainydays/p/2072950.html
Copyright © 2020-2023  润新知