#include <cstdlib> #include <iostream>
using namespace std;
int n, m, path[110][110], set[110];
void init() { memset(path, 0, sizeof(path)); memset(set, 0, sizeof(set)); }
int find_set(int a) { if(a != set[a]) set[a] = find_set(set[a]); return set[a]; }
int find(int a, int b) { if(find_set(a) == find_set(b)) { return true; } return false; }
void union_set(int a, int b) { set[find_set(a)] = find_set(b); }
void make_set(int n) { int i, j; for(i=1; i<=n; i++) { set[i] = i; } for(i=1; i<=n; i++) { for(j=i; j<=n; j++) { if(0 == path[i][j]) continue; // printf("path:%d %d\n", i, j);
union_set(i, j); } } }
int main(int argc, char *argv[]) { int D, a, b, i, q; scanf("%d %d", &n, &m); init(); for(i=0; i<m; i++) { scanf("%d %d %d", &D, &a, &b); if(0 == D) { path[a][b] = path[b][a] = 1; } else { path[a][b] = path[b][a] = 0; } } make_set(n); /* for(i=1; i<=n; i++) { printf("%d ", set[i]); } printf("\n"); */ scanf("%d", &q); for(i=0; i<q; i++) { scanf("%d %d", &a, &b); if(find(a, b)) printf("yes\n"); else printf("no\n"); } system("PAUSE"); return EXIT_SUCCESS; }
|