洛谷

题目就是让我们在DAG中找到一些点,覆盖所有点。

因为是DAG,可以想到tarjan缩一下点。假设我们需要找x个点,那么答案就是(n-x)/n。

我们怎么选点呢?

敏锐的我们很快就能想到,直接选出所有入度为0的点。

但是,当我们发现一个入度为0的点,但是其中元素为1,而它的出边所到的点的入度都>1,则x--。

因为它们可以被别的点更新。

code:

#include <bits/stdc++.h>
using namespace std; const int N=100010;
const int M=300010;
int n,m;
int s[M][2],o[N];
int low[N],dfn[N],sccno[N],scc,dfscnt,w[N];
int sta[N],top;
struct Edge {
int next,to;
}e[M];
int last[N],len,in[N],ans; char buffer[M],*S,*T;
inline char Get_Char()
{
if(S==T)
{
T=(S=buffer)+fread(buffer,1,M,stdin);
if(S==T) return EOF;
}
return *S++;
} int Get_Int()
{
char c;
int re=0;
for(c=Get_Char();c<'0'||c>'9';c=Get_Char());
while(c>='0'&&c<='9')
re=(re<<1)+(re<<3)+(c^48),c=Get_Char();
return re;
} void add(int x,int y)
{
s[++o[0]][0]=y,s[o[0]][1]=o[x],o[x]=o[0];
} void tarjan(int x)
{
sta[++top]=x;
low[x]=dfn[x]=++dfscnt;
for (int i=o[x];i;i=s[i][1]) {
int y=s[i][0];
if (!dfn[y])
tarjan(y),low[x]=min(low[x],low[y]);
else if (!sccno[y])
low[x]=min(low[x],dfn[y]);
}
if (dfn[x]==low[x]) {
++scc;
while (1) {
int y=sta[top--];
sccno[y]=scc;
++w[scc];
if (x==y) break;
}
}
} void add2(int x,int y)
{
e[++len].to=y,e[len].next=last[x],last[x]=len;
} int main()
{
int x,y;
n=Get_Int(),m=Get_Int();
if (!m) {printf("%.6lf",1.0/n);return 0;}
if (n==1) {puts("1.000000");return 0;}
for (int i=1;i<=m;++i)
x=Get_Int(),y=Get_Int(),add(x,y);
for (int i=1;i<=n;++i)
if (!dfn[i]) tarjan(i);
for (x=1;x<=n;++x)
for (int i=o[x];i;i=s[i][1]) {
y=s[i][0];
if (sccno[x]!=sccno[y]) {
add2(sccno[x],sccno[y]);
++in[sccno[y]];
}
}
bool flag=0;
for (x=1;x<=scc;++x) {
if (w[x]==1&&in[x]==0) {
bool k=0;
for (int i=last[x];i;i=e[i].next) {
y=e[i].to;
if (in[y]>1) k=1;
else {k=0;break;}
}
if (k) flag=1;
}
if (flag) {--ans;break;}
}
for (int i=1;i<=scc;++i)
if (!in[i]) ++ans;
double tmp=(n-ans)*1.0/n;
printf("%.6lf",tmp);
return 0;
}

最新文章

  1. io流操作大全
  2. 19_高级映射:一对多查询(使用resultMap)
  3. c# 捕捉键盘按键
  4. java Thread.join()
  5. 什麼是 N-key 與按鍵衝突?原理說明、改善技術、選購注意完全解析
  6. python 列表转字典
  7. python/基础输出输入用法
  8. 使用BeanUtils类实现DTO之间的同名属性复制
  9. Fiddler抓取HTTPS请求配置
  10. AlexeyAB大神版yolo 待完善
  11. nginx的web缓存服务环境部署记录
  12. SimpleDateFormat 格式化参数说明
  13. python实现tail -f 功能
  14. vue 非父子组件传值
  15. javascript 模块化编程
  16. 通过cgroup给docker的CPU和内存资源做限制
  17. MyISAM和innoDB对比,覆盖索引简单回顾
  18. 解决不能修改 Mysql 慢查询 long_query_time 值的问题
  19. openerp学习笔记 对象继承,对象初始化数据
  20. C# SuperSocket 消息推送

热门文章

  1. 出现蓝屏代码0x0000007b的原因及解决办法
  2. Sql Server2005 Synonyms
  3. Python - pandas 数据分析
  4. atitit.项目设计模式---ioc&#160;attilax总结v4&#160;q11
  5. Angularjs学习笔记6_table1
  6. nginx源码学习_数据结构(ngx_pool_t)
  7. jquery的val()
  8. 基于AFNetworking的网络判断【原创】
  9. Android Studio 使用笔记:给包重命名~~有点水
  10. constructors and destructors