HDU 5413 拓扑排序+BITSET

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5413

题意:给定一张有向图,对于边e<u,v>,如果去掉它的话,u仍然可以到达v的话,那么这条边就是多余边。

解法:显然的一个思路是倒着做。先topo一遍,然后倒着来,这里需要用到bitset维护每个点可以和哪些点直接相连,利用bitset是为了不超内存,然后用bitset处理出关系之后就可以遍历寻找答案了。


#include 
using namespace std;
struct FastIO
{
    static const int S = 1310720;
    int wpos;
    char wbuf[S];
    FastIO() : wpos(0) {}
    inline int xchar()
    {
        static char buf[S];
        static int len = 0, pos = 0;
        if (pos == len)
            pos = 0, len = fread(buf, 1, S, stdin);
        if (pos == len) return -1;
        return buf[pos ++];
    }
    inline int xuint()
    {
        int c = xchar(), x = 0;
        while (c <= 32) c = xchar();
        for (; '0' <= c && c <= '9'; c = xchar()) x = x * 10 + c - '0';
        return x;
    }
    inline int xint()
    {
        int s = 1, c = xchar(), x = 0;
        while (c <= 32) c = xchar();
        if (c == '-') s = -1, c = xchar();
        for (; '0' <= c && c <= '9'; c = xchar()) x = x * 10 + c - '0';
        return x * s;
    }
    inline void xstring(char *s)
    {
        int c = xchar();
        while (c <= 32) c = xchar();
        for (; c > 32; c = xchar()) * s++ = c;
        *s = 0;
    }
    inline void wchar(int x)
    {
        if (wpos == S) fwrite(wbuf, 1, S, stdout), wpos = 0;
        wbuf[wpos ++] = x;
    }
    inline void wint(int x)
    {
        if (x < 0) wchar('-'), x = -x;
        char s[24];
        int n = 0;
        while (x || !n) s[n ++] = '0' + x % 10, x /= 10;
        while (n--) wchar(s[n]);
        wchar('\n');
    }
    inline void wstring(const char *s)
    {
        while (*s) wchar(*s++);
    }
    ~FastIO()
    {
        if (wpos) fwrite(wbuf, 1, wpos, stdout), wpos = 0;
    }
} io;
const int maxn = 2e4+2;
int n, m, top, in[maxn], rec[maxn];
bitset  dp[maxn];
int head[maxn], edgecnt;
struct edge{
    int u, v, next;
}E[100002];
struct node{
    int u, v;
}q[100002];
void add(int u, int v){
    E[edgecnt].u = u ,E[edgecnt].v = v, E[edgecnt].next = head[u], head[u] = edgecnt++;
}
void Toposort()
{
    top = 0;
    for(int i=1; i<=n; i++) if(in[i]==0) rec[top++]=i;
    for(int i=0; i=0; i--){
            int u = rec[i];
            for(int j=head[u]; ~j; j=E[j].next){
                int v = E[j].v;
                dp[u][v] = 1;
                dp[u]|=dp[v];
            }
        }
        for(int i=1; i<=m; i++){
            int u = q[i].u;
            int v = q[i].v;
            for(int j=head[u]; ~j; j=E[j].next){
                if(E[j].v!=v&&dp[E[j].v][v]){
                    ans++;
                    break;
                }
            }
        }
        io.wint(ans);
    }
    return 0;
}

你可能感兴趣的:(ACM/ICPC_HDOJ)