题目链接:https://www.luogu.org/problemnew/show/P3916

题目大意

  略。

分析

  以终为始,逆向思维。

代码如下

 #include <bits/stdc++.h>
using namespace std; #define INIT() ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define Rep(i,n) for (int i = 0; i < (n); ++i)
#define For(i,s,t) for (int i = (s); i <= (t); ++i)
#define rFor(i,t,s) for (int i = (t); i >= (s); --i)
#define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i)
#define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i)
#define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
#define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i) #define pr(x) cout << #x << " = " << x << " "
#define prln(x) cout << #x << " = " << x << endl #define LOWBIT(x) ((x)&(-x)) #define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin()) #define ms0(a) memset(a,0,sizeof(a))
#define msI(a) memset(a,inf,sizeof(a))
#define msM(a) memset(a,-1,sizeof(a)) #define MP make_pair
#define PB push_back
#define ft first
#define sd second template<typename T1, typename T2>
istream &operator>>(istream &in, pair<T1, T2> &p) {
in >> p.first >> p.second;
return in;
} template<typename T>
istream &operator>>(istream &in, vector<T> &v) {
for (auto &x: v)
in >> x;
return in;
} template<typename T1, typename T2>
ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
out << "[" << p.first << ", " << p.second << "]" << "\n";
return out;
} inline int gc(){
static const int BUF = 1e7;
static char buf[BUF], *bg = buf + BUF, *ed = bg; if(bg == ed) fread(bg = buf, , BUF, stdin);
return *bg++;
} inline int ri(){
int x = , f = , c = gc();
for(; c<||c>; f = c=='-'?-:f, c=gc());
for(; c>&&c<; x = x* + c - , c=gc());
return x*f;
} typedef long long LL;
typedef unsigned long long uLL;
typedef pair< double, double > PDD;
typedef pair< int, int > PII;
typedef pair< int, PII > PIPII;
typedef pair< string, int > PSI;
typedef pair< int, PSI > PIPSI;
typedef set< int > SI;
typedef vector< int > VI;
typedef vector< VI > VVI;
typedef vector< PII > VPII;
typedef map< int, int > MII;
typedef map< int, PII > MIPII;
typedef map< string, int > MSI;
typedef multimap< int, int > MMII;
//typedef unordered_map< int, int > uMII;
typedef pair< LL, LL > PLL;
typedef vector< LL > VL;
typedef vector< VL > VVL;
typedef priority_queue< int > PQIMax;
typedef priority_queue< int, VI, greater< int > > PQIMin;
const double EPS = 1e-;
const LL inf = 0x7fffffff;
const LL infLL = 0x7fffffffffffffffLL;
const LL mod = 1e9 + ;
const int maxN = 1e5 + ;
const LL ONE = ;
const LL evenBits = 0xaaaaaaaaaaaaaaaa;
const LL oddBits = 0x5555555555555555; struct Edge{
int from, to;
}; struct Vertex{
VI edges;
}; int N, M;
int A[maxN];
bitset< maxN > vis;
Edge e[maxN];
int elen;
Vertex v[maxN]; void dfs(int x, int a) {
A[x] = a;
vis[x] = ;
foreach(i, v[x].edges) {
int y = e[*i].to;
if(!vis[y]) dfs(y, a);
}
} int main(){
//freopen("MyOutput.txt","w",stdout);
//freopen("input.txt","r",stdin);
INIT();
N = ri();
M = ri();
Rep(i, M) {
int x, y;
x = ri();
y = ri();
if(x == y) continue;
e[++elen].from = y;
e[elen].to = x;
v[y].edges.PB(elen);
} rFor(i, N, ) if(!vis[i]) dfs(i, i); For(i, , N) cout << A[i] << " ";
cout << endl;
return ;
}

洛谷P3916 图的遍历的更多相关文章

  1. Java实现 洛谷 P3916 图的遍历(反向DFS+记忆化搜索)

    P3916 图的遍历 输入输出样例 输入 4 3 1 2 2 4 4 3 输出 4 4 3 4 import java.io.BufferedReader; import java.io.IOExce ...

  2. 洛谷P3916 图的遍历 [图论,搜索]

    题目传送门 图的遍历 题目描述 给出 N 个点, M条边的有向图,对于每个点 v ,求 A(v) 表示从点 v 出发,能到达的编号最大的点. 输入输出格式 输入格式: 第1 行,2 个整数 N,M . ...

  3. 洛谷P3916||图的遍历||反向建图||链式前向星||dfs

    题目描述 给出 NN 个点, MM 条边的有向图,对于每个点 vv ,求 A(v)A(v) 表示从点 vv 出发,能到达的编号最大的点. 解题思路 看起来很简单的一道题, 但我依然调了一天,我还是太菜 ...

  4. 洛谷p3916图的遍历题解

    题面 思路: 反向建边,dfs艹咋想出来的啊 倒着遍历,如果你现在遍历到的这个点已经被标记了祖先是谁了 那么就continue掉 因为如果被标记了就说明前面已经遍历过了 而我们的顺序倒着来的 前边的一 ...

  5. 洛谷P3961 图的遍历

    题目来源 做这道题的方法不少. 在这里我只提一种 就是大法师. 可以采用反向建边,从最大的点开始dfs 我们考虑每次从所剩点中最大的一个点出发,我们暂且称它为i,而凡是i这个点所能到达的点,可以到达的 ...

  6. luogu P3916 图的遍历

    P3916 图的遍历 题目描述 给出 N 个点, M 条边的有向图,对于每个点 v ,求 A(v) 表示从点 v 出发,能到达的编号最大的点. 输入输出格式 输入格式: 第1 行,2 个整数 N,MN ...

  7. 【bfs】洛谷 P1443 马的遍历

    题目:P1443 马的遍历 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 记录一下第一道ac的bfs,原理是利用队列queue记录下一层的所有点,然后一层一层遍历: 其中: 1.p ...

  8. 洛谷 P3916 【图的遍历】反向加边+dfs

    前言: 对于这类带环的图,一般记忆化搜索不能很好的对所有遍历的边进行更新取值.因为环上的点可以相互到达,所以他们的答案因当是同步更新的,而dfs一旦你回溯完环上某个点就不会在更新这个点的答案了,做不到 ...

  9. 洛谷 P3916 【图的遍历】

    这道题绿题有点高了吧... 我一开始的思路就是一个暴力的遍历,用递归加一个记忆化,对于一个点不断的往下搜索,然后确定最大的,返回,给上面的节点.就在这个过程中,我们是搜到最大的数然后返回给上层的数,那 ...

随机推荐

  1. Github代码管理教程

    https://desktop.github.com/ 目录 Create and use a repository Start and manage a new branch Make change ...

  2. CF 600E. Lomsat gelral(dsu on tree)

    解题思路 \(dsu\) \(on\) \(tree\)的模板题.暴力而优雅的算法,轻儿子的信息暴力清空,重儿子的信息保留,时间复杂度\(O(nlogn)\) 代码 #include<iostr ...

  3. B/S 和 C/S 架构软件

    1.B/S架构: 通过C语言或java可以实现,使用B/S架构的软件,启动.打开应用和原生软件一样的效果. (正常浏览器打开的应用页面是有地址栏.菜单栏和标签栏的,但是通过配置可以关闭这些窗口,使B/ ...

  4. tornado的安装

      centos6.4 安装tornado框架连接工具  xshell 工具 1.如果在windows下有tornado的安装包首先现在上装到linux下的上传工具:yum install lrzsz ...

  5. Windows 7旗舰版产品密钥

    http://www.xitongcheng.com/jiaocheng/win7_article_45903.html 一.Windows 7旗舰版产品密钥 零售版: Windows 7 Ultim ...

  6. 3.2 Redux TodoApp

    上一节讲完了 redux 中的概念,但是仍然没有和 react 联系起来,这一节将利用 redux 在 react 中实现完整的 todolist: 在 react 使用 redux 通过 Provi ...

  7. MarkDown 快速开始 基础教学

    # MarkDown 快速上手 # > [源代码](https://www.cnblogs.com/qiyuexin/p/9932941.html) > by qyx@2018/11/07 ...

  8. SPOJ VFMUL - Very Fast Multiplication (FFT)

    题目链接:VFMUL - Very Fast Multiplication Description Multiply the given numbers. Input n [the number of ...

  9. HDU 5119 Happy Matt Friends (背包DP + 滚动数组)

    题目链接:HDU 5119 Problem Description Matt has N friends. They are playing a game together. Each of Matt ...

  10. npm和cnpm的安装(window)

    一:安装node.js 1.进入https://nodejs.org/en/中下载自己电脑相对应的node.js. 2.将下载下来的node.js进行安装. 3.利用管理员身份打开cmd,在里面输入n ...