1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
|
#include<bits/stdc++.h> using namespace std;
typedef long long ll; typedef __int128 int128; typedef pair<int , int > pii; typedef unsigned long long ull;
namespace FastIO { template<typename T> inline T read(T& x) { x = 0; int f = 1; char ch; while (!isdigit(ch = getchar())) if (ch == '-') f = -1; while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar(); x *= f; return x; } template<typename T, typename... Args> inline void read(T& x, Args &...x_) { read(x); read(x_...); return; } inline ll read() { ll x; read(x); return x; } }; using namespace FastIO;
const int N = 1e5 + 10;
int n, m, c, Q;
int fa[N]; inline int find(int x) { return fa[x] = (fa[x] == x ? x : find(fa[x])); }
set<int >st[N]; map<int, vector<int> > v[N];
inline void Merge(int u, int v) { u = find(u), v = find(v); if(u == v) return ; if(st[u].size() < st[v].size()) swap(u, v); for(auto &p : st[v]) { st[u].insert(p); } fa[v] = u; }
inline void Insert(int x, int y, int w) { st[find(x)].insert(y), st[find(y)].insert(x); v[x][w].push_back(y), Merge(y, v[x][w][0]); v[y][w].push_back(x), Merge(x, v[y][w][0]); }
inline void Query(int x, int y) { if(find(x) == find(y) || st[find(x)].count(y)) { printf("Yes\n"); } else printf("No\n"); }
inline void Input() { read(n, m, c, Q); for(int i = 1; i <= n; i++) fa[i] = i; int x, y, z; for(int i = 1; i <= m; i++) { read(x, y, z); Insert(x, y, z); } }
inline void Work() { char op[3]; int x, y, z; while(Q--) { scanf("%s", op); if(op[0] == '+') { read(x, y, z); Insert(x, y, z); } else { read(x, y); Query(x, y); } } }
int main() { int T = 1; while(T--) { Input(); Work(); } return 0; }
|