1 条题解

  • -3
    @ 2023-7-30 13:57:59

    非正解,并查集:

    #include<iostream>
    using namespace std;
    int n, m, q, x, y;
    int rel[5010];
    int find(int a) {
    	if (a == rel[a]) return a;
    	return rel[a] = find(rel[a]);
    }
    void join(int a, int b) {
    	int p = find(a), q = find(b);
    	if (p != q) rel[p] = q;
    }
    int main() {
    	freopen("tea.in", "r", stdin);
    	freopen("tea.out", "w", stdout);
    	scanf("%d%d%d", &n, &m, &q);
    	for (int i = 1; i <= n; ++ i)
    	    rel[i] = i;
    	while (m --){
    	    scanf("%d%d", &x, &y);
    		join(x, y);
    	}
    	while (q --){
    	    scanf("%d%d", &x, &y);
    		putchar(find(x) == find(y) ? 'Y' : 'N');
    		putchar('\n');
    	}
    	return 0;
    }
    

    信息

    ID
    445
    时间
    1000ms
    内存
    256MiB
    难度
    7
    标签
    (无)
    递交数
    83
    已通过
    20
    上传者