Popular Cows
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 25945 | Accepted: 10612 |
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
题意:有一群奶牛,每头奶牛都想成为最受欢迎的奶牛(当其它所有奶牛认为它受欢迎,它就是最受欢迎的)比如:A认为B受欢迎,B认为C受欢迎,那么在这三头奶牛中,最受欢迎的是C号奶牛,因为A认为B受欢迎,B认为C受欢迎,那么A 也认为C欢迎。该题要求的是有多少头奶牛是最受欢迎的奶牛。我们可以把每头奶牛想像成每个顶点,再把每个顶点按照所给关系连成图。当一头奶牛最受欢迎时,其它所有奶牛都觉得它受欢迎,所以每个顶点都可以到达该点。在全部顶点中,如果不能形成一幅图,证明没有一头奶牛是其它所有奶牛都认为它所欢迎,因为就是不存在最受欢迎的奶牛。 在这幅图中,为了方便求解出问题,我们可以把相互认为受欢迎的奶牛用一个强连通分量表示。当一个强连通分量中的奶牛是最受欢迎的时候,证明其它所有强联通分量都指向它,它在整幅图的走向中是最后的一个分量,所以可知他的出度为0。
代码如下:
#include<iostream> #include<vector> #include<cstring> #include<cstdio> #include<cmath> using namespace std; int N,M; vector<int> G[10005]; vector<int> rG[10005]; vector<int> vs; bool used[10005]; //用于查看该点是否被访问 int cmp[10005]; //存储该点对应的连通分量(拓扑序) int ans[10005]; //该连通分量对用的顶点个数 int out[10005]; //该连通分量的出度 int k; //初始化结合 void init() { fill(cmp,cmp+N+1,-1); fill(ans,ans+N+1,1); fill(out,out+N+1,0); fill(used,used+N+1,false); for(int i = 1;i <= N;i++) { G[i].clear(); rG[i].clear(); } vs.clear(); } //遍历正向图,并按后序遍历的方式标上序号。 void dfs(int u) { used[u] = true; for(int i = 0;i < G[u].size();i++) { if(!used[G[u][i]]) { dfs(G[u][i]); } } vs.push_back(u); } //遍历逆向图,求出强连通分量,ans数值表示该联通分量的个数 void rdfs(int v,int k1) { used[v] = true; cmp[v] = k1; for(int i = 0;i < rG[v].size();i++) { if(!used[rG[v][i]]) { ans[k1]++; rdfs(rG[v][i],k1); } } } void scc() { fill(used,used+N+1,false); vs.clear(); for(int i = 1;i <= N;i++) { if(!used[i]) { dfs(i); } } //正向遍历把所有顶点都访问一遍啦,所以需要把全部定点置为未访问的状态 fill(used,used+N+1,false); k = 0; //k代表强连通分量的个数 for(int i = vs.size()-1;i >= 0;i--) { if(!used[vs[i]]) { rdfs(vs[i],++k); } } //求解每个强连通分量的出度 for(int i = 1;i <= N;i++) { for(int j = 0;j < G[i].size();j++) { if(cmp[i] == cmp[G[i][j]]) continue; else out[cmp[i]]++; //该联通分量的出度 } } int ret = 0; int cnt = 0; //如果该强连通分量没有出度,证明它是其它所有的强连通分量所指向的分量 for(int i = 1;i <= k;i++) { if(!out[i]) { cnt++; ret = ans[i]; } //如果存在两个以上没有出度的强连通分量,证明整幅图不是连通的 if(cnt == 2) { ret = 0; break; } } printf("%d\n",ret); } int main() { while(scanf("%d%d",&N,&M) != EOF) { int i; int a,b; init(); for(i = 1;i <= M;i++) { scanf("%d%d",&a,&b); //添加正想和逆向图 G[a].push_back(b); rG[b].push_back(a); } scc(); } return 0; }