当前位置: 首页 > news >正文

B. Lawyers 题解

[ B. Lawyers ]( Problem - B - Codeforces )

写的不好凑合看qwq有问题私信或评论
单向边都可以连,双向边连一条即可。

我用的并查集做的,遍历所有边的时候如果是双向边就 join 连起来,然后如果两个点已经被连起来了,就说明出现了一个环,将这个环的祖宗节点标记1;如果是单向边就直接将B的祖宗节点标记1,其实整个问题就是将每个连通分量的祖宗节点标记为1,然后去找祖宗节点是否全被标记过。

代码

#include <bits/stdc++.h>
using namespace std;
//-------------------------------------------------------------------------------------------
#define int long long 
#define lost_R ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define P pair<int,int>
#define lowbit(x) (x&(-x))
#define dbg1(x) cout<<"# "<<x<<endl
#define dbg2(x,y) cout<<"# "<<x<<" "<<y<<endl
#define endl '\n'
const int mod=998244353;
const int N=2e5+10;
const int INF=0x3f3f3f3f3f3f3f3f;
const int inf=0x3f3f3f3f;
using ar3=array<int,3>;
using ar2=array<int,2>;
//--------------------------------------------------------------------------------------
int n,m;
int f[N], alive[N];
vector<P> g;
unordered_map<int, bool> mp;void init(int n){for(int i=1;i<=n;i++) f[i]=i, alive[i]=0;
}int find(int x){return f[x]==x?x:f[x]=find(f[x]);
}void merge(int x, int y){x=find(x), y=find(y);if(x==y){  //成环alive[x]=1;return;}f[x]=y;alive[y]|=alive[x]; //祖宗节点之间的继承
}void solve(){cin>>n>>m;if(n<=2){cout<<"NO"<<endl;return;}init(n);g.clear();mp.clear();auto hash_edge = [&](int x, int y){return x*(n+1)+y;};for(int i=1;i<=m;i++){int a,b;cin>>a>>b;g.push_back({a,b});mp[hash_edge(a,b)]=true;}for(auto [u,v]:g){if(mp.count(hash_edge(v,u))){if(u<=v) merge(u,v); //确保双向连只连一条}else{alive[find(v)]=1;}}for(int i=1;i<=n;i++){if(!alive[find(i)]){cout<<"NO"<<endl;return;}}cout<<"YES"<<endl;
}signed main(){lost_R;int T=1;//cin>>T;for(int i=1;i<=T;i++){solve();}return 0;
}
http://www.sczhlp.com/news/3834/

相关文章:

  • 攻防世界 ics-05(工具秒杀版)
  • USB主机进入睡眠/休眠带动USB从机进入睡眠
  • Redis地理散列GeoHash
  • 树06
  • aadio打开文件对话框
  • 【自学嵌入式:51单片机】用I2C通信读写AT24C02芯片,用串口通信获取AT24C02芯片存储的数据
  • go学习笔记:gorm 语法中加Model与不加Model,有什么差异?
  • Flutter3-MacOS桌面OS系统|flutter3.32+window_manager客户端OS模板
  • 很无语的一件事
  • (笔记)博弈论 公平组合游戏
  • Linguistics-English-Psychology-Charactetistics words: 性格类单词 + Applied-Grammar.com:
  • 最近Vibe Coding的经验总结
  • aardio加载窗口并显示
  • 深度学习归一化技术全景解析:原理、对比与应用建议
  • css-clamp 响应式布局
  • 关于组合数的几点推论
  • 【译】现场编程糟透了
  • 智能生成git提交消息工具 GIM 发布 1.7 版本了
  • About Me
  • 体育直播系统搭建:核心数据详细接入指南
  • 2025.7.31 Codeforces Round 1040 (Div. 1)
  • 利用ssh反向代理使服务器能够访问外网
  • 100天掌握YARA:如何编写.NET代码特征规则
  • 通过胜率理解偏好学习的理论与优化方法
  • go学习笔记:Go 语言中的 fmt.Sprintf的用法
  • 机器学习概念梳理
  • 对表修改
  • HCl 的海南旅行日记——Day 2
  • Bash 倒计时关机脚本
  • 图像生成-FUDUKI解读-再谈Metric-induced Probability Paths -23 - jack