PAT 1118

    xiaoxiao2021-03-25  131

    #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<vector> using namespace std; const int maxn = 10010; int father[maxn]; int findfather(int a) { int b,temp; b = a; while (father[a] != a) a = father[a]; while (b != a) { temp = father[b]; father[b] = a; b = temp; } return a; } void init() { for (int i=0;i<maxn;i++) { father[i] = i; } return; } void _union(int a, int b) { if (findfather(a) != findfather(b)) father[findfather(b)] = findfather(a); return; } int main() { init(); int n; cin >> n; int m; int max = -1; vector<int> s; int temp; for (int i=0;i<n;i++) { cin >> m; for (int j=0;j<m;j++) { cin >> temp; if (temp > max) max = temp; s.push_back(temp); } for (int j = 0;j < s.size()-1;j++) _union(s[j],s[j+1]); s.clear(); } int num = 0; for (int i=1;i<=max;i++) { if (father[i] == i) num++; } cout << num << " " << max << endl; int query,a,b; cin >> query; for (int i=0;i<query;i++) { cin >> a >> b; if (findfather(a) == findfather(b)) cout << "Yes" << endl; else cout << "No" << endl; } system("pause"); return 0; }
    转载请注明原文地址: https://ju.6miu.com/read-11167.html

    最新回复(0)