题目链接:点击打开链接
思路:
带权并查集水题。 带权并查集可以知道在一个集合里的两点间距离。那么这种同义反义关心恰好对应距离的奇偶。
附上一图:
这就是合并的过程。
细节参见代码:
#include <cstdio>#include <cstring>#include <algorithm>#include <iostream>#include <string>#include <vector>#include <stack>#include <ctime>#include <bitset>#include <cstdlib>#include <cmath>#include <set>#include <list>#include <deque>#include <map>#include <queue>#define Max(a,b) ((a)>(b)?(a):(b))#define Min(a,b) ((a)<(b)?(a):(b))using namespace std;typedef long long ll;typedef long double ld;const double eps = 1e-6;const double PI = acos(-1);const int mod = 1000000000 + 7;const int INF = 0x3f3f3f3f;// & 0x7FFFFFFFconst int seed = 131;const ll INF64 = ll(1e18);const int maxn = 1e5+10;int T,n,m,q,p[maxn],dist[maxn];map<string, int> mp;char s[33], s1[33];int _find(int x) { if(p[x] == x) return x; int oldfa = p[x]; p[x] = _find(p[x]); dist[x] = (dist[x] + dist[oldfa])%2; return p[x];}void init() { for(int i = 1; i <= n; i++) { p[i] = i; dist[i] = 0; }}int main() { scanf("%d%d%d", &n, &m, &q); for(int i = 1; i <= n; i++) { scanf("%s", s); mp[s] = i; } init(); for(int i = 1; i <= m; i++) { int id; scanf("%d%s%s", &id, s, s1); int id1 = mp[s], id2 = mp[s1]; int x = _find(id1), y = _find(id2); if(x != y) { PRintf("YES/n"); if(id == 1) p[x] = y, dist[x] = (dist[id2]-dist[id1]+2)%2; else p[x] = y, dist[x] = (dist[id2]-dist[id1]+1+2)%2; } else { int cur = (dist[id1]-dist[id2]+2)%2; if(id == 1) { if(cur & 1) printf("NO/n"); else printf("YES/n"); } else { if(cur & 1) printf("YES/n"); else printf("NO/n"); } } } while(q--) { scanf("%s%s", s, s1); int id1 = mp[s], id2 = mp[s1]; int x = _find(id1), y = _find(id2); if(x != y) printf("3/n"); else { int cur = (dist[id1] - dist[id2] + 2)%2; if(cur & 1) printf("2/n"); else printf("1/n"); } } return 0;}
新闻热点
疑难解答