poj2762 Going from u to v or from v to u?
来源:互联网 发布:mac finder 拷贝路径 编辑:程序博客网 时间:2024/06/01 23:00
对于任意两个点x,y要存在一条x->y或者y->x的路径。显然是缩点然后判断是不是一条链。
const int maxn = 1010;int n, m;vector<vector<int> > G;int dfn[maxn], low[maxn], depth;int belong[maxn], block;int in[maxn], mark[maxn];stack<int> st;int head[maxn], pnt[maxn<<3], nxt[maxn<<3], ecnt;void addedge(int u,int v) { pnt[ecnt] = v, nxt[ecnt] = head[u], head[u] = ecnt++;}void tarjan(int u) { dfn[u] = low[u] = ++depth; st.push(u), mark[u] = 1; int Size = G[u].size(); for (int i = 0;i < Size;++i) { int v = G[u][i]; if (dfn[v] == 0) { tarjan(v); low[u] = min(low[v], low[u]); }else if (mark[v]) low[u] = min(low[u], dfn[v]); } if (dfn[u] == low[u]) { block++; while(true) { int x = st.top(); st.pop(); belong[x] = block; mark[x] = 0; if (x == u) break; } } return ;}int ans;void dfs(int u) { ans++; mark[u] = 1; for (int i = head[u]; ~i;i = nxt[i]) { int v = pnt[i]; if (!mark[v]) { // ans++; dfs(v); return ; } }}int main(int argc, const char * argv[]){ // freopen("in.txt","r",stdin); // freopen("out.txt","w",stdout); // clock_t _ = clock(); int t; cin >> t; while(t--) { scanf("%d%d", &n, &m); G.clear();G.resize(n + 2); int u, v; while(m--) { scanf("%d%d", &u, &v); G[u].push_back(v); } memset(dfn, 0,sizeof dfn), depth = block = 0; memset(mark, 0,sizeof mark); for (int i = 1;i <= n;++i) if (dfn[i] == 0) tarjan(i); memset(head, -1,sizeof head), ecnt = 0; memset(in, 0,sizeof in); for (int i = 1;i <= n;++i) { for (int j = 0;j < G[i].size();++j) { u = i, v = G[i][j]; if (belong[u] != belong[v]) { addedge(belong[u], belong[v]); in[belong[v]]++; } } } ans = 0; for (int i = 1;i <= block;++i) if (in[i] == 0) {dfs(i);break;} if (ans == block) puts("Yes"); else puts("No"); } // printf("\nTime cost: %.2fs\n", 1.0 * (clock() - _) / CLOCKS_PER_SEC); return 0;}
0 0
- POJ2762 Going from u to v or from v to u?
- poj2762 Going from u to v or from v to u? --- 缩点+拓扑
- POJ2762——Going from u to v or from v to u?
- poj2762 Going from u to v or from v to u?(强联通+拓扑排序)
- POJ2762-Going from u to v or from v to u?(强连通缩点+DP)
- poj2762 Going from u to v or from v to u?
- POJ2762 Going from u to v or from v to u? 强连通+缩点
- POJ2762 Going from u to v or from v to u?(强联通分量)
- poj2762 Going from u to v or from v to u? 强连通分量 + 拓扑排序
- POJ2762 Going from u to v or from v to u
- poj2762 Going from u to v or from v to u?
- 【POJ2762】Going from u to v or from v to u?(tarjan+缩点+拓扑排序)
- poj2762 Going from u to v or from v to u?
- POJ2762 Going from u to v or from v to u?
- [练习][poj2762]tarjan缩点 Going from u to v or from v to u?
- POJ2762 Going from u to v or from v to u?(强连通分量缩点+拓扑排序)
- POJ2762 Going from u to v or from v to u?(缩点+树最长路)
- poj2762 Going from u to v or from v to u? 有向图 强连通分量 拓扑排序
- 不用安装Oracle客户端,远程连接Oracle数据库
- Javascript Object, Set, Map总结
- Android禁止状态栏下拉 4.4
- Android调试串口无法连接串口设备的问题及解决办法
- C++类的静态成员详细讲解
- poj2762 Going from u to v or from v to u?
- golang带cookie post get请求 例子
- 线性时间排序 计数排序(Counting Sort)
- webpy save session to redis
- 数据处理---Spring Batch之基础
- sql日期格式化
- Android四大组件之Service精通
- RT-thread内核之互斥量
- 跟我学Android之八 ActionBar与菜单