跳到正文

个人记录

3-30 图论小测总结

Moonlight_dreams约 10 分钟
原文
文章信息与版本

文章信息

发布
2025年3月31日
收录
2025年12月12日
更新
2026年11月15日
篇幅
约 10 分钟

版本

  1. v1当前

T1T1

P5318【深基18.例3】查找文献P5318 【深基18.例3】查找文献

思路思路

这道题就直接写一个 dfsdfs 和 bfsbfs 就可以了。

  • dfsdfs 函数函数

    先判断当前这个点是否访问过,如果没有被访问过,就打标记,然后输出当前点,接着遍历当前点的邻接点,最后递归。

C++
void dfs(int cur)
{
	if (vis[cur] == 1)
    {
        return ;
    }
	vis[cur] = 1;
	cout << cur << " "; 
	for(int nxt : g[cur])
    {
		dfs(nxt); 
    }
	return ;
}
  • bfsbfs 函数函数

    先清空 visvis 数组,因为这个数组已经在 dfsdfs 函数函数 中用过了。

    然后打标记、输出当前这个点、遍历邻接点。

C++
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代码如下AC 代码如下

C++
#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;
}

T3T3

P2661[NOIP2015提高组]信息传递P2661 [NOIP 2015 提高组] 信息传递

思路思路

我们先初始化并查集,输入 nn 个数,表示 ii 对应的消息传递的对象。

然后每次 findfind 的时候统计一下是否有环,如果有环,就取 MINMIN,否则 ii 就认它的传递对象为父节点。

注意:注意:
  • 环不可以反过来

    因为每个点的父节点只有一个,而儿子节点可能不止一个,如果反过来,就会有多个父节点了。

AC代码如下AC 代码如下

C++
#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;
}

T4T4

P1827[USACO3.4]美国血统AmericanHeritageP1827 [USACO3.4] 美国血统 American Heritage

思路思路

就是二叉树求后序遍历的板子……

dfsdfs 函数函数

他给了中序遍历和前序遍历,我们着重看前序遍历。

众所周知,前序遍历的第一个字母肯定是这棵树的父节点,而我们可以用这个父节点通过中序遍历找到这棵树的左右子树。

接着 dfsdfs 中序遍历的左子树、前序遍历的左子树。

然后 dfsdfs 中序遍历的右子树、前序遍历的右子树。

最后输出这棵树的父节点。

注意:注意:
  • 题目是先给的中序遍历,再给的前序遍历。

  • 在 dfsdfs 函数中,一定要先去 dfsdfs 后面的子树,再输出当前树的父节点,这样才是后序遍历。

AC代码如下AC 代码如下

C++
#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;
}

T5T5

P1087[NOIP2004普及组]FBI树P1087 [NOIP 2004 普及组] FBI 树

思路思路

我们通过分析样例来理解题目。

dfsdfs 函数函数

如果当前字符串长度为 1,说明不能继续分了,输出 getfbi(s)getfbi(s)(判断是 ‘I’ 串 还是 ‘F’ 串 还是 ‘B’ 串的函数)。

然后 dfsdfs 前半串和后半串。

AC代码如下AC 代码如下

C++
#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;
}

T6T6

P8681[蓝桥杯2019省AB]完全二叉树的权值P8681 [蓝桥杯 2019 省 AB] 完全二叉树的权值

思路思路

我们把深度相同的树的权值相加,然后找权值和的最大值。

而求深度其实就是看当前高度是 2?+12^? + 1。

最后输出深度权值和最大的深度。

AC代码如下AC 代码如下

C++
#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;
}
原文