7 해설

  • -1
    @ 2023-7-16 14:36:02
    #include <bits/stdc++.h>
    #define IOS ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    #define re register
    using namespace std;
    typedef long long ll;
    int fth[1000010], n, m, q, x, y, tx, ty;
    int fnd(int t)
    {
    	return (fth[t]==t?t:fth[t]=fnd(fth[t]));
    }
    int main()
    {
    	IOS;
    	cin>>n>>m;
    	for(int i=0; i<=n; i++) fth[i]=i;
    	for(int i=0; i<m; i++)
    	{
    		cin>>x>>y;
    		tx=fnd(x), ty=fnd(y);
    		if(tx!=ty) fth[ty]=tx;
    	}
    	cin>>q;
    	while(q--)
    	{
    		cin>>x>>y;
    		cout<<(fnd(x)==fnd(y)?"Yes\n":"No\n");
    	}
    	return 0;
    }
    

    정보

    ID
    454
    시간
    1000ms
    메모리
    512MiB
    난이도
    2
    태그
    제출 기록
    302
    맞았습니다.
    80
    아이디