CF1062F Upgrading Cities
题意
由于这是个\(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的更多相关文章
- [CF1062F]Upgrading Cities[拓扑排序]
题意 一张 \(n\) 点 \(m\) 边的 \(DAG\) ,问有多少个点满足最多存在一个点不能够到它或者它不能到. \(n,m\leq 3\times 10^5\) 分析 考虑拓扑排序,如果 \( ...
- 一句话题解&&总结
CF79D Password: 差分.两点取反,本质是匹配!最短路+状压DP 取反是套路,匹配是发现可以把操作进行目的化和阶段化,从而第二次转化问题. 且匹配不会影响别的位置答案 sequence 计 ...
- Codeforces Round #520 (Div. 2) Solution
A. A Prank Solved. 题意: 给出一串数字,每个数字的范围是$[1, 1000]$,并且这个序列是递增的,求最多擦除掉多少个数字,使得别人一看就知道缺的数字是什么. 思路: 显然,如果 ...
- CodeForces round 520 div2
A:A Prank 题意:给定一个递增序列, 问最多能删除多少个连续数字,要求删除数字之后能还原成原来的数列. 题解:直接找就好了,为了方便可以使得第0个数字为0, 第n+1个元素为1001 代码: ...
- Connect the Cities[HDU3371]
Connect the Cities Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- codeforces 613D:Kingdom and its Cities
Description Meanwhile, the kingdom of K is getting ready for the marriage of the King's daughter. Ho ...
- CF449B Jzzhu and Cities (最短路)
CF449B CF450D http://codeforces.com/contest/450/problem/D http://codeforces.com/contest/449/problem/ ...
- 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 ...
- Installation and Upgrading
Cumulative Feature Overview Identifies product features available to you when upgrading. This tool r ...
随机推荐
- 上手spring boot项目(三)之spring boot整合mybatis进行增删改查
使用mybatis框架进行增删改查大致有两种基础方式,一种扩展方式.两种基础方式分别是使用xml映射文件和使用方法注解.扩展方式是使用mybatis-plus的方式,其用法类似于spring-data ...
- linux—chown
1 .修改 /usr/local下bin目录的所属者 2.修改 /usr/local下bin目录的所属组 3.修改 /usr/local下games目录的所属者和所属组 4.修改 /usr/local ...
- 【算法】331- JS洗牌算法
点击上方"前端自习课"关注,学习起来~ 最近的一个塔罗牌项目中,有一个洗牌的需求,其实也就是随机打乱数组,遂网上搜了下,再此做个整理- 塔罗牌 举例来说,我们有一个如下图所示的数组 ...
- Java关于赋值
下面的程序有问题吗?如果有问题说明一下, 如果没有问题猜测一下运行结果: public static void main(String[] args){ short s = 1; s=s+1; Sys ...
- 12个超好用的IntelliJ IDEA 插件!你用过几个?
一.前言 IntelliJ IDEA 如果说IntelliJ IDEA是一款现代化智能开发工具的话,Eclipse则称得上是石器时代的东西了. 其实笔者也是一枚从Eclipse转IDEA的探索者,随着 ...
- 2016/09/29 SQL中的join
1.建表 首先建tb_a并插入数据. )); insertinto tb_a(symbol, sname) values ('A','B'); insertinto tb_a(symbol, snam ...
- 微服务架构 SpringBoot(一)
spring Boot:官网地址 https://spring.io/ 由来: 随着spring组件功能的强大,配置文件也越来越复杂繁琐,背离了spring公司的简洁快速开发原理,2015年就推出Sp ...
- 小白的springboot之路(十二)、集成log4j2日志
0.前言 日志记录对系统来说必不可少,spring boot中常用的日志组件有log4j.logback.log4j2,其中logback是spring boot默认的,已自带:选用log4j2就可以 ...
- 高精度模板 val.1
目录 高精构造 结构体 char数组转高精: 高精加高精 高精乘单精 高精除单精 同样搬以前初三写的... 其实还有个val.2,搬到文章里去了 @ 在做一道斯特林数的时候被卡高精...于是滚来写一些 ...
- Java题库——Chapter17 二进制I/0
Introduction to Java Programming 的最后一章,完结撒花!Chapter 17 Binary I/O Section 17.2 How is I/O Handled in ...