766D Mahmoud and a Dictionary[带权并查集][dfs]

SGO基线解算软件(南方测绘UI设计最美观的一款新版) 不多说,先放地址: 下载地址:https://www.southgnss.com/download_view.asp?did=361 SGO是南方测绘集团广州南方卫星导航仪器有限公司自主研发新一代南方地理数据处理平台软件,对南方特有数据格式文件(STH格式文件)、标准数据格式文件(RINEX格式文件)进行静态数据处理、动态数据后处理和进行RTK结果检验,新增加了北斗数据处理和摄影测量模块,并围绕南方的不同类型测量产品集成多款测绘领域所需的小工具,实现测绘内业数据处理一体化。 相关概念 G.. 阅读详情

D. Mahmoud and a Dictionary
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Mahmoud wants to write a new dictionary that contains n words and relations between them. There are two types of relations: synonymy (i. e. the two words mean the same) and antonymy (i. e. the two words mean the opposite). From time to time he discovers a new relation between two words.

He know that if two words have a relation between them, then each of them has relations with the words that has relations with the other. For example, if like means love and love is the opposite of hate, then like is also the opposite of hate. One more example: if love is the opposite of hate and hate is the opposite of like, then love means like, and so on.

Sometimes Mahmoud discovers a wrong relation. A wrong relation is a relation that makes two words equal and opposite at the same time. For example if he knows that love means like and like is the opposite of hate, and then he figures out that hate means like, the last relation is absolutely wrong because it makes hate and like opposite and have the same meaning at the same time.

After Mahmoud figured out many relations, he was worried that some of them were wrong so that they will make other relations also wrong, so he decided to tell every relation he figured out to his coder friend Ehab and for every relation he wanted to know is it correct or wrong, basing on the previously discovered relations. If it is wrong he ignores it, and doesn't check with following relations.

After adding all relations, Mahmoud asked Ehab about relations between some words based on the information he had given to him. Ehab is busy making a Codeforces round so he asked you for help.

Input

The first line of input contains three integers n, m and q (2 ≤ n ≤ 105, 1 ≤ m, q ≤ 105) where n is the number of words in the dictionary, mis the number of relations Mahmoud figured out and q is the number of questions Mahmoud asked after telling all relations.

The second line contains n distinct words a1, a2, ..., an consisting of small English letters with length not exceeding 20, which are the words in the dictionary.

Then m lines follow, each of them contains an integer t (1 ≤ t ≤ 2) followed by two different words xi and yi which has appeared in the dictionary words. If t = 1, that means xi has a synonymy relation with yi, otherwise xi has an antonymy relation with yi.

Then q lines follow, each of them contains two different words which has appeared in the dictionary. That are the pairs of words Mahmoud wants to know the relation between basing on the relations he had discovered.

All words in input contain only lowercase English letters and their lengths don't exceed 20 characters. In all relations and in all questions the two words are different.

Output

First, print m lines, one per each relation. If some relation is wrong (makes two words opposite and have the same meaning at the same time) you should print "NO" (without quotes) and ignore it, otherwise print "YES" (without quotes).

After that print q lines, one per each question. If the two words have the same meaning, output 1. If they are opposites, output 2. If there is no relation between them, output 3.

See the samples for better understanding.

Examples
input
3 3 4
hate love like
1 love like
2 love hate
1 hate like
love like
love hate
like hate
hate like
output
YES
YES
NO
1
2
2
2
input
8 6 5
hi welcome hello ihateyou goaway dog cat rat
1 hi welcome
1 ihateyou goaway
2 hello ihateyou
2 hi goaway
2 hi hello
1 hi hello
dog cat
dog hi
hi hello
ihateyou goaway
welcome ihateyou
output
YES
YES
YES
YES
NO
YES
3
3
1
1
2

题意: 给定n,m,q 其中n位n个单词,m为m个关系,表示单词两两间的关系,其中关系有两种,分别为1和2,1表示同义词,2表示反义词。q个询问,询问两者间关系,若没关系,输出3.。

思路:根据题目信息,关系是分类的,分为1,2两种关系,如果关系不分类,那么就是并查集,如果关系分类,就是带权的并查集。

带权并查集学习参考自:http://blog.youkuaiyun.com/harlow_cheng/article/details/52737486

两两间关系1,2用0,1表示,则关系之间符合向量运算。

具体见注释。

#include<iostream>
#include<map>
#include<algorithm>
#include<string>
using namespace std;
map<string ,int> num;
int n, m, q;
int pre[100005], relation[100005];
string s1, s2;

int find(int x)
{
    if(pre[x]==x)
    {
        return x;
    }
    int order=pre[x];
    //路径压缩
    pre[x]=find(pre[x]);
    relation[x]=(relation[x]+relation[order])%2;//符合向量图运算

    return pre[x];
}

int main()
{
    cin >> n >> m >> q;
    for(int i = 0; i < n; ++i)
    {
        cin >> s1;
        num[s1] = i;
    }
    for(int i = 0; i < n; ++i)
    {
        pre[i] = i;
        relation[i] = 0;
    }
    int x, y, z;
    for(int i = 0; i < m; ++ i)
    {
        cin >> z >> s1 >> s2;
        --z;
        x = num[s1], y = num[s2];
        int a = find(x), b = find(y);
        if(a!=b)//关系还没有建立过
        {
            cout << "YES" << endl;
            //更新结点值
            pre[b]=a;
            relation[b]=(relation[x]-relation[y]+z+2)%2; //+2防止出现负值
        }
        else
        {
            int p = (relation[x]-relation[y]+2)%2;
            if(p == z) cout << "YES" << endl;
            else cout << "NO" << endl;
        }
    }
    for(int i = 0; i < q; ++i)
    {
        cin >> s1 >> s2;
        x = num[s1], y = num[s2];
        int a = find(x), b = find(y);
        if(a != b)
            cout << "3" << endl;
        else
            cout << (relation[x] - relation[y] + 2)%2 +1 << endl;
    }
    return 0;
}


Android应用之【巧妙隐藏桌面图标的进阶方案】 本文详细探讨了Android应用中隐藏桌面图标的进阶方案,解决了传统方法在Android 10及以上版本中出现的黑色图标残留问题。通过Intent-Filter配置和厂商适配技巧,实现子应用真正隐身,同时确保主应用正常启动。适用于企业工具套件、模块化系统等场景,提升用户体验和安全性。 阅读详情

相关推荐

Stata森林图进阶:个性化定制与高级技巧

本文详细介绍了Stata森林图的进阶技巧,包括个性化定制与高级应用。通过meta分析工具如metan命令,用户可以优化字体、颜色、坐标轴等视觉元素,提升森林图的专业性和可读性。文章还提供了黑白印刷设置、高分辨率导出等实用技巧,帮助科研人员制作符合学术出版要求的森林图。

ios99的博客 254

D. Mahmoud and a Dictionary----带权并查集

D. Mahmoud and a Dictionary time limit per test 4 seconds memory limit per test 256 megabytes input standard input output standard output Mahmoud wants to write a new di

bless295的博客 618

WindTerm 安装使用教程

WindTerm是一款Github上开源的 SSH 终端工具,它是完全可以比肩MobaXterm工具的。

m0_69576880的博客 4007

Codeforces1253D 带权并查集

https://codeforces.com/contest/1253/problem/D n个点 m条边 <=2e5 l<m<r l能到r 则l一定得满足可以到中间点 加最小边数能满足条件 14 8 1 2 2 7 3 4 6 3 5 7 3 8 6 8 11 12 1 #include<bits/stdc++.h> using namespace std; ty...

qq_40423146的博客 278

基于 Datawhale 开源量化投资学习指南(10):基于 LightGBM 的量化选股

通过本篇文章,我们深入探索了如何运用 LightGBM 机器学习模型实现量化选股。首先,我们通过获取股票的历史数据,进行特征工程和标签制作,将选股问题转化为一个分类问题。接着,我们使用 LightGBM 训练模型,并在验证集和测试集中评估了模型的表现。该选股策略的核心在于结合机器学习算法,从历史数据中寻找能够捕捉股票短期涨跌的关键特征,从而为投资者提供辅助决策支持。虽然本文的模型取得了一定的效果,但这只是量化投资领域的一小部分。

不想宅的冷同学 1346

本地部署gradle

在部署CAS构建执行包时,如果按照CAS的官方命令gradlew clean build,如果系统没安装过gradle,这个步骤将会从网上下载gradle,但是进度非常慢,耗时很长。为了更快地完成项目构建,我们可以直接在本地部署好gradle,节省一点时间。

益添的博客 779

Codeforces Round #786 (Div. 3) B. Dictionary

time limit per test2 seconds memory limit per test512 megabytes inputstandard input outputstandard output The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combi

Lyriv的博客 514

Rapid SCADA MQTT

一、配置RapidSCADA 1、准备KpMqtt.dll、Jint.dll库文件,从网盘下载。 2、将以上两个文件拷贝到SCADA安装目录的Comm/KP目录下,如下: C:\SCADA\ScadaComm\KP 3、新建SCADA项目 4、新建Line 5、新建Device 6、拷贝Config目录下的KpMqtt_001.xml文件、KpMqtt_Job.js文件到项目Comm\Config下,如下示例链接 C:\SCADA\Projects\MQTTProject\Instances\Defau

mumanquan1的博客 3005

CodeForces - 982D Shark 思路+带权并查集

题意: 给定n长度序列,包含n个不同的数,求一个k使得以小于k的数(或区间)分割的所有区间,区间长度相同的同时,区间的个数尽量多,k尽量小 思路: n个数值各不相同,我们能够知道,答案肯定是n个数中某个数+1; 这样我们从小到大遍历n个数,同时维护他们在原序列的下标,这样就将原问题转化为: 将当前的数(a[i])加入之前的所有数的集合,看下标连续的所有区间的长度是否一样,在更新答案(区间...

床前明月光 357

电磁阀颤振工作原理

颤振 颤振,弹性结构在均匀气(或液)流中受到空气(或液体)动力、弹性力和惯性力(见达朗伯原理)的耦合作用而发生的大幅度振动,它是气动弹性力学中最重要的问题之一。 随着机电液一体化技术的发展,电液比例控制技术的应用越来越广泛。电液比例控制系统范围较广,在液压传动与控制过程中,系统能够接受模拟信号或数字信号,使输出的流量或压力连续成比例地变化,都属于此类。该控制系统的优点是充分利用了液压控制与电气控制的长处,即功率传递靠液压,信号处理靠电气的优化措施。 电液比例控制原理与要求 液压控制系统中两个最重要的被控参数

小幽余生不加糖 1万+

【Windows Server 2019】发布服务器 | 远程桌面服务的安装与配置 Ⅰ——理论,实验拓扑和安装基于RemoteAPP的RDS

博客介绍了Windows Server中远程桌面服务的理论,实验拓扑以及基于RemoteAPP的RDS安装。博文介绍了如何在Windows Server上部署发布服务器。

VMware, Windows, Linux, Cisco, Container, Kubernetes, Kali 6128

并查集总结(不看后悔系列)

在计算机科学中,并查集是一种树型的数据结构,用于处理一些不交集(Disjoint Sets)的合并及查询问题。有一个联合-查找算法(union-find algorithm)定义了两个用于此数据结构的操作: ...

一身清贫怎敢入繁华 880

EndNote X9 快速上手教程(毕业论文参考文献管理器)

EndNote X9 快速上手教程一、EndNote X9安装1.安装2 安装之后重要步骤二、第一次打开 EndNote三、参考文献引用1. 导入知网文献2. 参考文献引用四、常见问题1. 引用文献中出现%J的问题2 参考文献中出现et.al1 在文章中引用的编号的地方出现2 在参看文献列表里出现 一、EndNote X9安装 1.安装 打开文件夹 安装前,一定要读 #Readme.txt 文件 一定要把License.dat和ENX9Inst.msi放在同一文件夹,安装的时候会自动激活。 我的安装路

程序员,他们想的是什么?他们想的永远都是技术,他们崇尚的也永远都是技术。 2万+

Codeforces Round 123 (Div. 2) E. Building Forest(带权并查集)

这个并查集维护的是点的连通性,以及集合中任意两点之间的距离(注意,这个"距离"并不一定代表着原图两点的距离,根据合并情况的不同,代表的信息会是不一样的),并查集能做到的带权并查集也能做到,带权并查集事实上就是并查集的另一扩展。(首先强调,这里的边并不一定代表着单向边,笔者为了方便标记,图中的这些单向边的意义是指出表示一个点的根节点,而在带权并查集中这些单向边应该是双向边)带权并查集事实上是将一些要维护的信息作为"边权",这些信息构成的内容变成"距离",然后经过合并集合后我们就可以高效的查询集合内的信息了。

田所浩二的博客 266

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

D. Mahmoud and a Dictionary(种类并查集)

D. Mahmoud and a Dictionary time limit per test 4 seconds memory limit per test 256 megabytes input standard input output standard output Mahmoud wants to write a new dictionary that contains...

提拉米苏的博客 611

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

食物链

  Acceteped : 88  Submit : 156 Time Limit : 1000 MS Memory Limit : 65536 KB Description动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形。A吃B, B吃C,C吃A。现有N个动物,以1-N编号。每个动物都是

huangmingzheng的专栏 333

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

Mahmoud and a Dictionary CodeForces - 766D

Mahmoud wants to write a new dictionary that contains n words and relations between them. There are two types of relations: synonymy (i. e. the two words mean the same) and antonymy (i. e. the two wor...

蒟蒻小胖 359

数据结构实验之——有向带权图和无向带权图DFS、BFS算法实现以及领接矩阵的展示

DFS、BFC算法说明代码实现测试用例 说明 BFS算法我们常用的方法就是利用队列来处理,访问一个结点,就访问所有与它有关的结点,然后反复,比较简单 DFS算法便是访问一个结点,然后一直访问下去,直到没有结点可访问,可谓“不撞南墙不回头”,该算法最重要的就是实现int FirstAdjVex函数、int NextAdjVex函数这两个函数的实现 下面提供有向和无向图的DFS、BFS的实现 想要得到有向和无向只需要在void createMGraph函数里面修改即可,我也做了标记,供大家参考 代码实现 #de

Xmumu_的博客 3297

寻根:dfs(子树带权和不带权的和)

类似哈夫曼树求wpl(带权路径长度),上移一层 就减去子树的 不 带权的和。下移就加 题目描述 有一颗有 n 个结点树,结点被编号为 1 ~ n,记根结点深度为 1,如果第 i 个结点的深度是 d,则它贡献的价值是 d × wi,这棵树的价值是所有结点的价值和。 求当根结点为 1 ~ n 时, 树的价值分别为多少 输入格式 第一行一个整数 T,代表有 T 组测试数据。对于每一组测试数据, 第一行有 1 个整数 n, 第二行有 n 个整数 wi, 接下来 n − 1 行,每行有两个整数 x, y,.

weixin_50904510的博客 399

【题解】codeforces766D Mahmoud and a Dictionary

题目链接 题意:有n个单词,给定m个关系,每个关系要么表示单词a与单词b相同,要么表示单词a与单词b相反。并且“相同”与“相反”有性质:若a与b相同,b与c相同,则a与c相同(从而单词的相同关系是等价关系);若a与b相反,b与c相反,则a与c相同。按顺序判断这m个关系是否可以成立,若可以成立,则加上这个关系,否则忽略。再给定q个询问,每个询问 查询单词a与单词b的关系(相同、相反或未知)。 分

ZJZ的博客 1140

CodeForces - 766D Mahmoud and a Dictionary (并查集)

Mahmoud wants to write a new dictionary that contains n words and relations between them. There are two types of relations: synonymy (i. e. the two words mean the same) and antonymy (i. e. the two wor

Tsaryu的博客 607

Codeforces Round #396 (Div. 2)-D. Mahmoud and a Dictionary(并查集)

原题链接 D. Mahmoud and a Dictionary time limit per test 4 seconds memory limit per test 256 megabytes input standard input output standard output Mahmoud wan

L. 498

D. Mahmoud and a Dictionary 题解

http://codeforces.com/contest/766/problem/D D. Mahmoud and a Dictionary time limit per test 4 seconds memory limit per test 256 megabytes input standard input output standard output...

weixin_30544657的博客 188

Mahmoud and a Dictionary codeforces396 div2 D

D. Mahmoud and a Dictionary time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard output Mahmoud wants to write a new dictionary that contains n words

黑码 611

Keil环境搭建全攻略:安装激活、芯片包管理与高频报错排查

嵌入式开发离不开集成开发环境(IDE)的选择,而Keil凭借对ARM内核的完善支持,成为STM32、GD32等单片机开发的主流工具。Keil环境搭建看似简单,却常因C51与MDK版本混淆、激活失败、芯片包(Pack)缺失而阻碍项目推进。理解版本差异与Pack原理,掌握正确的激活方法,是规避环境坑点的关键。从技术价值看,一套稳定配置好的Keil环境,能大幅减少编译与下载环节的‘玄学’报错,让开发者将精力集中在代码实现上。在实际开发中,无论是基于STM32的入门学习,还是GD32的工程移植,规范的安装—激活—装

weixin_33913332的博客 355

Codeforces 766D Mahmoud and a Dictionary(并查集+map)

思路:对于同义词和反义词分别考虑最一般的形式。 即,对于集合x,y和其相应的反义词的集合x+n,y+n,讨论其关系。#include <string.h> #include <iostream> #include <cstdio> #include <queue> #include<algorithm> #include <map> using namespace std; typedef lon

普通で歩む 385
上一篇: 766C Mahmoud and a Message[字符串][简单dp]
下一篇: 766E Mahmoud and a xor trip[二进制拆位][dfs]
Qer_
博客等级 码龄11年 34粉丝 69原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值