首页 > 其他分享 >NOIP冲刺 【图论复习】 图的遍历

NOIP冲刺 【图论复习】 图的遍历

时间:2022-09-30 12:46:06浏览次数:64  
标签:图论 ch NOIP int long re 遍历 include define

还有两个月就NOIP了我居然还在敲这种东西.........

洛谷 P5318 DFS BFS 模版题

复习一下 DFS:

从第一个节点开始搜 用vis数组记忆化 搜到每一个点时 如果没搜过 就把他标记 并搜他所能到达的节点 直至不能再搜为止 复杂度O(V+E) 最坏复杂度Q(n!) ;

复习一下BFS:

从第一个节点开始搜 用vis数组记忆化 同时开一个队列 先将第一个节点入队 然后将他的每一个能到达的点都入队 在队列中进行搜索 如果队首没有搜过 就标记 然后重复上述步骤存入所能到达的节点 然后将队首出队 直至队列为空

 

题很简单 就是板子 注意排序 (然而是本蒟蒻第一次用邻接表存图){

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cmath>
 4 #include <cstdio>
 5 #include <climits>
 6 #include <cassert>
 7 #include <cstring>
 8 #include <cstdlib>
 9 #include <cctype>
10 #include <iomanip>
11 #include <utility>
12 #include <set>
13 #include <map>
14 #include <list>
15 #include <stack>
16 #include <queue>
17 #include <deque>
18 #include <vector>
19 #include <bitset>
20 #include <ctime>
21 #define int long long
22 #define ll long long
23 #define ull unsigned long long
24 #define re register
25 #define mod1 998244353
26 #define mod2 100000007
27 #define lowbit(x) x & (-x)
28 #define gcd(a,b) __gcd(a,b)
29 #define mid ((l + r) >> 1)
30 #define rep(i,a,b)  for(re int i(a);i <= b;++ i)
31 #define rrep(i,a,b) for(re int i(a);i >= b;i --)
32 using namespace std;
33 inline int read() {
34     re int x = 0,f = 1;
35     re char ch = getchar();
36     while(ch < '0' || ch > '9') {
37         if(ch == '-') f = -1;
38         ch = getchar();
39     }
40     while(ch >= '0' && ch <= '9') {
41         x = (x << 3) + (x << 1) + (ch ^ 48);
42         ch = getchar();
43     }
44     return x * f;
45 }
46 //#define OJ
47 //#define debug
48 const int M = 1e5 + 1;
49 vector<int> v[M];
50 bool vis[M];
51 queue<int> q;
52 inline void dfs(int u) {
53     vis[u] = 1;
54     cout << u << " ";
55     rep(i,0,v[u].size() - 1) {
56         if(v[u].size() == 0) break;
57         if(v[u][i]) {
58             if(!vis[v[u][i]]) dfs(v[u][i]);
59         }
60     }
61 }
62 inline void bfs(int u) {
63     q.push(u);
64     vis[u] = 1;
65     cout << u << " ";
66     while(!q.empty()) {
67         int t = q.front();
68         rep(i,0,v[t].size() - 1) {
69             if(v[t].size() == 0) break;
70             if(v[t][i]) {
71                 if(!vis[v[t][i]]) q.push(v[t][i]),cout << v[t][i] << " ",vis[v[t][i]] = 1;
72             }
73         }
74         q.pop();
75     }
76 }
77 signed main() {
78 #ifdef OJ
79     freopen(".in","r",stdin);
80     freopen(".out","w",stdout);
81 #endif
82     int n = read();
83     int m = read();
84     rep(i,1,m) {
85         re int x = read();
86         re int y = read();
87         v[x].push_back(y);
88     }
89     rep(i,1,n) sort(v[i].begin(),v[i].end());
90     dfs(1);
91     cout << endl;
92     memset(vis,false,sizeof(vis));
93     bfs(1);
94 #ifdef debug
95     printf("\nTime used = %lf", (double)clock() / CLOCKS_PER_SEC);
96 #endif
97     return 0;
98 }

Cause‘ nothing lasts forever, but cold november rain..

标签:图论,ch,NOIP,int,long,re,遍历,include,define
From: https://www.cnblogs.com/Eruption/p/16744551.html

相关文章

  • CSP-S 2022进不去NOIP记
    初赛Day0过了。9.26day-OP414报了qbxt腾飞的线上。9.28day-NP414明天放假!9.30day-ZP414这才过了一半,事倒是很多...早上五点多爬起来打游戏,打完之后看手机突......
  • ES6形式常用的数组遍历函数
    文章目录​​0.给定一个数组​​​​1.find():查找成员对象​​​​2.findIndex():查找成员下标​​​​3.filter():过滤数组​​​​4.forEach():迭代数组​​​​5.some......
  • P2671 [NOIP2015 普及组] 求和(洛谷前缀和
    P2671[NOIP2015普及组]求和1//(x+z)*(num[x]+num[z])=2//(x1+x2)*(y1+y2)+(x1+x3)*(y1+y3)+(x2+x3)*(y2+y3)3//=x1*(y1*(n-2)+y1+y2+...+yn)4//+x2*(y2*......
  • Android 如何遍历容器(布局)下的所有控件(节点/组件)?
    通过上图可知,Android的页面是由多个ViewGroup和View构成,其中ViewGroup包含许多View和ViewGroup。View称之为“微件”,也可以说是组件、节点、控件。ViewGroup......
  • P1040 [NOIP2003 提高组] 加分二叉树
    区间dp好题!在更新\(f[i][j]\)时,顺便记录该子树的根节点\(g[i][j]\)。最后递归求解。#include<bits/stdc++.h>usingnamespacestd;classsolve{ public: int......
  • vue 遍历图片渲染
    原文链接:https://blog.csdn.net/sywdebug/article/details/120763271举例说明获取目录下的文件名新创建一个vue项目,获取views目录下的以.vue结尾的文件的文件名mounted......
  • P3960 NOIP2017 提高组 列队
    P3960NOIP2017提高组列队将每一行的第1到m-1个和第m列分离出来分析知这n+1个“区间”要维护弹出第k个和插入最后使用平衡树,一个区间若没有被算则用[l,r]表示(方伯伯......
  • P1600 [NOIP2016 提高组] 天天爱跑步
    P1600NOIP2016提高组天天爱跑步LCA+桶点击查看代码///*考虑上行的情况(u,v)中u被i看到<=>1.u∈{i的子树} 2.lca(u,v)不属于{i的子树} 3.de......
  • 二叉树的前中后序遍历的两种方法
    前中后序遍历的记忆方式:前中后可以记为中间节点的顺序位置,如:前序遍历:中左右;中序遍历:左中右;后续遍历:左右中。//前序遍历:算法实现:前序遍历顺序为中左右。需要传......
  • 二叉树前中后序遍历的两种方式
    前中后序遍历的记忆方式:前中后可以记为中间节点的顺序位置,如:前序遍历:中左右;中序遍历:左中右;后续遍历:左右中。//前序遍历:算法实现:前序遍历顺序为中左右。需要传......