HDU 5413 CRB and Roads bitset (看题解)

HDU 5413

居然不知道acyclic是没有环的意思GG。

如果是拓扑图的话, 用bitset暴力更新就完事了。

#include<bits/stdc++.h>
#define LL long long
#define LD long double
#define ull unsigned long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ALL(x) (x).begin(), (x).end()
#define fio ios::sync_with_stdio(false); cin.tie(0);

using namespace std;

const int N = 2e4 + 2;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const double PI = acos(-1);

template<class T, class S> inline void add(T &a, S b) {a += b; if(a >= mod) a -= mod;}
template<class T, class S> inline void sub(T &a, S b) {a -= b; if(a < 0) a += mod;}
template<class T, class S> inline bool chkmax(T &a, S b) {return a < b ? a = b, true : false;}
template<class T, class S> inline bool chkmin(T &a, S b) {return a > b ? a = b, true : false;}

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int n, m, ans, deg[N];
vector<int> G[N];
bitset<20000> bit[20000];
bool vis[N];

void dfs(int u) {
    if(vis[u]) return;
    vis[u] = true;
    for(auto &v : G[u]) {
        dfs(v);
        bit[u] |= bit[v];
    }
    for(auto &v : G[u]) {
        if(bit[u][v]) {
            ans++;
        }
        else {
            bit[u][v] = 1;
        }
    }
}

int main() {

    int T; scanf("%d", &T);
    while(T--) {
        scanf("%d%d", &n, &m);
        for(int i = 0; i < n; i++) {
            G[i].clear();
            bit[i].reset();
            deg[i] = 0;
            vis[i] = 0;
        }
        for(int i = 1; i <= m; i++) {
            int u, v;
            scanf("%d%d", &u, &v);
            u--; v--;
            G[u].push_back(v);
            deg[v]++;
        }

        ans = 0;

        for(int i = 0; i < n; i++) {
            if(!deg[i]) {
                dfs(i);
            }
        }

        printf("%d\n", ans);
    }
    return 0;
}

/*
*/

 

posted @ 2019-07-09 20:51  NotNight  阅读(116)  评论(0编辑  收藏  举报