一、题目描述:
给你一个
每走过一个新的点,你的能力值会
你只能经过点权小于等于你能力值的点。每条边,每个点都可以经过无限次,问能否走遍整个图。
如果可以,输出
二、做题思路:
很明显,你只能选择点权为
考虑优化,之前已经访问过的点再用作起点肯定不优(这里不做解释)。用一个
每次
现在时间复杂度已经是
假设一开始枚举的点是中间那个
如果重复拓展到这几个点,那么一定是经过了点权较大的点,此时经过的点 (蓝点) 一定大于
由此可以发现,总拓展点数最大为
三、完整代码:
1 #include<iostream> 2 #include<cstring> 3 #include<queue> 4 #define N 200010 5 using namespace std; 6 int T,n,m,u1,v1,now,c[N],vis[N]; 7 struct EDGE{ 8 int v,nxt; 9 }edge[N*2]; 10 int head[N],cnt; 11 void add(int u,int v) 12 { 13 edge[++cnt].v=v; 14 edge[cnt].nxt=head[u]; 15 head[u]=cnt; 16 } 17 struct node{ 18 int pos,dis; 19 bool operator < (const node &t)const{ 20 return t.dis<dis; 21 } 22 }; 23 priority_queue <node> q; 24 int dij(int s) 25 { 26 while(!q.empty()) 27 q.pop();q.push({s,0});now=0; 28 while(!q.empty()) 29 { 30 int u=q.top().pos;q.pop(); 31 if(vis[u]==s) continue;vis[u]=s; 32 if(now<c[u]) return 0; now++; 33 for(int i=head[u];i!=-1;i=edge[i].nxt) 34 q.push({edge[i].v,c[edge[i].v]}); 35 } 36 return now==n; 37 } 38 void solve() 39 { 40 cin>>n>>m;cnt=0; 41 for(int i=1;i<=n;i++) 42 { 43 cin>>c[i]; 44 head[i]=-1,vis[i]=0; 45 } 46 for(int i=1;i<=m;i++) 47 { 48 cin>>u1>>v1; 49 add(u1,v1),add(v1,u1); 50 } 51 for(int i=1;i<=n;i++) 52 if(!c[i]&&!vis[i]&&dij(i)) 53 { 54 cout<<"YES"<<'\n'; 55 return ; 56 } 57 cout<<"NO"<<'\n'; 58 } 59 int main() 60 { 61 ios::sync_with_stdio(false); 62 cin.tie(0);cout.tie(0); 63 cin>>T; 64 for(int i=1;i<=T;i++) 65 solve(); 66 return 0; 67 }
四、写题心得:
这个题写起来不难,就是时间复杂度难以证明。不过自己花了这么多时间证出来了(头一次配了图 ! ),也是真的很高兴。拜拜!
标签:
Codeforces
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探