Description
Every cow's dream is to become the most popular cow in the herd. In a herd of N (1 <= N <= 10,000) cows, you are given up to M (1 <= M <= 50,000) ordered pairs of the form (A, B) that tell you that cow A thinks that cow B is popular. Since popularity is transitive, if A thinks B is popular and B thinks C is popular, then A will also think that C is
popular, even if this is not explicitly specified by an ordered pair in the input. Your task is to compute the number of cows that are considered popular by every other cow.
popular, even if this is not explicitly specified by an ordered pair in the input. Your task is to compute the number of cows that are considered popular by every other cow.
Input
* Line 1: Two space-separated integers, N and M
* Lines 2..1+M: Two space-separated numbers A and B, meaning that A thinks B is popular.
* Lines 2..1+M: Two space-separated numbers A and B, meaning that A thinks B is popular.
Output
* Line 1: A single integer that is the number of cows who are considered popular by every other cow.
Sample Input
3 3 1 2 2 1 2 3
Sample Output
1
Hint
Cow 3 is the only cow of high popularity.
『题解』
建立有向图,若牛a喜欢牛b,则图中节点a可达节点b,i.e.存在从a到b的一条有向边。于是问题就转化为,在有向图中求出有多少点是被所有其它点可达的。
事实上,在有向无环图DAG中,从任何点出发开始走,必定终止于一个出度为0的点。因此,如果有唯一的出度为0的点p,则一定由任意点出发可达p。
对于一般的有向图,若有向图中存在强连通分量,则在强连通分量中,每两点之间都相互可达。于是可以将强连通分量缩成一个点,重新建立图结构,就形成DAG。为便于去重,新图邻接表可采用数据结构vector<set<int>>;或者就像本代码示例中的方法,为强连通分量的点染色即可。正如上文所述,若DAG中存在唯一的出度为0的点,则该点代表的强连通分量中的所有点都满足条件,即被除自身以外所有点可达。若DAG中,出度为0的点不止一个,则原题无解,答案为0。
『C++』
1 #include <iostream> 2 #include <algorithm> 3 #include <cstring> 4 using namespace std; 5 #define maxn 10005 6 7 struct Edge 8 { 9 int next; 10 int to; 11 }e[maxn*5]; 12 13 int ent, cnt, tnt, top; 14 int head[maxn], SCC[maxn], out[maxn], dfn[maxn], low[maxn]; 15 int Stack[maxn]; 16 bool inStack[maxn]; 17 18 void Build(int u, int v) 19 { 20 e[ent].to = v; 21 e[ent].next = head[u]; 22 head[u] = ent++; 23 } 24 25 void Tarjan(int u) 26 { 27 int v; 28 29 dfn[u] = low[u] = ++tnt; 30 Stack[top++] = u; 31 inStack[u] = 1; 32 33 for (int i = head[u]; i != -1; i = e[i].next) { 34 v = e[i].to; 35 if (!dfn[v]) { 36 Tarjan(v); 37 low[u] = min(low[u], low[v]); 38 } 39 else if (inStack[v]) 40 low[u] = min(low[u], dfn[v]); 41 } 42 if (dfn[u] == low[u]) { 43 do { 44 v = Stack[--top]; 45 inStack[v] = 0; 46 SCC[v] = cnt; 47 } while (v != u); 48 cnt++; 49 } 50 } 51 52 int main() 53 { 54 int N, M, A, B; 55 int ans = 0, num = 0, index = -1; 56 memset(head, -1, sizeof(head)); 57 memset(inStack, 0, sizeof(inStack)); 58 memset(SCC, 0, sizeof(SCC)); 59 memset(Stack, 0, sizeof(Stack)); 60 memset(dfn, 0, sizeof(dfn)); 61 memset(low, 0, sizeof(low)); 62 ent = cnt = tnt = top = 0; 63 64 cin >> N >> M; 65 66 for (int i = 0; i < M; i++) { 67 cin >> A >> B; 68 Build(A, B); 69 } 70 71 for (int u = 1; u <= N; u++) 72 if (!dfn[u]) Tarjan(u); 73 74 for (int u = 1; u <= N; u++) { 75 for (int i = head[u]; i != -1; i = e[i].next) { 76 int v = e[i].to; 77 if (SCC[u] != SCC[v])out[SCC[u]]++; 78 } 79 } 80 81 for (int i = 0; i < cnt; i++) 82 if (out[i] == 0) { 83 num++; 84 index = i; 85 } 86 if (num == 1) 87 for (int u = 1; u <= N; u++) 88 if (SCC[u] == index)ans++; 89 printf("%d\n", ans); 90 91 //system("pause"); 92 return 0; 93 }