图的基本存储的基本方式二

xiaoxiao2021-02-28  9

多组输入，到文件结尾。

Output

对于每一条询问，输出一行。若 a b 可以直接连通输出 Yes ，否则输出 No

Example Input

2 1 0 1 2 0 1 1 0

Yes No

Author

lin #include <bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 5e5 + 5; typedef struct node { int data; struct node *next; } node; node *a[maxn]; int main() { ll n, m; while(~scanf("%lld%lld", &n, &m)) { for(int i = 0; i < n; i++) { a[i] = NULL; } while(m--) { int u, v; scanf("%d%d", &u, &v); if(!a[u]) { a[u] = new node; a[u]->data = v; a[u]->next = NULL; } else { node *p, *q; p = new node; p->data = v; p->next = NULL; q = a[u]->next; p->next = q; a[u]->next = p; } } ll q; scanf("%lld", &q); while(q--) { int flag = 0; int u, v; scanf("%d%d", &u, &v); if(!a[u]) { printf("No\n"); } else { node *p = a[u]; while(p) { if(p->data == v) { flag = 1; break; } p = p->next; } if(flag) { printf("Yes\n"); } else { printf("No\n"); } } } } return 0; } /*************************************************** User name: Result: Accepted Take time: 52ms Take Memory: 4000KB Submit time: 2018-01-28 19:22:59 ****************************************************/