给一些单词,它们可能是同义或者反义,给出一些关系定义,从前面的定义开始建立关系,如果有的关系定义和之前的冲突输出NO,否则输出YES。
然后查询q次单词x和单词y的关系。
扩展域并查集
1~n 存朋友,n+1~2n 存敌人
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | #include <iostream> #include <map> using namespace std ; const int N=1e6+3; int fa[N],m,K; int n; int find( int x){ return x==fa[x]?x:fa[x]=find(fa[x]); } void cmb( int x, int y){ int fx=find(x),fy=find(y); fa[fy]= fx; } map<string, int > mp; signed main(){ int i,j,tes; string s1,s2; cin>>K>>n>>tes; for (i=1;i<=K;i++){ cin>>s1; mp[s1]=i; fa[i]=i; fa[i+K]=i+K; } int op; for (i=1;i<=n;i++){ cin>>op>>s1>>s2; int x=mp[s1],y=mp[s2]; int fx=find(x),fy=find(y); if (op==1){ if (fx==find(y+K)){ cout<< "NO\n" ; continue ;} cout<< "YES\n" ; cmb(x,y); cmb(find(x+K),find(y+K)); } else { if (fx==fy) {cout<< "NO\n" ; continue ;} cout<< "YES\n" ; cmb(find(x+K),fy); cmb(find(y+K),fx); } } for (i=1;i<=tes;i++){ cin>>s1>>s2; int x=mp[s1],y=mp[s2]; if (find(x)==find(y)) cout<<1; else if (find(x)==find(y+K)) cout<<2; else cout<<3; cout<<endl; } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!