HDU 5413 拓扑排序+BITSET
来源:互联网 发布:怎么经营淘宝网店 编辑:程序博客网 时间:2024/06/05 15:40
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5413
题意:给定一张有向图,对于边
解法:显然的一个思路是倒着做。先topo一遍,然后倒着来,这里需要用到bitset维护每个点可以和哪些点直接相连,利用bitset是为了不超内存,然后用bitset处理出关系之后就可以遍历寻找答案了。
#include <bits/stdc++.h>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 <maxn> 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<top; i++){ int u = rec[i]; for(int j=head[u]; ~j; j=E[j].next){ int v = E[j].v; if(--in[v]==0) rec[top++] = v; } }}int main(){ int T; T = io.xint(); while(T--) { n = io.xint(); m = io.xint(); edgecnt=0; for(int i=1; i<=n; i++){ head[i]=-1; in[i]=0; } for(int i=1; i<=m; i++){ int u, v; u = io.xint(); v = io.xint(); q[i].u = u, q[i].v = v; add(u, v); in[v]++; } Toposort(); for(int i=0; i<=n; i++){ dp[i].reset(); dp[i].set(i); } int ans = 0; for(int i=top-1; 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;}
阅读全文
0 0
- HDU 5413 拓扑排序+BITSET
- [HDU 5413] CRB and Roads (拓扑排序+bitset卡时)
- 【bzoj4484】【JSOI2015】【最小表示】【拓扑排序+bitset】
- UVALive 7272 Promotions【拓扑排序】【bitset】
- UVALive 7272 Promotions【拓扑排序】【bitset】
- HDU 1272 拓扑排序
- HDU 3342 拓扑排序
- [HDU-1285] 拓扑排序
- hdu 1285拓扑排序
- hdu 1285 拓扑排序
- hdu 3342 拓扑排序
- HDU 3342 拓扑排序
- HDU 1285 拓扑排序
- HDU-2647拓扑排序
- hdu 3342 拓扑排序
- hdu 2647 拓扑排序
- hdu 1285 拓扑排序
- hdu Reward(拓扑排序)
- javascript 实现1加到100
- Android UI性能优化(渲染)
- API学习HashMap
- C到C++注释的转换
- LeetCode--Search in Rotated Sorted Array
- HDU 5413 拓扑排序+BITSET
- bzoj 4956: Secret Chamber at Mount Rushmore
- Android apk动态加载机制的研究
- 2017工作流触发器实例
- 【算法题】数组分段
- 折半查找(二分查找)
- 【思维-二进制思想】nyoj-备用 2350--彩排
- SSM框架整合
- springboot整合spring data jpa