这道题就直接写一个 和 就可以了。
-
先判断当前这个点是否访问过,如果没有被访问过,就打标记,然后输出当前点,接着遍历当前点的邻接点,最后递归。
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 ;
}-
主函数
没啥好讲的,但是有一点要记住: 一定要排序一定要排序一定要排序一定要排序一定要排序一定要排序一定要排序一定要排序一定要排序一定要排序一定要排序一定要排序一定要排序一定要排序一定要排序一定要排序一定要排序!!!
#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;
}我们先初始化并查集,输入 个数,表示 对应的消息传递的对象。
然后每次 的时候统计一下是否有环,如果有环,就取 ,否则 就认它的传递对象为父节点。
-
环不可以反过来
因为每个点的父节点只有一个,而儿子节点可能不止一个,如果反过来,就会有多个父节点了。
#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;
}就是二叉树求后序遍历的板子……
他给了中序遍历和前序遍历,我们着重看前序遍历。
众所周知,前序遍历的第一个字母肯定是这棵树的父节点,而我们可以用这个父节点通过中序遍历找到这棵树的左右子树。
接着 中序遍历的左子树、前序遍历的左子树。
然后 中序遍历的右子树、前序遍历的右子树。
最后输出这棵树的父节点。
-
题目是先给的中序遍历,再给的前序遍历。
-
在 函数中,一定要先去 后面的子树,再输出当前树的父节点,这样才是后序遍历。
#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;
}我们通过分析样例来理解题目。
如果当前字符串长度为 1,说明不能继续分了,输出 (判断是 ‘I’ 串 还是 ‘F’ 串 还是 ‘B’ 串的函数)。
然后 前半串和后半串。
#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;
}我们把深度相同的树的权值相加,然后找权值和的最大值。
而求深度其实就是看当前高度是 。
最后输出深度权值和最大的深度。
#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;
}