Manthan, Codefest 18 (rated, Div. 1 + Div. 2) D. Valid BFS?

本文介绍了一种验证给定序列是否符合从特定顶点开始的树的BFS遍历的有效性的算法。通过模拟BFS过程并检查父节点顺序对子节点的影响来实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

The BFS algorithm is defined as follows.

Consider an undirected graph with vertices numbered from
1
to
n
. Initialize
q
as a new queue containing only vertex
1
, mark the vertex
1
as used.
Extract a vertex
v
from the head of the queue
q
.
Print the index of vertex
v
.
Iterate in arbitrary order through all such vertices
u
that
u
is a neighbor of
v
and is not marked yet as used. Mark the vertex
u
as used and insert it into the tail of the queue
q
.
If the queue is not empty, continue from step 2.
Otherwise finish.
Since the order of choosing neighbors of each vertex can vary, it turns out that there may be multiple sequences which BFS can print.

In this problem you need to check whether a given sequence corresponds to some valid BFS traversal of the given tree starting from vertex
1
. The tree is an undirected graph, such that there is exactly one simple path between any two vertices.

Input
The first line contains a single integer
n
(
1

n

2

10
5
) which denotes the number of nodes in the tree.

The following
n

1
lines describe the edges of the tree. Each of them contains two integers
x
and
y
(
1

x
,
y

n
) — the endpoints of the corresponding edge of the tree. It is guaranteed that the given graph is a tree.

The last line contains
n
distinct integers
a
1
,
a
2
,

,
a
n
(
1

a
i

n
) — the sequence to check.

Output
Print “Yes” (quotes for clarity) if the sequence corresponds to some valid BFS traversal of the given tree and “No” (quotes for clarity) otherwise.

You can print each letter in any case (upper or lower).

Examples
inputCopy
4
1 2
1 3
2 4
1 2 3 4
outputCopy
Yes
inputCopy
4
1 2
1 3
2 4
1 2 4 3
outputCopy
No
Note
Both sample tests have the same tree in them.

In this tree, there are two valid BFS orderings:

1
,
2
,
3
,
4
,
1
,
3
,
2
,
4
.
The ordering
1
,
2
,
4
,
3
doesn’t correspond to any valid BFS order.

bfs 模拟即可;
注意父亲节点的顺序影响子节点的顺序;

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#pragma GCC optimize("O3")
using namespace std;
#define maxn 300005
#define inf 0x3f3f3f3f
#define INF 0x7fffffff
typedef long long  ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9 + 7;
#define sq(x) (x)*(x)
#define eps 1e-6
const int N = 2500005;

inline int read()
{
    int x = 0, k = 1; char c = getchar();
    while (c < '0' || c > '9') { if (c == '-')k = -1; c = getchar(); }
    while (c >= '0' && c <= '9')x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
    return x * k;
}

ll gcd(ll a, ll b) {
    return b == 0 ? a : gcd(b, a%b);
}

int vis[maxn];
int a[maxn];
vector<int> g[maxn];
int P[maxn];
int pre[maxn];

void dfs(int x,int fa)
{
    pre[x]=fa;
    for(int i=0;i<g[x].size();i++){
        if(g[x][i]==fa)continue;
        dfs(g[x][i],x);
    }
}


int main()
{
    int n;n=read();
    int  i,j;
    for(i=0;i<n-1;i++){
       int u,v;u=read();v=read();
       g[u].push_back(v);g[v].push_back(u);
    }
    pre[1]=0;vis[0]=1;dfs(1,0);
    for(i=1;i<=n;i++)a[i]=read();
    if(a[1]!=1){
        cout<<"NO"<<endl;return 0;
    }
    vis[1]=1;P[1]=1;
    for(i=2;i<=n;i++){
        if(!vis[pre[a[i]]]){cout<<"NO"<<endl;return 0;}
        vis[a[i]]=1;P[a[i]]=i;
        if(P[pre[a[i]]]>=P[pre[a[i-1]]])continue;
        else {
            cout<<"NO"<<endl;return 0;
        }
    }
    cout<<"YES"<<endl;return 0;
}




内容概要:该论文探讨了一种基于粒子群优化(PSO)的STAR-RIS辅助NOMA无线通信网络优化方法。STAR-RIS作为一种新型可重构智能表面,能同时反射和传输信号,与传统仅能反射的RIS不同。结合NOMA技术,STAR-RIS可以提升覆盖范围、用户容量和频谱效率。针对STAR-RIS元素众多导致获取完整信道状态信息(CSI)开销大的问题,作者提出一种在不依赖完整CSI的情况下,联合优化功率分配、基站波束成形以及STAR-RIS的传输和反射波束成形向量的方法,以最大化总可实现速率并确保每个用户的最低速率要求。仿真结果显示,该方案优于STAR-RIS辅助的OMA系统。 适合人群:具备一定无线通信理论基础、对智能反射面技术和非正交多址接入技术感兴趣的科研人员和工程师。 使用场景及目标:①适用于希望深入了解STAR-RIS与NOMA结合的研究者;②为解决无线通信中频谱资源紧张、提高系统性能提供新的思路和技术手段;③帮助理解PSO算法在无线通信优化问题中的应用。 其他说明:文中提供了详细的Python代码实现,涵盖系统参数设置、信道建模、速率计算、目标函数定义、约束条件设定、主优化函数设计及结果可视化等环节,便于读者理解和复现实验结果。此外,文章还对比了PSO与其他优化算法(如DDPG)的区别,强调了PSO在不需要显式CSI估计方面的优势。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值