3-30 图论小测总结
Moonlight_dreams · · 个人记录
T1
P5318 【深基18.例3】查找文献
思路
这道题就直接写一个
-
dfs 函数 先判断当前这个点是否访问过,如果没有被访问过,就打标记,然后输出当前点,接着遍历当前点的邻接点,最后递归。
void dfs(int cur)
{
if (vis[cur] == 1)
{
return ;
}
vis[cur] = 1;
cout << cur << " ";
for(int nxt : g[cur])
{
dfs(nxt);
}
return ;
}
-
bfs 函数 先清空
vis 数组,因为这个数组已经在dfs 函数 中用过了。然后打标记、输出当前这个点、遍历邻接点。
void bfs(int step)
{
queue<int> qu;
memset(vis , 0 , sizeof vis);
qu.push(step);
vis[step] = 1;
while (qu.empty() == 0)
{
int cur = qu.front();
qu.pop();
cout << cur << " ";
for (int nxt : g[cur])
{
if(vis[nxt] == 0)
{
vis[nxt] = 1;
qu.push(nxt);
}
}
}
return ;
}
-
主函数
没啥好讲的,但是有一点要记住: 一定要排序一定要排序一定要排序一定要排序一定要排序一定要排序一定要排序一定要排序一定要排序一定要排序一定要排序一定要排序一定要排序一定要排序一定要排序一定要排序一定要排序!!!
AC 代码如下
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
vector <int> g[N];
int n , m;
bool vis[N];
void dfs(int cur)
{
if(vis[cur] == 1)
return ;
vis[cur] = 1;
cout << cur << " ";
for(int nxt : g[cur])
dfs(nxt);
return ;
}
void bfs(int step)
{
queue<int> qu;
memset(vis , 0 , sizeof vis);
qu.push(step);
vis[step] = 1;
while (qu.empty() == 0)
{
int cur = qu.front();
qu.pop();
cout << cur << " ";
for (int nxt : g[cur])
{
if(vis[nxt] == 0)
{
vis[nxt] = 1;
qu.push(nxt);
}
}
}
return ;
}
signed main()
{
cin >> n >> m;
for(int i = 1; i <= m; i++)
{
int x, y;
cin >> x >> y;
g[x].push_back(y);
}
for(int i = 1; i <= n; i++)
sort(g[i].begin(), g[i].end());
dfs(1);
cout << endl;
bfs(1);
return 0;
}
T3
P2661 [NOIP 2015 提高组] 信息传递
思路
我们先初始化并查集,输入
然后每次
注意:
-
环不可以反过来
因为每个点的父节点只有一个,而儿子节点可能不止一个,如果反过来,就会有多个父节点了。
AC 代码如下
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2e5 + 5;
int fa[N];
int cnt = 0;
int get(int x)
{
cnt++;
if (fa[x] == x)
{
return x;
}
return get(fa[x]);
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for (int i = 1; i <= n; i++)
{
fa[i] = i;
}
int minn = INT_MAX;
for (int i = 1; i <= n; i++)
{
int x;
cin >> x;
cnt = 0;
if (get(x) == i)
{
minn = min(minn , cnt);
}
else
{
fa[i] = x;
}
}
cout << minn;
return 0;
}
T4
P1827 [USACO3.4] 美国血统 American Heritage
思路
就是二叉树求后序遍历的板子……
dfs 函数
他给了中序遍历和前序遍历,我们着重看前序遍历。
众所周知,前序遍历的第一个字母肯定是这棵树的父节点,而我们可以用这个父节点通过中序遍历找到这棵树的左右子树。
接着
然后
最后输出这棵树的父节点。
注意:
-
题目是先给的中序遍历,再给的前序遍历。
-
在
dfs 函数中,一定要先去dfs 后面的子树,再输出当前树的父节点,这样才是后序遍历。
AC 代码如下
#include <bits/stdc++.h>
#define int long long
using namespace std;
void dfs(string s , string t)
{
if (t.size() == 0)
{
return ;
}
char root = t[0];
int pos = s.find(root);
dfs(s.substr(0 , pos) , t.substr(1 , pos));
dfs(s.substr(pos + 1) , t.substr(pos + 1));
cout << root;
return ;
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
string s , t;
cin >> s >> t;
dfs(s , t);
return 0;
}
T5
P1087 [NOIP 2004 普及组] FBI 树
思路
我们通过分析样例来理解题目。
dfs 函数
如果当前字符串长度为 1,说明不能继续分了,输出
然后
AC 代码如下
#include <bits/stdc++.h>
#define int long long
using namespace std;
char getfbi(string s)
{
int cnt = 0;
int lens = s.size();
for (int i = 0; i < lens; i++)
{
if (s[i] == '0')
{
cnt++;
}
}
if (cnt == 0)
{
return 'I';
}
if (cnt == lens)
{
return 'B';
}
return 'F';
}
void dfs(string s)
{
int lens = s.size();
if (lens == 1)
{
cout << getfbi(s);
return ;
}
dfs(s.substr(0 , lens / 2));
dfs(s.substr(lens / 2));
cout << getfbi(s) ;
return ;
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
string s;
cin >> n >> s;
dfs(s);
return 0;
}
T6
P8681 [蓝桥杯 2019 省 AB] 完全二叉树的权值
思路
我们把深度相同的树的权值相加,然后找权值和的最大值。
而求深度其实就是看当前高度是
最后输出深度权值和最大的深度。
AC 代码如下
#include<bits/stdc++.h>
#define int long long
using namespace std;
int a[100005];
signed main()
{
int n;
cin>>n;
for(int i=1;i<=n;++i)
{
cin>>a[i];
}
int cnt = 0 , ans = 0;
int cnt2 = 1;
int id = 0;
int maxn = INT_MIN;
int nn = 1;
int sum = 1;
for (int i = 1; i <= n; i++)
{
cnt++;
ans += a[i];
if (cnt == sum)
{
if (ans > maxn)
{
id = cnt2;
maxn = ans;
}
cnt2++;
sum += nn * 2;
nn *= 2;
ans = 0;
}
}
if (ans > maxn)
{
id = cnt2;
maxn = ans;
}
cout << id;
return 0;
}