Mahmoud and a Dictionary CodeForces
来源:互联网 发布:萧山网络问政关注 编辑:程序博客网 时间:2024/06/06 13:59
http://codeforces.com/problemset/problem/766/D
带权并查集
#include<cstdio>#include<iostream>#include<string> #include<algorithm>#include<cstring>#include<map> typedef long long ll;using namespace std;const int maxn=1e5+10;struct node{int pre;int rel;}set[maxn];map <string,int> mp; int fnd_rt(int x){if(set[x].pre==x)return x;int fa=set[x].pre;set[x].pre=fnd_rt(fa);set[x].rel=(set[x].rel+set[fa].rel)%2;return set[x].pre;}int main(){int cnt=1;int n,m,q;cin>>n>>m>>q;for(int i=1;i<=n;i++){ set[i].pre=i;set[i].rel=0; }string a,b;for(int i=1;i<=n;i++){cin>>a;mp[a]=cnt++;}int r;for(int i=1;i<=m;i++){cin>>r>>a>>b; int ra=fnd_rt(mp[a]);int rb=fnd_rt(mp[b]);if(ra==rb){if(r==1&&(set[mp[a]].rel+set[mp[b]].rel)%2==0)cout<<"YES"<<endl; else if(r==2&&(set[mp[a]].rel+set[mp[b]].rel)%2!=0)cout<<"YES"<<endl;elsecout<<"NO"<<endl;continue; } else cout<<"YES"<<endl; set[ra].pre=rb; set[ra].rel=(set[mp[a]].rel+r-1+set[mp[b]].rel)%2; }for(int i=1;i<=q;i++){cin>>a>>b;int ra=fnd_rt(mp[a]);int rb=fnd_rt(mp[b]);if(ra!=rb) cout<<"3"<<endl;else{if((set[mp[a]].rel+set[mp[b]].rel)%2==0)cout<<"1"<<endl;elsecout<<"2"<<endl;}}return 0;}
阅读全文
0 0
- Mahmoud and a Dictionary CodeForces
- Mahmoud and a Dictionary CodeForces
- Mahmoud and a Dictionary CodeForces
- Mahmoud and a Dictionary CodeForces
- 【codeforces 766D】Mahmoud and a Dictionary
- codeforces 766D Mahmoud and a Dictionary
- Codeforces 766D Mahmoud and a Dictionary
- Codeforces 766D-Mahmoud and a Dictionary
- codeforces 766D. Mahmoud and a Dictionary
- Codeforces Round #396 (Div. 2)Mahmoud and a Dictionary
- D. Mahmoud and a Dictionary
- 【题解】codeforces766D Mahmoud and a Dictionary
- Mahmoud and a Dictionary codeforces396 div2 D
- Mahmoud and a Message CodeForces
- Mahmoud and a Triangle (CodeForces
- Codeforces Round #396 (Div. 2)-D. Mahmoud and a Dictionary(关系并查集)
- Codeforces Round #396 (Div. 2)-D. Mahmoud and a Dictionary(并查集)
- Codeforces Round #396 (Div. 2) D. Mahmoud and a Dictionary(并查集)
- java.lang.IllegalStateException: Failed to load ApplicationContext
- 个人总结20
- 访问者模式
- redis
- 类和对象
- Mahmoud and a Dictionary CodeForces
- Matlab中plot、fplot、ezplot的使用方法和区别
- 数据库事务并发带来的问题
- solr管理界面解析
- springMVC 的工作原理
- 谈笑风生线段树(点修改)
- composer的安装与使用
- 日常运维(三):Linux网络相关,netfilter,iptables
- learning R with swirl-Base Graphics