题目大意是:在一个牧群中,有N个奶牛,给定M对关系(A,B)表示A仰慕B,而且仰慕关系有传递性,问被所有奶牛(除了自己)仰慕的奶牛个数
因为仰慕关系具有传递性,因此在一个强连通分量中,每个奶牛都被分量中的其他奶牛膜拜,而且也膜拜着分量中的其他奶牛,这种互相膜拜的场景在现实生活中也是经常存在的,因此,本题可以将强连通分量缩点,并构造新图,最后做一次扫描,统计出度为0的点的个数,如果正好为1,表示这个强连通分量(可能是一个点,也可能是多个点)中的奶牛都符合条件,输出其中的个数即可,如果不唯一,显然就没有奶牛符合被所有奶牛膜拜的条件了。
本题我使用了Kosaraju算法,网上已经有很好的解释了, http://hi.baidu.com/edwardmj/blog/item/70ca38d1035810cca9ec9af9.html
北大的那本图论书上关于这个算法的说明显然是错的,不应该用dfn数组,而应该是顶点的出栈顺序,这个顺序有点类似于后根遍历。
一.Kosrarju
/*
ID: sdj22251
PROG: subset
LANG: C++
*/
#include <iostream>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <cmath>
#include <ctime>
#define LOCA
#define MAXN 10005
#define INF 100000000
#define eps 1e-7
using namespace std;
struct Edge
{int v, next;
}edge[10 * MAXN], revedge[10 * MAXN];
int head[MAXN], revhead[MAXN], e, visited[MAXN];
int order[MAXN], cnt, id[MAXN], tmp;
int uu[5 * MAXN], vv[5 * MAXN], out[MAXN], pos;
int n, m;
void init()
{e = 0;memset(head, -1, sizeof(head));memset(revhead, -1, sizeof(revhead));memset(out, 0 , sizeof(out));
}
void insert(const int &x, const int &y)
{edge[e].v = y;edge[e].next = head[x];head[x] = e;revedge[e].v = x;revedge[e].next = revhead[y];revhead[y] = e;e++;
}
void readdata()
{for(int i = 0; i < m; i++){scanf("%d%d", &uu[i], &vv[i]);insert(uu[i], vv[i]);}
}
void dfs(int u)
{visited[u] = 1;for(int i = head[u]; i != -1; i = edge[i].next){int v = edge[i].v;if(!visited[v])dfs(v);}order[cnt++] = u;
}
void dfs_rev(int u)
{visited[u] = 1;id[u] = cnt;for(int i = revhead[u]; i != -1; i = revedge[i].next){int v = revedge[i].v;if(!visited[v])dfs_rev(v);}
}
void Kosaraju()
{init();readdata();memset(visited, 0, sizeof(visited));cnt = 0;for(int i = 1; i <= n; i++){if(!visited[i])dfs(i);}memset(visited, 0, sizeof(visited));cnt = 0;for(int i = n - 1; i >= 0; i--){if(!visited[order[i]]){cnt++;dfs_rev(order[i]);}}for(int i = 0; i < m; i++){int u = id[uu[i]];int v = id[vv[i]];if(u != v) out[u]++;}tmp = 0;for(int i = 1; i <= cnt; i++){if(out[i] == 0){pos = i;tmp++;}}
}
int main()
{while(scanf("%d%d", &n, &m) != EOF){Kosaraju();int ans = 0;if(tmp != 1)printf("0\n");else{for(int i = 1; i <= n; i++){if(id[i] == pos) ans++;}printf("%d\n", ans);}}return 0;
}
然后又用tarjan
#include <iostream>
#include <map>
#include <cstdio>
#include <stack>
#include <cstring>
#include <algorithm>
#define MAXN 10005
#define MAXM 100005
#define INF 1000000000
using namespace std;
int n, m;
int scc;//强连通分量
int index;//每个节点的dfs访问次序编号
int dfn[MAXN];//标记结点i的dfs访问次序
int low[MAXN];//记录节点u或u的子树中的所有节点的最小标号
int fa[MAXN];//属于哪个分支
bool instack[MAXN];//是否在栈中
int in[MAXN], head[MAXN], e;
int out[MAXN];//出度
stack <int>s;
struct Edge
{int v, next;
}edge[MAXM];
void insert(int x, int y)
{edge[e].v = y;edge[e].next = head[x];head[x] = e++;
}
void tarjan(int u)
{dfn[u] = low[u] = ++index;s.push(u);instack[u] = true;for (int j = head[u]; j != -1; j = edge[j].next){int v = edge[j].v;if(dfn[v] == 0)//未曾访问过{tarjan(v);low[u] = min(low[u], low[v]);}else if(instack[v])low[u] = min(low[u], dfn[v]);}if(dfn[u] == low[u]){scc++;while(1){int tmp = s.top();s.pop();instack[tmp] = 0;fa[tmp] = scc;if(tmp == u) break;}}
}
void init()
{scc = index = 0;memset(dfn, 0, sizeof(dfn));memset(instack, 0, sizeof(instack));e = 0;memset(head, -1, sizeof(head));memset(in, 0, sizeof(in));memset(out, 0, sizeof(out));}
void solve()
{for (int i = 1;i <= n; i++){if (!dfn[i])tarjan(i);}for (int i = 1;i <= n; i++){for(int j = head[i]; j != -1; j = edge[j].next){int u = fa[i];int v = fa[edge[j].v];if(u != v){out[u]++;in[v]++;}}}int cnt = 0;for(int i = 1; i <= scc; i++)if(out[i] == 0) cnt++;int ans = 0;for(int i = 1; i <= n; i++)if(out[fa[i]] == 0) ans++;if(cnt != 1) ans = 0;printf("%d\n", ans);
}int main()
{int x, y;while(scanf("%d%d", &n, &m) != EOF){init();while(m--){scanf("%d%d", &x, &y);insert(x, y);}solve();}return 0;
}