PKU 2186 Popular Cows
来源:互联网 发布:java深度优先遍历算法 编辑:程序博客网 时间:2024/05/16 17:37
看完解题报告敲的....
没啥好说的做个模板贴出来-_-
Popular Cows
Time Limit:1sMemory limit:32MAccepted Submit:25Total Submit:64
Every cow's dream is to become the most popular cow in the herd. In aherd 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 cowA thinks that cow B is popular. Since popularity is transitive, if Athinks B is popular and B thinks C is popular, then A will also thinkthat C is popular, even if this is not explicitly specified by anordered pair in the input. Your task is to compute the number of cowsthat are considered popular by every other cow. * 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. * Line 1: A single integer that is the number of cows who are considered popular by every other cow.
这里把解题报告里面的一些思路贴出来 。。
容易发现,新图中唯一的出度为0的点即为所求。
因为新图不含有环,这样的点一定存在。
如果出度为0的点不唯一则无解。
幻灯片 15 .O{font-size:149%;}
下面是自己写的
没啥好说的做个模板贴出来-_-
Input
Output
Sample Input
3 3
1 2
2 1
2 3
Sample Output
1
Hint
Cow 3 is the only cow of high popularity.
这里把解题报告里面的一些思路贴出来 。。
容易发现,新图中唯一的出度为0的点即为所求。
因为新图不含有环,这样的点一定存在。
如果出度为0的点不唯一则无解。
•
- #include<iostream>
- #include<vector>
- using namespace std;
- #define SIZE 10050
- typedef vector<int> VI;
- typedef vector<VI> VII;
- VII adj;//原图
- VII tadj;//反向图
- bool v[SIZE];//标记是否访问
- int ttime[SIZE];//顺序
- int n,m,top,scc;//scc标号
- int id[SIZE];
- int idtotal[SIZE];
- int out[SIZE];//出度
- int num;
- void dfs1(int i)//按顺序标记点
- {
- int k,temp;
- for(k=0;k<adj[i].size();k++)
- {
- temp=adj[i][k];//下一个点
- if(!v[temp])
- {
- v[temp]=true;
- dfs1(temp);
- ttime[top++]=temp;
- }
- }
- }
- void dfs2(int i)
- {
- int k,temp;
- id[i]=scc;
- for(k=0;k<tadj[i].size();k++)
- {
- temp=tadj[i][k];
- if(!v[temp])
- {
- v[temp]=true;
- num++;
- dfs2(temp);
- }
- }
- }
- void init()
- {
- int i,x,y;
- top=0;
- adj.resize(n+1);
- tadj.resize(n+1);
- for(i=0;i<adj.size();i++)adj[i].clear();
- for(i=0;i<tadj.size();i++)tadj[i].clear();
- fill_n(out,SIZE,0);
- for(i=1;i<=m;i++)
- {
- scanf("%d%d",&x,&y);
- adj[x].push_back(y);
- tadj[y].push_back(x);
- }
- }
- void solve()
- {
- int i;;
- int len,j,temp;
- init();
- fill_n(v,SIZE,false);
- for(i=1;i<=n;i++)
- {
- if(!v[i])
- {
- v[i]=true;
- dfs1(i);
- ttime[top++]=i;
- }
- }
- fill_n(v,SIZE,false);
- fill_n(idtotal,SIZE,0);
- scc=0;
- for(i=top-1;i>=0;i--)//逆序
- {
- if(!v[ttime[i]])
- {
- num=1;scc++;
- v[ttime[i]]=true;
- dfs2(ttime[i]);
- idtotal[scc]=num;//子图编号的总的顶点数
- }
- }
- int find_ans=0;
- int ans;
- for(i=1;i<=n;i++)
- {
- len=adj[i].size();
- for(j=0;j<len;j++)
- {
- temp=adj[i][j];
- if(id[temp]!=id[i])out[id[i]]++;
- }
- }
- for(i=1;i<=scc;i++)
- {
- if(!out[i])find_ans++,ans=idtotal[i];
- }
- printf("%d/n",find_ans==1?ans:0);
- }
- int main()
- {
- while(scanf("%d%d",&n,&m)!=EOF)solve();
- return 0;
- }
下面是自己写的
- #include <iostream>
- #include <vector>
- using namespace std;
- typedef vector<int> VI;
- typedef vector<VI> VII;
- #define N 10001
- VII mat1,mat2;
- bool v[N];
- int o[N],olen,n,t,out[N],ccnt[N],clen,cnt,pre[N];
- void mark(int x)
- {
- int i,tmp,tlen=mat1[x].size();
- for(i=0;i<tlen;i++)
- {
- tmp=mat1[x][i];
- if(!v[tmp]){
- v[tmp]=true;
- mark(tmp);
- o[olen++]=tmp;
- }
- }
- }
- void dfs(int x)
- {
- int i,tmp,tlen=mat2[x].size();
- pre[x]=clen;
- for(i=0;i<tlen;i++){
- tmp=mat2[x][i];
- if(!v[tmp]){
- v[tmp]=true;
- cnt++;
- dfs(tmp);
- }
- }
- }
- inline void init()
- {
- int i,x,y;
- mat1.resize(n+1);mat2.resize(n+1);
- for(i=olen=0;i<=n;i++)mat1[i].clear(),mat2[i].clear();
- for(i=0;i<t;i++)scanf("%d%d",&x,&y),mat1[x].push_back(y),mat2[y].push_back(x);
- }
- inline void solve()
- {
- int i,j,tmp,tlen,ans;
- memset(v,false,sizeof(v));
- for(i=1;i<=n;i++){
- if(!v[i])
- {
- v[i]=true;
- mark(i);
- o[olen++]=i;
- }
- }
- memset(v,false,sizeof(v));
- memset(ccnt,0,sizeof(ccnt));
- memset(out,0,sizeof(out));
- clen=0;
- for(i=olen-1;i>=0;i--){
- if(!v[o[i]]){
- cnt=1;
- clen++;
- v[o[i]]=true;
- dfs(o[i]);
- ccnt[clen]=cnt;
- }
- }
- for(i=1;i<=n;i++){
- tlen=mat1[i].size();
- for(j=0;j<tlen;j++){
- tmp=mat1[i][j];
- if(pre[tmp]!=pre[i])out[pre[i]]++;
- }
- }
- for(i=1,tmp=0;i<=clen;i++){
- if(out[i]==0)tmp++,ans=ccnt[i];
- }
- if(tmp==1)printf("%d/n",ans);
- else printf("0/n");
- }
- int main()
- {
- while(scanf("%d%d",&n,&t)!=EOF){init();solve();}
- return 0;
- }
- PKU 2186 Popular Cows
- pku 2186 Popular Cows(强连通分量)
- POJ 2186 Popular Cows
- poj 2186 Popular Cows
- poj 2186 Popular Cows
- poj 2186 Popular Cows
- poj 2186 Popular Cows
- poj 2186 Popular Cows
- POJ 2186 Popular Cows
- poj 2186 Popular Cows
- poj 2186 Popular Cows
- POJ 2186(Popular Cows)
- POJ 2186 Popular Cows
- poj 2186 Popular Cows
- poj 2186 Popular Cows
- POJ 2186 Popular Cows
- POJ 2186: Popular Cows
- poj 2186 Popular Cows
- 利用ajax获取服务器时间,并显示
- js 每秒钟刷新一次页面 || dorado每秒钟执行一次命令方法
- 表单验证类
- 人生致命的八个经典问题
- java控制 添加图片到word
- PKU 2186 Popular Cows
- excel flash
- 用JSF1.2时提示could not find Factory: javax.faces.context.FacesContextFactory解决办法
- Js中 关于top、clientTop、scrollTop、offsetTop等
- 关于java内存优化
- Struts原理简介
- 首发博客测试页
- 解决Window共享人数限制
- 英文 日文 自我介绍