Explosion at Cafebazaar

时间限制: 1 Sec  内存限制: 128 MB

题目描述

You are an engineer at Cafebazaar and your specialty is understanding networks and their behaviors. Your colleagues have designed a network of n switches with m directed links that connect pairs of these switches. Each switch has a buffer where it stores the data and two modes, called sending mode and receiving mode. In the sending mode, a switch sends the data stored in its buffer to each of its outgoing links simultaneously and at the end clears its buffer. In the receiving mode,a switch concatenates the data from all its incoming links and stores them in its buffer, so at the end, the length of the data stored in its buffer is equal to the sum of the lengths of all the data on the incoming links. 
Assume that at time t = 0, all the switches are in sending mode and have an empty buffer except switch i which stores a 1-bit package of data in its buffer. Also, all the switches change their modes after each second, so at time t = 1 all the switches change to receiving mode, at time t = 2 they change to sending mode, and so on. Switch i is called explosive if the maximum length of data stored in the buffers of switches is not bounded as t goes to infinity. 
Your task is to calculate the number of explosive switches in the network.

输入

There are multiple test cases in the input. The first line of each test case contains two space-separated integers n and m,where n indicates the number of switches and m indicates the number of directed links (1 ⩽ n, m ⩽ 50, 000). Each of the next m lines contains two space-separated integers u, v where (u, v) indicates a directed link from switch u to switch v (1 ⩽ u, v ⩽ n, u≠v). The input terminates with a line containing 0 0 which should not be processed.

输出

For each test case, output a single line containing the number of explosive switches.

样例输入

3 3
1 2
2 3
3 1
5 6
1 2
2 3
3 1
3 4
4 5
5 3
4 5
1 2
2 3
3 2
3 2
3 4
0 0

样例输出

0
5
3

来源/分类

Tehran2016


题意:在n个点m条边的图中,每个点可以沿有向边发送数据,当然每个点也可以接收数据。每个点发送数据后,该点的数据就会清零,图中所有点发送和接收数据是同步的。(即它们一起发送数据,一起接收数据)。问在哪些点放上单位数据,可以使图中某些点的数据量达到无穷大。
分析:要使单位数据最后变成无穷大数据,首先要有环。首先考虑单个环使数据爆炸的情况。环中必须存在某一点,它的入度大于等于2,这个环才能无限扩充数据。其次如果是简单的n个点n条边的环,它虽然不能使数据量增大,但是它可以源源不断的产生数据和保留数据,所以如果一个简单环连接着另一个环,那么这个简单环上的点也可以使数据爆炸。最后,所有连向数据爆炸环的点都可以使数据爆炸。
做法:我的做法比较繁琐,我先求出了所有的环(强连通分量)并把环缩成点,再判一个环是否可以使数据爆炸,再判两个环是否可以使数据爆炸,最后反向跑dfs判连向爆炸环的点。
#include<bits/stdc++.h>
#define N 50050
using namespace std; int dfn[N],low[N],vis[N],color[N],now_clock,now_color;
int Stack[N],top;
vector<int>edges[N];
int explosive[N],du[N],sum_point[N],dfs2_vis[N],ans;
vector<int>newedges[N];
vector<int>edges2[N]; void init(int n)
{
for(int i=;i<=n;i++)edges[i].clear(),newedges[i].clear(),edges2[i].clear();
memset(dfn,,sizeof(dfn));
memset(low,,sizeof(low));
memset(vis,,sizeof(vis));
memset(explosive,,sizeof(explosive));
memset(du,,sizeof(du));
memset(sum_point,,sizeof(sum_point));
memset(dfs2_vis,,sizeof(dfs2_vis));
now_clock=;
top=;
now_color=;
ans=; } void dfs(int x)
{
dfn[x]=low[x]=now_clock++;
vis[x]=;
Stack[++top]=x; int Size=edges[x].size();
for(int i=;i<Size;i++)
{
int v=edges[x][i];
if(!dfn[v])
{
dfs(v);
low[x]=min(low[x],low[v]);
}
else
if(vis[v])low[x]=min(low[x],dfn[v]);
} if(dfn[x]==low[x])
{
while(Stack[top]!=x)
{
vis[Stack[top]]=;
color[Stack[top]]=now_color;
top--;
} vis[Stack[top]]=;
color[Stack[top]]=now_color++;
top--;
}
} void dfs2(int x)
{
if(!dfs2_vis[x])
{
dfs2_vis[x]=;
ans+=sum_point[x];
} int Size=newedges[x].size();
for(int i=;i<Size;i++)
if(!dfs2_vis[newedges[x][i]])
dfs2(newedges[x][i]); } int dfs3(int x)
{
if(sum_point[x]>=)return ; int Size=edges2[x].size();
for(int i=;i<Size;i++)
if(dfs3(edges2[x][i]))return ; return ;
} int main()
{ int n,m;
while(scanf("%d %d",&n,&m)==)
{
if(!n)return ; init(n); while(m--)
{
int u,v;
scanf("%d %d",&u,&v);
edges[u].push_back(v);
} for(int i=;i<=n;i++)
if(!dfn[i])dfs(i); for(int i=;i<=n;i++)
{
int Size=edges[i].size();
for(int j=;j<Size;j++)
{
int u=i,v=edges[i][j];
if(color[u]==color[v])
du[v]++; }
} for(int i=;i<=n;i++)
{
sum_point[color[i]]++;
if(du[i]>=)explosive[color[i]]=;
}
/* for(int i=1;i<=n;i++)printf("%d ",color[i]);
printf("\n");
for(int i=1;i<now_color;i++)printf("%d ",sum_point[i]);
printf("\n");
for(int i=1;i<now_color;i++)printf("%d ",explosive[i]);
printf("\n");
*/
for(int i=;i<=n;i++)
{
int Size=edges[i].size();
for(int j=;j<Size;j++)
{
int u=i,v=edges[i][j];
if(color[u]!=color[v])
{
newedges[color[v]].push_back(color[u]);
edges2[color[u]].push_back(color[v]);
}
}
} for(int i=;i<now_color;i++)
if(!explosive[i]&&sum_point[i]>=)
{
int Size=edges2[i].size();
for(int j=;j<Size;j++)
if(dfs3(edges2[i][j]))
{
explosive[i]=;
break;
}
} for(int i=;i<now_color;i++)
if(explosive[i])dfs2(i); printf("%d\n",ans);
} return ;
}

Explosion at Cafebazaar的更多相关文章

  1. 2016 ACM ICPC Asia Region - Tehran

    2016 ACM ICPC Asia Region - Tehran A - Tax 题目描述:算税. solution 模拟. B - Key Maker 题目描述:给出\(n\)个序列,给定一个序 ...

  2. NBUT 1635 Explosion(最小顶点覆盖)

    [1635] Explosion 时间限制: 10000 ms 内存限制: 65535 K 问题描述 there is a country which contains n cities connec ...

  3. Codeforces Gym 191033 E. Explosion Exploit (记忆化搜索+状压)

    E. Explosion Exploit time limit per test 2.0 s memory limit per test 256 MB input standard input out ...

  4. 2018-2019 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2018)-E. Explosion Exploit-概率+状压dp

    2018-2019 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2018)-E. Explosion Exploit-概率+状压dp [P ...

  5. upc组队赛3 Chaarshanbegaan at Cafebazaar

    Chaarshanbegaan at Cafebazaar 题目链接 http://icpc.upc.edu.cn/problem.php?cid=1618&pid=1 题目描述 Chaars ...

  6. hdu 5036 Explosion(概率期望+bitset)

    Problem Description Everyone knows Matt enjoys playing games very much. Now, he to N. Input The firs ...

  7. HDU - 5036 Explosion

    Problem Description Everyone knows Matt enjoys playing games very much. Now, he is playing such a ga ...

  8. hdu5306 Explosion

    题目链接 题意 有n个房间,每个房间里面有若干把钥匙,每把钥匙可以打开对应的一扇门.如果手中没有钥匙,就要随机轰炸一个房间来打开这个房间.如果有钥匙,就要去打开这些房间.问期望轰炸次数是多少. 思路 ...

  9. HDU 5036 Explosion (传递闭包+bitset优化)

    <题目链接> 题目大意: 一个人要打开或者用炸弹砸开所有的门,每个门后面有一些钥匙,一个钥匙对应一个门,告诉每个门里面有哪些门的钥匙.如果要打开所有的门,问需要用的炸弹数量为多少. 解题分 ...

随机推荐

  1. Python学习日志9月15日

    一周就要过去了,而我跟一周以前没什么区别.回想一下,我这周做了什么事情呢.恍然若失.这周的精力都浪费在很多不必要的事情上了.学过一片古文,讲后羿学射箭,他有一个同学跟他一样聪明,在一起学习.后羿呢,专 ...

  2. winhex 中磁盘大小与偏移

    下图为c盘(活动分区).上方base offset为相对于整个硬盘的字节偏移量.partition 1中信息包括c盘开始扇区,总扇区数.partition 2 信息为扩展分区开始扇区和扇区数.由 P1 ...

  3. ucosii(2.89)mutex 应用要点

    mutex 的创建在于共享资源打交道是可以可以保证满足互斥条件:1,必须保证继承优先级要高于可能与相应共享资源打交道的任务中优先级最高的优先级.2,不要将占有Mutex的任务挂起,也不要让占有mute ...

  4. js 监听页面url锚点变化 window.onpopstate

    window.onpopstate = function (event) { if (location.href.indexOf('#') == -1) { location.reload(); } ...

  5. Gersgorin 圆盘

    将学习到什么 好多.   Gersgorin 圆盘定理   对任何 \(A \in M_n\),我们总可以记 \(A=D+B\),其中 \(D=\mathrm{diag}(a_{11},\cdots, ...

  6. 2、Task 使用 ContinueWith 而不要使用 Wait

    1.线程自旋:在阻塞线程的时候为了等待解锁(访问临界资源)(Sleep). 2.上下文切换:将处理器当前线程的状态保存到操作系统内部的线程对象中,然后再挑出一个就绪的线程,把上下文信息传递给处理器,然 ...

  7. springmvc导出excel(POI)

    /** * 导出excel表格 */ @RequestMapping(value = "/doExportData", method = {RequestMethod.POST, ...

  8. C++的反射

    写得挺不错,支持转帖下 C++语言本身是不支持反射的,但实际应用中总是会有将对象序列化的需求,总不可能C++不支持,我们就不用C++了,既然发明C++的大师们没有考虑这个,那我们只有自己动手了,毛主席 ...

  9. 更新portage之后 安装 certbot

    运行的时候一直报如下的错误: sudo certbot 错误结果: Traceback (most recent call last): File "/usr/lib/python-exec ...

  10. 微信小程序 wx.request POST请求------中文乱码问题

    问题: 一个简单的表单,提交后台返回数据“提交成功”. 以为没问题了,但是没过多久后台小哥就问为啥那么多乱码,找了很久原因,发现在提交的时候就已经乱码了. 嗯,前端问题,然后测试GET/POST方法. ...