亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 學院 > 開發設計 > 正文

codeforces766D Mahmoud and a Dictionary [并查集]【數據結構】

2019-11-10 16:45:34
字體:
來源:轉載
供稿:網友

題目連接:http://codeforces.com/contest/766/PRoblem/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 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, m is 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,加和為零表示敵對關系,然后可以在維護一個并查集或者怎樣 ,但是最后GG了

看了題解 發現用另一個數組來維護本堆的反義詞的堆是哪一個,真是奧妙重重。666

這樣的話 這個題目就簡單清晰多了,直接維護就行了。

注意對于m條關系的判定時的細節就好了,詳見代碼。

借(chao)鑒(xi)這里<–戳戳戳

附本題代碼 —————————————————————————————-.

int n,m,q,op;string str,str1,str2;map<string,int>mmp;int pre[N],opp[N];int findi(int x){ int r = x; while(r != pre[r]) r=pre[r]; int i=x,j; while(i!=j){ j=pre[i]; pre[i]=r; i=j; } return r;}void init(){ mmp.clear(); Rep(i,0,n) pre[i]=i,opp[i]=0;}int main(){ while(cin>>n>>m>>q){ init(); Rep(i,1,n) cin>>str,mmp[str]=i; Rep(i,1,m){ cin>>op>>str1>>str2; int x = mmp[str1],y = mmp[str2]; int u = findi(x),v = findi(y); int uu= findi(opp[u]),vv= findi(opp[v]); if(op==1){ if(uu!=v){ puts("YES"); pre[u]=v; if(uu&&vv) pre[uu]=vv; if(!vv) opp[v]=uu; } else puts("NO"); } else { if(u!=v) { puts("YES"); if(uu) pre[v]=uu; else opp[u]=v; if(vv) pre[u]=vv; else opp[v]=u; } else puts("NO"); } } Rep(i,1,q){ cin>>str1>>str2; int x = mmp[str1],y = mmp[str2]; int u = findi(x),v = findi(y); int uu= findi(opp[u]),vv= findi(opp[v]); if (u==v) puts("1"); else { if (uu==v||vv==u) puts("2"); else puts("3"); } } } return 0;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
日韩欧美在线字幕| 精品久久久久人成| 日韩一级裸体免费视频| 98视频在线噜噜噜国产| 国产成人精品在线| 久久久久久九九九| 欧洲精品毛片网站| 久久人人看视频| 91免费精品国偷自产在线| 日韩在线观看精品| 17婷婷久久www| 国产精品国语对白| 亚洲一区制服诱惑| 亚洲偷熟乱区亚洲香蕉av| 久久精品91久久久久久再现| 中文字幕久精品免费视频| 97精品久久久| 成人网在线视频| 国产精品视频xxxx| 日韩在线www| 欧美日韩国产中文字幕| 欧美国产视频一区二区| 91高清视频在线免费观看| 精品一区二区三区四区在线| 日韩一区二区欧美| 精品久久久久久久久中文字幕| 久久久av电影| 亚洲国产欧美在线成人app| 国产一区二区三区视频在线观看| 亚洲欧美一区二区三区情侣bbw| 性欧美暴力猛交69hd| 成人性生交大片免费看小说| 成人在线一区二区| 精品一区二区亚洲| 日韩av日韩在线观看| 久久深夜福利免费观看| 国产精品伦子伦免费视频| 国产69精品99久久久久久宅男| 91色琪琪电影亚洲精品久久| 欧美成人黑人xx视频免费观看| 成人性生交大片免费看小说| 国产精品1234| 日韩成人高清在线| 国产日韩精品一区二区| 亚洲成av人片在线观看香蕉| 亚洲精品美女久久久| 亚洲大胆人体视频| 91久久久久久国产精品| 欧美丝袜第一区| 欧美电影《睫毛膏》| 亚洲第一福利网| 在线国产精品播放| 久久免费视频网| 91精品国产自产在线观看永久| 北条麻妃在线一区二区| 久久久精品在线观看| 国产亚洲免费的视频看| 国产精品亚洲视频在线观看| 亚洲精品videossex少妇| 欧美激情欧美狂野欧美精品| 欧美激情精品久久久久久| 91在线精品播放| 亚洲tv在线观看| 欧美www视频在线观看| 在线亚洲欧美视频| 尤物yw午夜国产精品视频明星| 精品国产91久久久久久| 亚洲综合一区二区不卡| 久久免费国产视频| 91av视频在线播放| 最近2019中文免费高清视频观看www99| 欧美一区二区大胆人体摄影专业网站| 欧美自拍视频在线观看| 亚洲最大激情中文字幕| 九九久久综合网站| 成人网页在线免费观看| 久久久最新网址| 亚洲一区美女视频在线观看免费| 日韩免费观看在线观看| 97香蕉超级碰碰久久免费的优势| 91九色国产社区在线观看| 欧美激情视频给我| 日韩中文综合网| 日韩中文第一页| 日韩一二三在线视频播| 91免费人成网站在线观看18| 九九热精品视频国产| 日韩精品视频免费在线观看| 成人免费视频97| 久久99久久久久久久噜噜| 国产不卡视频在线| 国产精品国产自产拍高清av水多| 亚洲va久久久噜噜噜久久天堂| 久久综合伊人77777蜜臀| 欧美国产在线视频| 北条麻妃久久精品| 亚洲精品免费一区二区三区| 在线视频欧美性高潮| 欧美成人免费小视频| 91精品国产综合久久香蕉最新版| 中文字幕精品www乱入免费视频| 亚洲精品二三区| 亚洲a中文字幕| 久久在线观看视频| 欧美激情高清视频| 欧美最猛黑人xxxx黑人猛叫黄| 美女精品视频一区| 97超级碰碰碰| 亚洲欧美综合精品久久成人| 成人黄色免费看| 欧美午夜精品久久久久久久| 久久国产精品久久久久久| 欧美色videos| 亚洲第一精品电影| 久久男人资源视频| 久久精品国产一区| 国产精品99久久久久久久久久久久| 久久久日本电影| 日韩美女免费视频| 精品视频在线播放免| 久久在精品线影院精品国产| 国产精品丝袜白浆摸在线| 福利精品视频在线| 日韩综合视频在线观看| 91在线网站视频| 色琪琪综合男人的天堂aⅴ视频| 欧美精品一区二区三区国产精品| 欧美疯狂xxxx大交乱88av| 欧美人与性动交| www.色综合| 日韩免费在线视频| 亚洲xxxx18| 欧美成人剧情片在线观看| 国产精品日韩精品| 国产欧美一区二区三区四区| www.精品av.com| 亚洲欧美成人在线| 自拍偷拍免费精品| 亚洲欧美制服中文字幕| 北条麻妃一区二区在线观看| 欧美亚洲视频一区二区| 久久久精品一区二区| 一区二区三区视频在线| 欧美日韩成人在线视频| 亚洲韩国青草视频| 欧美精品在线免费观看| 欧美一级黑人aaaaaaa做受| 日韩精品视频在线免费观看| 在线国产精品播放| 红桃视频成人在线观看| 亚洲自拍偷拍福利| 亚洲a级在线播放观看| 中文字幕亚洲专区| 亚洲视频第一页| 亚洲成人免费网站| 久久精品精品电影网| 成人中文字幕+乱码+中文字幕| 欧美大片欧美激情性色a∨久久| 神马久久久久久| 日韩精品久久久久久福利| 中文综合在线观看| 欧美成年人视频| 欧美日韩国产中字| 欧美成人午夜激情|