标签:ORC 题解 ict pre 输出 book printf dict 并且
#include<bits/stdc++.h> using namespace std; #define ll long long const int maxn=1e5+10; char a[30],b[30]; map<string,int>ma; int book[maxn*2]; int fin(int x) { if(book[x]==x)return x; else return book[x]=fin(book[x]); } int main() { int n,m,q; scanf("%d %d %d",&n,&m,&q); for(int i=1;i<=n;i++) { scanf("%s",a); ma[a]=i; } for(int i=1;i<=2*n;i++) book[i]=i; for(int i=1;i<=m;i++) { int com; scanf("%d %s %s",&com,a,b); int x=ma[a],y=ma[b]; if(com==1) { if(fin(x)==fin(y+n)||fin(y)==fin(x+n)) printf("NO\n"); else { printf("YES\n"); book[fin(x)]=fin(y); book[fin(x+n)]=fin(y+n); } } else { if(fin(x)==fin(y)||fin(x+n)==fin(y+n)) printf("NO\n"); else { printf("YES\n"); book[fin(x)]=fin(y+n); book[fin(y)]=fin(x+n); } } } for(int i=1;i<=q;i++) { scanf("%s %s",a,b); int x=ma[a],y=ma[b]; if(fin(x)==fin(y)||fin(x+n)==fin(y+n)) printf("1\n"); else if(fin(x)==fin(y+n)||fin(y)==fin(x+n)) printf("2\n"); else printf("3\n"); } return 0; }
codeforces#766 D. Mahmoud and a Dictionary (并查集)
标签:ORC 题解 ict pre 输出 book printf dict 并且
原文地址:https://www.cnblogs.com/carcar/p/10126848.html