似乎我搞得太复杂了?

先tarjan缩点然后dfs就行了QAQ。

(我不说我被一个sb错调了半个小时。。。。不要以为缩点后dfs就可以肆无忌惮的不加特判判vis了。。

bfs的做法:减反图,然后从大到小枚举(贪心),标记即可

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <vector>
#include <map>
using namespace std;
typedef long long ll;
#define pii pair<int, int>
#define mkpii make_pair<int, int>
#define pdi pair<double, int>
#define mkpdi make_pair<double, int>
#define pli pair<ll, int>
#define mkpli make_pair<ll, int>
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << (#x) << " = " << (x) << endl
#define error(x) (!(x)?puts("error"):0)
#define printarr2(a, b, c) for1(_, 1, b) { for1(__, 1, c) cout << a[_][__]; cout << endl; }
#define printarr1(a, b) for1(_, 1, b) cout << a[_] << '\t'; cout << endl
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; } const int N=1e5+10;
int m, mx[N], LL[N], FF[N], vis[N], tot, scc, st[N], fa[N], top;
struct Gr {
int ihead[N], cnt, n;
struct dat { int next, to; }e[N];
void add(int u, int v) { e[++cnt].next=ihead[u]; ihead[u]=cnt; e[cnt].to=v; }
void tarjan() { for1(i, 1, n) if(!FF[i]) tarjan(i); }
void tarjan(int x) {
LL[x]=FF[x]=++tot; vis[x]=1; st[++top]=x;
for(int i=ihead[x]; i; i=e[i].next) {
int y=e[i].to;
if(!FF[y]) {
tarjan(y);
LL[x]=min(LL[x], LL[y]);
}
else if(vis[y] && FF[y]<LL[x]) LL[x]=FF[y];
}
if(FF[x]==LL[x]) {
++scc;
int y;
do {
y=st[top--];
vis[y]=0;
fa[y]=scc;
mx[scc]=max(mx[scc], y);
} while(y!=x);
}
}
void dfs() { CC(vis, 0); for1(i, 1, n) if(!vis[i]) dfs(i); }
void dfs(int x) {
if(vis[x]) return; //这个我就不说了,坑。。。
vis[x]=1;
for(int i=ihead[x]; i; i=e[i].next) {
dfs(e[i].to);
mx[x]=max(mx[x], mx[e[i].to]);
}
}
}G, g; int main() {
read(G.n); read(m);
for1(i, 1, m) { int u=getint(), v=getint(); G.add(u, v); }
G.tarjan();
g.n=scc;
for1(x, 1, G.n) for(int i=G.ihead[x]; i; i=G.e[i].next) if(fa[x]!=fa[G.e[i].to]) g.add(fa[x], fa[G.e[i].to]);
g.dfs();
for1(i, 1, G.n) printf("%d ", mx[fa[i]]);
return 0;
}

  


【题目描述】

给出 N 个点,M 条边的有向图,对于每个点 v,求 A(v) 表示从点 v 出发,能到达的编号最大的点。

【输入格式】

第 1 行,2 个整数 N,M。 接下来 M 行,每行 2 个整数 Ui,Vi,表示边 ⟨Ui,Vi⟩。点用 1,2,...,N 编号。

【输出格式】

N 个整数 A(1),A(2),...,A(N)。

【样例输入】

4 3

1 2

2 4

4 3

【样例输出】

4 4 3 4

【数据范围】

对于 60% 的数据,1 ≤ N,K ≤ 10^3

对于 100% 的数据,1 ≤ N,M ≤ 10^5。

【NOIP模拟题】Graph(tarjan+dfs)的更多相关文章

  1. NOIP 模拟 路径求和 - Tarjan+dfs+码

    题目大意: 各一个奇环内向森林,求每两个点对间的距离之和.无法到达则距离为-1. 分析: 首先Tarjan找出size大于1的连通分量(环),环中的边的贡献可以单独计算. 然后从入度为0的点向内dfs ...

  2. NOIP模拟题汇总(加厚版)

    \(NOIP\)模拟题汇总(加厚版) T1 string 描述 有一个仅由 '0' 和 '1' 组成的字符串 \(A\),可以对其执行下列两个操作: 删除 \(A\)中的第一个字符: 若 \(A\)中 ...

  3. 8.22 NOIP 模拟题

      8.22 NOIP 模拟题 编译命令 g++ -o * *.cpp gcc -o * *.c fpc *.pas 编译器版本 g++/gcc fpc 评测环境 位 Linux, .3GHZ CPU ...

  4. 【入门OJ】2003: [Noip模拟题]寻找羔羊

    这里可以复制样例: 样例输入: agnusbgnus 样例输出: 6 这里是链接:[入门OJ]2003: [Noip模拟题]寻找羔羊 这里是题解: 题目是求子串个数,且要求简单去重. 对于一个例子(a ...

  5. 9.9 NOIP模拟题

    9.9 NOIP模拟题 T1 两个圆的面积求并 /* 计算圆的面积并 多个圆要用辛普森积分解决 这里只有两个,模拟计算就好 两圆相交时,面积并等于中间两个扇形面积减去两个三角形面积 余弦定理求角度,算 ...

  6. noip模拟题题解集

    最近做模拟题看到一些好的题及题解. 升格思想: 核电站问题 一个核电站有N个放核物质的坑,坑排列在一条直线上.如果连续M个坑中放入核物质,则会发生爆炸,于是,在某些坑中可能不放核物质. 任务:对于给定 ...

  7. NOIP模拟题17.9.26

    B 君的任务(task)[题目描述]与君初相识,犹如故人归.B 君看到了Z 君的第一题,觉得很难.于是自己出了一个简单题.你需要完成n 个任务,第i 任务有2 个属性ai; bi.其中ai 是完成这个 ...

  8. noip模拟题 2017.10.28 -kmp -Tarjan -鬼畜的优化

    题目大意 给定A串,选择A串的前lB个字符作为B串,再在B串后增加一个字符,问最长的相等的A串前缀和B串的后缀. Solution 1(KMP) 用1个奇怪的字符连接A串和B串,再用KMP求最长公共前 ...

  9. 6.19 noip模拟题(题目及解析转自 hzwer 2014-3-15 NOIP模拟赛)

    Problem 1 高级打字机(type.cpp/c/pas) [题目描述] 早苗入手了最新的高级打字机.最新款自然有着与以往不同的功能,那就是它具备撤销功能,厉害吧. 请为这种高级打字机设计一个程序 ...

随机推荐

  1. Sublime key

    -– BEGIN LICENSE -– TwitterInc 200 User License EA7E-890007 1D77F72E 390CDD93 4DCBA022 FAF60790 61AA ...

  2. PHP 通过LDAP协议,操作Windows Active Directory

    原文地址:http://hi.baidu.com/lllangxx/item/3ccb7cdfa13b56eb3dc2cb39 一.学习如何管理Active Directory Active Dire ...

  3. spring mvc 返回json的配置

    转载自:http://my.oschina.net/haopeng/blog/324934 springMVC-servlet.xml 配置 1 2 3 4 5 6 7 8 9 10 11 12 13 ...

  4. (三)hibernate单表操作

    0. 贴上节hbm文件 <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hib ...

  5. 转 多线程 闭锁(Latch) 栅栏(CyclicBarrier)

    java多线程并发系列之闭锁(Latch)和栅栏(CyclicBarrier) 标签: java并发编程 2015-05-28 16:45 2939人阅读 评论(0) 收藏 举报 本文章已收录于: . ...

  6. Python 算法(1) 快速排序

    快速排序(quickSort) 快排的思想:首先任意选取一个数据(通常选用数组的第一个数)作为关键数据,然后将所有比它小的数都放到它前面,所有比它大的数都放到它后面,这个过程称为一趟快速排序. 百度百 ...

  7. spring in action 7.2 小结

    1 对于二进制文件上传功能的实现.在spring中使用multipart来处理,处理方式有两种. CommonsMultipartResolver:使用Jakarta Commons FileUplo ...

  8. spring in action 5.1 小结 spring mvc起步

    0 配置 DispatcherServlet 是 spring mvc的核心,常规配置方法可以查看之前博客.springMVC简单例子 在此使用servlet 3 规范和 spring3.1 功能增强 ...

  9. java开发中国际化

    1 静态文本的国际化,就是比如页面中中文显示用户名就是用户名,用于显示就是 username. 其中静态文件命名遵循:基础名_语言简称_国家简称.properties 需要使用的类是 1)import ...

  10. mysql string types ---- mysql 字符类型详解

    一.mysql 中包涵的字符类型: [national] char [(m)] [character set charset_name] [collate collation_name] [natio ...