随笔 - 531  文章 - 0  评论 - 3  阅读 - 10215 

给一些单词,它们可能是同义或者反义,给出一些关系定义,从前面的定义开始建立关系,如果有的关系定义和之前的冲突输出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;
    }
     
 }

 

posted on   towboat  阅读(9)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示