题意

由于这是个\(DAG\),我们考虑拓朴排序,求某个点能到的和能到它的点,这是两个问题,我们可以正反两边拓朴排序,这样就只用考虑它能到的点了

设\(f[x]\)表示\(x\)能到的点数\(+\)能到\(x\)的点数

如果在拓朴排序的过程中:

\(q.size()==1\),显然当前队首\(x\)能到达剩下的所有点,中途记录出现在队列中的点的个数\(tot\),\(f[x]+=n-tot\)

\(q.size()==2\),记录队列中的两点\(x,y\),对于\(y\),如果\(y\)存在\(y->z\)且\(z\)的入度为1,那么\(x\)显然不能到\(z\),标记一下\(x\)即可

最后枚举每个点,判断是否合法即可

code:

#include<bits/stdc++.h>
using namespace std;
const int maxn=300010;
const int maxm=300010;
int n,m,cnt,ans;
int head[maxn],deg[maxn],u[maxm],v[maxm],f[maxn];
bool vis[maxn];
struct edge
{
int to,nxt;
}e[maxm<<1];
inline int read()
{
char c=getchar();int res=0,f=1;
while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
while(c>='0'&&c<='9')res=res*10+c-'0',c=getchar();
return res*f;
}
inline void add(int u,int v)
{
e[++cnt].nxt=head[u];
head[u]=cnt;
e[cnt].to=v;deg[v]++;
}
inline void solve(int x,int y,int w)
{
bool flag=0;
for(int i=head[y];i;i=e[i].nxt)
{
int z=e[i].to;
if(deg[z]==1){flag=1;break;}
}
if(flag)vis[x]=1;
else f[x]+=w;
}
inline void topsort()
{
queue<int>q;
int tot=0;
for(int i=1;i<=n;i++)if(!deg[i])q.push(i),tot++;
while(!q.empty())
{
int x=q.front(),num=q.size();q.pop();
if(num==1)f[x]+=n-tot;
if(num==2)solve(x,q.front(),n-tot);
for(int i=head[x];i;i=e[i].nxt)
{
int y=e[i].to;
if(!(--deg[y]))q.push(y),tot++;
}
}
}
int main()
{
n=read(),m=read();
for(int i=1;i<=m;i++)
{
u[i]=read(),v[i]=read();
add(u[i],v[i]);
}
topsort();
memset(deg,0,sizeof(deg));
memset(head,0,sizeof(head));
cnt=0;
for(int i=1;i<=m;i++)add(v[i],u[i]);
topsort();
for(int i=1;i<=n;i++)if(!vis[i]&&f[i]>=n-2)ans++;
printf("%d",ans);
return 0;
}

CF1062F Upgrading Cities的更多相关文章

  1. [CF1062F]Upgrading Cities[拓扑排序]

    题意 一张 \(n\) 点 \(m\) 边的 \(DAG\) ,问有多少个点满足最多存在一个点不能够到它或者它不能到. \(n,m\leq 3\times 10^5\) 分析 考虑拓扑排序,如果 \( ...

  2. 一句话题解&&总结

    CF79D Password: 差分.两点取反,本质是匹配!最短路+状压DP 取反是套路,匹配是发现可以把操作进行目的化和阶段化,从而第二次转化问题. 且匹配不会影响别的位置答案 sequence 计 ...

  3. Codeforces Round #520 (Div. 2) Solution

    A. A Prank Solved. 题意: 给出一串数字,每个数字的范围是$[1, 1000]$,并且这个序列是递增的,求最多擦除掉多少个数字,使得别人一看就知道缺的数字是什么. 思路: 显然,如果 ...

  4. CodeForces round 520 div2

    A:A Prank 题意:给定一个递增序列, 问最多能删除多少个连续数字,要求删除数字之后能还原成原来的数列. 题解:直接找就好了,为了方便可以使得第0个数字为0, 第n+1个元素为1001 代码: ...

  5. Connect the Cities[HDU3371]

    Connect the Cities Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...

  6. codeforces 613D:Kingdom and its Cities

    Description Meanwhile, the kingdom of K is getting ready for the marriage of the King's daughter. Ho ...

  7. CF449B Jzzhu and Cities (最短路)

    CF449B CF450D http://codeforces.com/contest/450/problem/D http://codeforces.com/contest/449/problem/ ...

  8. VS2013: upgrading a Windows Phone 7/8 and Windows 8 apps(转)

    VS2013: upgrading a Windows Phone 7/8 and Windows 8 apps September 17, 2013Windows 8, Windows PhoneJ ...

  9. Installation and Upgrading

    Cumulative Feature Overview Identifies product features available to you when upgrading. This tool r ...

随机推荐

  1. 转-友晶Sdram_Control_4Port的全页操作Bug?

    http://www.cnblogs.com/edaplayer/p/3678897.html 以前在学校初学fpga的时候碰到sdram就搞不定了,现在突然发现网上有好多现成的代码,友晶的,alte ...

  2. Python3 多线程爬取梨视频

    多线程爬取梨视频 from threading import Thread import requests import re # 访问链接 def access_page(url): respons ...

  3. 10分钟理解BFC原理

    10 分钟理解 BFC 原理 一.常见定位方案 在讲 BFC 之前,我们先来了解一下常见的定位方案,定位方案是控制元素的布局,有三种常见方案: 普通流 (normal flow) 在普通流中,元素按照 ...

  4. CCF-CSP题解 201403-4 无线网络

    新建不超过\(k\)个无线路由器,求使路由器1.2连通最少的中间路由器. 首先常规建图,将相距不超过\(r\)的路由器(包括新建的)相连. 想到了分层\(dijkstra\).类似的,作\(bfs\) ...

  5. poj 1270 Following Orders (拓扑排序+回溯)

    Following Orders Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5473   Accepted: 2239 ...

  6. let和const总结(ES6)

    文章目录 let const 1. let要好好用 1. 基本用法 2. let声明的变量不存在变量提升 3. TDZ(temporal dead zone)暂时性死区 4. 不允许重复声明 2. 块 ...

  7. C#界面设计相关设置

    1.Anchor属性设置 对需要设置的控件,如主窗体中的TextBox,设置Anchor为上下左右都停靠,就会实现随着窗体的变化而变化. 2.AutoScaleMode属性的用法:<转自:htt ...

  8. 2019有赞中高级Java工程师面试题与解答

    说说JVM的内存分区 线程私有的区域 程序计数器:JVM中程序计数器相当于汇编语言中的CPU中的寄存器,保存程序当前执行的指令的地址. 虚拟机栈:Java方法执行的栈由许多个栈帧构成,每个栈帧对应一个 ...

  9. Graylog 环境搭建与使用

    graylog 官网地址: www.graylog.org graylog 简易架构图: graylog 生产架构图: graylog 有两个版本,1.企业版:2.开源版 下面使用开源版本搭建日志中心 ...

  10. 如何将本地的项目推送到github

    一.创建密钥 1.本地终端命令行生成密钥 访问密钥创建的帮助文档:https://help.github.com/en/github/authenticating-to-github/generati ...