POJ 2762 — Going from u to v or from v to u? 强连通+拓扑
2015-05-03 00:06
495 查看
原题:http://poj.org/problem?id=2762
题意:给定n个点,m条有向边;
问是否满足图中任意两点单连通,即 u到v 或 v到u;
思路:首先用强连通缩点,然后直接判断是否为单链;
可以用拓扑的方式来判断,每次入度为0的点只能有一个;
题意:给定n个点,m条有向边;
问是否满足图中任意两点单连通,即 u到v 或 v到u;
思路:首先用强连通缩点,然后直接判断是否为单链;
可以用拓扑的方式来判断,每次入度为0的点只能有一个;
#include<stdio.h> #include<string.h> #include<vector> #include<iostream> #include<algorithm> using namespace std; const int N = 1100; int head ; bool vis ; int DFN , low , stack , belong ; int n, m, top, taj, time, edgenum; struct node { int from, to, nex; bool sign; }edge[6100]; void add(int u, int v) { edge[edgenum].from = u; edge[edgenum].to = v; edge[edgenum].sign = false; edge[edgenum].nex = head[u]; head[u] = edgenum++; } vector<int>bcc ; void tarjan(int u, int fa) { DFN[u] = low[u] = time++; vis[u] = 1; stack[top++] = u; for(int i = head[u];i!=-1;i = edge[i].nex) { int v = edge[i].to; if(DFN[v] == -1) { tarjan(v, u); low[u] = min(low[u], low[v]); if(DFN[u]<low[v]) edge[i].sign = 1; } else if(vis[v]) low[u] = min(low[u], DFN[v]); } if(low[u] == DFN[u]) { taj ++ ; bcc[taj].clear(); while(1) { int now = stack[--top]; vis[now] = 0; belong[now] = taj; bcc[taj].push_back(now); if(now == u) break; } } } vector<int>G ; int du ; void suodian() { memset(du, 0, sizeof(du)); for(int i = 1;i<=taj;i++) G[i].clear(); for(int i = 0;i<edgenum;i++) { int u = belong[edge[i].from]; int v = belong[edge[i].to]; if(u!=v) { G[u].push_back(v); du[v]++; } } } void tarjan_init(int all) { memset(DFN, -1, sizeof(DFN)); memset(low, -1, sizeof(low)); memset(vis, 0, sizeof(vis)); time = top = taj = 0; for(int i = 1;i<=all;i++) { if(DFN[i] == -1) tarjan(i, i); } } int main() { int cas; scanf("%d", &cas); while(cas--) { scanf("%d%d", &n, &m); memset(head, -1, sizeof(head)); edgenum = 0; while(m--) { int u, v; scanf("%d%d", &u, &v); add(u, v); } tarjan_init(n); suodian(); int flag = 0; int ans ; memset(ans, 0, sizeof(ans)); for(int t = 0;t<taj;t++) { int con = 0; for(int i = 1;i<=taj;i++) { if(du[i] == 0) { du[i] = -1; con++; for(int j = 0;j<G[i].size();j++) du[G[i][j]]--; } } if(con>1) { flag = 1; break; } } if(flag) printf("No\n"); else printf("Yes\n"); } return 0; }
相关文章推荐
- poj 2762 Going from u to v or from v to u?(强连通分量)
- POJ 2762 Going from u to v or from v to u? (判断单连通)
- POJ 2762 Going from u to v or from v to u?
- poj 2762 Going from u to v or from v to u?
- POJ2762——Going from u to v or from v to u?
- POJ 2762 Going from u to v or from v to u? 图的单连通性 tarjan or kosaraju
- POJ 2762 Going from u to v or from v to u?
- POJ 2762 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缩点+拓扑排序topsort
- POJ 2762 Going from u to v or from v to u?
- poj 2762 Going from u to v or from v to u? 强连通最长链
- poj 2762 Going from u to v or from v to u?
- [ tarjan + dfs ] poj 2762 Going from u to v or from v to u?
- 【连通图|单连通】POJ-2762 Going from u to v or from v to u?
- POJ-2762 Going from u to v or from v to u?
- poj2762 Going from u to v or from v to u? 有向图 强连通分量 拓扑排序
- poj 2762 Going from u to v or from v to u(targan缩点+拓扑排序)
- poj 2762 Going from u to v or from v to u?(SCC缩点+拓扑排序)
- poj 2762 Going from u to v or from v to u? 强连通最长链