连通图 poj2186 最受欢迎的牛(求最受欢迎的牛的数量)
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 27531 | Accepted: 11077 |
Description
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
* Lines 2..1+M: Two space-separated numbers A and B, meaning that A thinks B is popular.
Output
Sample Input
3 3
1 2
2 1
2 3
Sample Output
1
Hint
Source
#include <string.h>
#include <stdio.h>
#define V 10500
#define E 50500 struct edge
{
int to, next;
}Edge[E];
int head[V], e, n; int indeg[V], outdeg[V]; //点的入度和出度数
int belong[V], low[V], dfn[V], scc, cnt;//dfn[]:遍历到u点的时间; low[]:u点可到达的各点中最小的dfn[v]
int S[V], top;
bool vis[V];//v是否在栈中 int addedge(int u, int v)
{
Edge[e].to = v;
Edge[e].next = head[u];
head[u] = e++;
return ;
}
void tarjan(int u)
{
int v;
dfn[u] = low[u] = ++cnt;//开始时dfn[u] == low[u]
S[top++] = u;//不管三七二十一进栈
vis[u] = true;
for (int i=head[u]; i!=-; i=Edge[i].next)
{
v = Edge[i].to;
if (dfn[v] == )//如果v点还未遍历
{
tarjan(v);//向下遍历
low[u] = low[u] < low[v] ? low[u] : low[v];//确保low[u]最小
}
else if (vis[v] && low[u] > dfn[v])//v在栈中,修改low[u]
low[u] = dfn[v];
}
if (dfn[u] == low[u])//u为该强连通分量中遍历所成树的根
{
++scc;
do
{
v = S[--top];//栈中所有到u的点都属于该强连通分量,退栈
vis[v] = false;
belong[v] = scc;
} while (u != v);
} } int solve(){
scc = top = cnt = ;
memset(dfn, , sizeof(dfn));
memset(vis, false, sizeof(vis));
for (int u=; u<=n; ++u)
if (dfn[u] == )
tarjan(u);
return scc;
} void count_deg()
{
memset(indeg, , sizeof(indeg));
memset(outdeg, , sizeof(outdeg));
for (int u=; u<=n; ++u)
for (int i=head[u]; i!=-; i=Edge[i].next)
{
int v = Edge[i].to;
if (belong[u] != belong[v])
{
indeg[belong[v]]++;
outdeg[belong[u]]++;
}
}
} int main()
{
int u, v, i;
int m;
while (~scanf("%d%d", &n,&m))
{
e = ;
memset(head, -, sizeof(head));
for (int i=;i<=m;i++){
scanf("%d%d",&u,&v);
addedge(u, v);
}
solve();
count_deg(); int num=,tmp;
for(int i = ; i <= scc; i++)
{
if(!outdeg[i])
{
num++;
tmp = i;
} }
int ans=;
if(num == )
{
for(int i = ; i <= n; i++)
{
if(belong[i] == tmp)
ans++;
}
printf("%d\n", ans);
}
else
{
printf("0\n");
} }
return ;
}
连通图 poj2186 最受欢迎的牛(求最受欢迎的牛的数量)的更多相关文章
- 求1+2+3...+n 牛客网 剑指Offer
求1+2+3...+n 牛客网 剑指Offer 题目描述 求1+2+3+...+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字及条件判断语句(A?B:C). ...
- HihoCoder1084: 扩展KMP(二分+hash,求T串中S串的数量,可以失配一定次数)
时间限制:4000ms 单点时限:4000ms 内存限制:256MB 描述 你知道KMP吗?它是用于判断一个字符串是否是另一个字符串的子串的算法.今天我们想去扩展它. 在信息理论中,在两个相同长度的字 ...
- 《编程题》穷举法求N年后有多少头牛
若一头小母牛,从出生起第四个年头开始每年生一头母牛,按这个规律,第N年时有多少头母牛? #include <iostream> int main(int argc, const char ...
- POJ-3107 Godfather 求每个节点连接的联通块数量
dp[n][2],维护儿子的联通块数量和父亲的联通块数量. 第一遍dfs求儿子,第二遍dfs求爸爸. #include<iostream> #include<cstring> ...
- 牛客oi测试赛 二 B 路径数量
题目描述 给出一个 n * n 的邻接矩阵A. A是一个01矩阵 . A[i][j]=1表示i号点和j号点之间有长度为1的边直接相连. 求出从 1 号点 到 n 号点长度为k的路径的数目. 输入描述: ...
- 求数组中的逆序对的数量----剑指offer36题
在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对.输入一个数组,求出这个数组中的逆序对的总数: 如数组{7,5,6,4},逆序对总共有5对,{7,5},{7,6},{7, ...
- 547. Friend Circles 求间接朋友形成的朋友圈数量
[抄题]: There are N students in a class. Some of them are friends, while some are not. Their friendshi ...
- JSF页面中的JS取得受管bean的数据(受管bean发送数据到页面)
JSF中引入jsf.js文件之后,可以拦截jsf.ajax.request请求.一直希望有一种方法可以像jquery的ajax一样,能在js中异步取得服务器端发送的数据.无奈标准JSF并没有提供这样的 ...
- 不裸缩点》。。。POJ2186受欢迎的牛
不裸缩点>...POJ2186受欢迎的牛 :first-child { margin-top: 0; } blockquote > :last-child { margin-bottom: ...
随机推荐
- SVN合并步骤
1.trunk->branch/tag 分支路径在分支文件夹中,选择右键检出 2.合并分支到主干分支新增 1.txt 文件 需要合并到主干 在trunck->鼠标右键合并->合并到不 ...
- 在浏览器里使用SAPGUI
事务码SICF,service name输入WEBGUI, 点右键,选择Test Service: 可以在浏览器里敲SE38进入ABAP editor了: 然么缺乏语法高亮显示: 如果想要浏览器里的语 ...
- 巧用代理设计模式(Proxy Design Pattern)改善前端图片加载体验
这篇文章介绍一种使用代理设计模式(Proxy Design Pattern)的方法来改善您的前端应用里图片加载的体验. 假设我们的应用里需要显示一张尺寸很大的图片,位于远端服务器.我们用一些前端框架的 ...
- mysql数据库操作手册
1 存储过程的写法 以下是一个带有入参的存储过程模板, #删除方案-存储过程 CREATE PROCEDURE procPersonAppointRecallPlanByPlanUuidDelet ...
- 阻止form元素内的input标签回车提交表单
<form></form>标签内input元素回车会默认提交表单. 阻止回车默认提交表单: $('form').on('keydown', function (event) { ...
- VC++:鼠标的使用
长期改变鼠标形状: SetClassLongPtr(GetSafeHwnd(), GCLP_HCURSOR, (LONG)LoadCursor(NULL, IDC_WAIT));//这个是x64下可以 ...
- CMDB 数据加密 最终整合API验证+AES数据加密
当CMDB运行在内网的时候,经过API验证的三关是没有问题的,但是如果运行在外网,有一个问题是,黑客截取后的访问速度比客户端快的时候还会造成数据泄露.为了解决这个问题,就要对数据进行加密 RSA加密 ...
- WINDOWS-API:关于线程CreateThread,_beginthead(_beginthreadex),AfxBeginThread
[转]windows多线程编程CreateThread,_beginthead(_beginthreadex)和AfxBeginThread的区别 在Windows的多线程编程中,创建线程的函数主要有 ...
- opensuse 15.0 安装ctdb
问题 1 2019/05/20 15:27:14.574363 ctdb-eventd[26329]: 60.nfs: /etc/ctdb/nfs-linux-kernel-callout: line ...
- windbg双机调试配置
环境 虚拟机 win7 Pro x86 vmware 12 windbg x86 虚拟机win7配置 管理员权限运行cmd.exe 然后输入以下命令: bcdedit /? bcdedit /enum ...