Hdu--3594(仙人掌图)
2014-11-01 18:56:51、
思路:判断仙人掌图,具体看:https://files.cnblogs.com/ambition/cactus_solution.pdf
主要是判断3个性质:(1)没有横向边(2)没有桥(3)每个点u及其子节点v中返回u的祖先节点的边数和<2
1 /************************************************************************* 2 > File Name: 3594.cpp 3 > Author: Nature 4 > Mail: 564374850@qq.com 5 > Created Time: Sat 01 Nov 2014 05:33:01 PM CST 6 ************************************************************************/ 7 8 #include <cstdio> 9 #include <cstring> 10 #include <cstdlib> 11 #include <cmath> 12 #include <vector> 13 #include <map> 14 #include <set> 15 #include <stack> 16 #include <queue> 17 #include <iostream> 18 #include <algorithm> 19 using namespace std; 20 #define lp (p << 1) 21 #define rp (p << 1|1) 22 #define getmid(l,r) (l + (r - l) / 2) 23 #define MP(a,b) make_pair(a,b) 24 typedef long long ll; 25 const int INF = 1 << 30; 26 const int maxn = 20010; 27 28 int T,n; 29 int first[maxn],next[50010],ver[50010],ecnt; 30 int dfn[maxn],low[maxn],vis[maxn],tot; 31 32 void Init(){ 33 memset(first,-1,sizeof(first)); 34 ecnt = 0; 35 } 36 37 void Add_edge(int u,int v){ 38 next[++ecnt] = first[u]; 39 ver[ecnt] = v; 40 first[u] = ecnt; 41 } 42 43 bool Dfs(int p){ 44 dfn[p] = low[p] = ++tot; 45 vis[p] = 1; 46 int ac = 0,bc = 0; 47 for(int i = first[p]; i != -1; i = next[i]){ 48 int v = ver[i]; 49 if(vis[v] == 2) return false; 50 if(!dfn[v]){ 51 if(!Dfs(v)) return false; 52 if(low[v] > dfn[p]) return false; 53 if(low[v] < dfn[p]) ++ac; 54 low[p] = min(low[p],low[v]); 55 } 56 else if(dfn[v] < dfn[p]){ 57 low[p] = min(low[p],dfn[v]); 58 ++bc; 59 } 60 } 61 vis[p] = 2; 62 if(ac + bc >= 2) return false; 63 return true; 64 } 65 66 bool Tarjan(){ 67 memset(dfn,0,sizeof(dfn)); 68 memset(low,0,sizeof(low)); 69 memset(vis,0,sizeof(vis)); 70 tot = 0; 71 for(int i = 1; i <= n; ++i) if(!dfn[i]) 72 if(!Dfs(i)) return false; 73 return true; 74 } 75 76 int main(){ 77 int a,b; 78 scanf("%d",&T); 79 while(T--){ 80 Init(); 81 scanf("%d",&n); 82 while(scanf("%d%d",&a,&b) != EOF && (a || b)){ 83 Add_edge(a,b); 84 } 85 if(Tarjan()) printf("YES\n"); 86 else printf("NO\n"); 87 } 88 return 0; 89 }