Transmission DoorPopular Cows
| Time Limit: 2000MS |
|
Memory Limit: 65536K |
| Total Submissions: 31808 |
|
Accepted: 12921 |
Descriptionevery cow's dream is to become the very popular cow in the herd. In a herd of n (1 <= n <=) cows, you is given up to M (1 <= m <= 50,000) ordered pairs of the form (A, b) That's cow A thinks that cow B is popular. Since popularity is transitive, if a thinks B was popular and b thinks C is popular, then a would also think that C is
Popular, even if this is a ordered pair in the input, explicitly specified. Your task is to compute the number of cows that was 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.Output* Line 1: A single integer, that is, the number of cows who was considered popular by every and other cow.Sample Input
3 31 22) 12 3
Sample Output
1
HintCow 3 is the only Cow of high popularity.Ideas
Test Instructions: give the envy of a pair of cows, and when a envy b,b envy C, you can think C is also a envy. A few cows were admired by all the other cows in the N cow.
Puzzle : According to the example can be seen, this graph is not a DAG graph, but we can targin to make it into a DAG graph, for the Dag graph, we know that if a cow has a degree, then it is not all the cattle admired by all other cows, If its degree is 0 then it is possible to be the cattle admired by all other cows, but when the 0 of the cattle more than 1 o'clock, there is no other than their own cattle admired by other cows, because there must be another end of the 0 cattle do not admire it.
#include <iostream> #include <cstdio> #include <cstring>using namespace std;const int maxn = 10005;int Tot, top, scc_cnt, Index;int HEAD[MAXN], DFN[MAXN], LOW[MAXN], OUTDE[MAXN], BELONG[MAXN], ST[MAXN], INST[MAXN], CNT[MAXN] ; struct edge{int V, next;} edge[maxn*maxn];void init () {tot = top = index = scc_cnt = 0; Memset (Head,-1, sizeof (head)); memset (belong, 0, sizeof (belong)); memset (DFN, 0, sizeof (DFN)); memset (low, 0, sizeof); memset (St, 0, sizeof (ST)); memset (inst, 0, sizeof (inst)); memset (outde, 0, sizeof (OUTDE)); memset (CNT, 0, sizeof (CNT));} void Addedge (int u, int v) {Edge[tot] = (edge) {V, head[u]}; Head[u] = tot++;} void Targin (int u) {int V; Dfn[u] = low[u] = ++index; St[++top] = u; Inst[u] = 1; for (int i = head[u];i! = -1;i = edge[i].next) {v = edge[i].v; if (!dfn[v]) {Targin (v); Low[u] = min (low[u],low[v]); } else if (Inst[v]) Low[u]= Min (low[u],dfn[v]); } if (dfn[u] = = Low[u]) {scc_cnt++; do {v = st[top--]; INST[V] = 0; BELONG[V] = scc_cnt; cnt[scc_cnt]++; } while (U! = v); }}int Main () {int N, M, U, V, res, sum = 0; Init (); scanf ("%d%d", &n, &m); for (int i = 0; i < M; i++) {scanf ("%d%d", &u, &v); Addedge (U, v); } for (int i = 1; I <= N; i++) if (!dfn[i]) targin (i); for (int i = 1, i <= N; i++) {for (int j = head[i]; ~j; j = edge[j].next) {int v = edge[j ].V; if (belong[i]! = Belong[v]) {outde[belong[i]]++; }}} for (int i = 1; I <= scc_cnt; i++) {if (!outde[i]) {res = i; sum++; }} if (Sum > 1) printf ("0\n"); else printf ("%d\n", Cnt[res]);}
POJ 2186 Popular Cows (targin pinch point)