Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define pb push_back
  5. #define mp make_pair
  6. #define clr(i, j) memset(i, j, sizeof i)
  7. typedef long long ll;
  8. typedef unsigned long long ull ;
  9. typedef pair<int,int> pii;
  10. const int N = 1e5+1;
  11. int n, m, q, x, y, t, xroot, yroot;
  12. vector<int> adj[N];
  13. string a, b;
  14. map<string, int> p;
  15. pii sub[N];
  16. int find(int x)
  17. {
  18. if(x == sub[x].first)
  19. return x;
  20. return sub[x].first = find(sub[x].first);
  21. }
  22. void Union(int x, int y)
  23. {
  24. if(x == y)
  25. return;
  26. xroot = find(x);
  27. yroot = find(y);
  28. if(sub[yroot].second > sub[xroot].second)
  29. swap(xroot, yroot);
  30. sub[yroot].first = sub[xroot].first;
  31. if(sub[xroot].second == sub[yroot].second)
  32. sub[xroot].second++;
  33. //cout << xroot << " " << yroot << endl;
  34. for(int i=0; i<adj[yroot].size(); i++)
  35. adj[xroot].pb(adj[yroot][i]);
  36. sort(adj[xroot].begin(), adj[xroot].end());
  37. }
  38. int main()
  39. {
  40. cin >> n >> m >> q;
  41. for(int i=0; i<n; i++)
  42. {
  43. sub[i].first = i;
  44. sub[i].second = 0;
  45. cin >> a;
  46. p[a] = i;
  47. }
  48. for(int i=0; i<m; i++)
  49. {
  50. cin >> t >> a >> b;
  51. //cout << *s.find(a) << " " << endl;
  52. x = find(p[a]), y = find(p[b]);
  53. if(( binary_search(adj[x].begin(), adj[x].end(), y) && t == 1) || (t == 2 && x == y))
  54. cout << "NO" << endl;
  55. else
  56. {
  57. cout << "YES" << endl;
  58. if(t == 1)
  59. Union(x, y);
  60. else
  61. {
  62. adj[x].pb(y), adj[y].pb(x);
  63. if(adj[x].size() > 1)
  64. Union(adj[x][adj[x].size()-1], adj[x][adj[x].size()-2]);
  65. if(adj[y].size() > 1)
  66. Union(adj[y][adj[y].size()-1], adj[y][adj[y].size()-2]);
  67. }
  68. }
  69. }
  70. for(int i=0; i<q; i++)
  71. {
  72. cin >> a >> b;
  73. x = find(p[a]), y = find(p[b]);
  74. if(x == y)
  75. cout << 1 << endl;
  76. else if(binary_search(adj[x].begin(), adj[x].end(), y))
  77. cout << 2 << endl;
  78. else
  79. cout << 3 << endl;
  80. }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement