else{
bool flag=true;
for(int j=1;j<=v;j++){
for(int x=0;x<tu[j].size();x++){
if(color[j]==color[tu[j][x]]){
flag=false;break;
}
}
}
if(flag) printf(“Yes\n”);
else printf(“No\n”);
}
}
return 0;
}
L2-024 部落 (25 分)
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e4+5;
int arr[maxn];
int father[maxn];
int find(int x){
if(x == father[x]) return x;
else{
int tmp = find(father[x]);
father[x] = tmp;
return tmp;
}
}
void join(int x, int y){
int fx = find(x), fy = find(y);
if(fx != fy)
father[fy] = fx;
}
int main(){
int n,k;
set st_total,st_group;
cin>>n;
for(int i=1;i<=maxn;i++){
father[i]=i;
}
for(int i=1;i<=n;i++){
cin>>k;
for(int j=0;j<k;j++){
cin>>arr[j];
st_total.insert(arr[j]);
if(j) join(arr[j-1], arr[j]); //j不为零的时候,合并两个元素
}
}
for(int i=1;i<=st_total.size();i++){
st_group.insert(find(i));
}
cout<<st_total.size()<<" "<<st_group.size()<<endl;
int m,u,v;
cin>>m;
for(int i=0;i<m;i++){
cin>>u>>v;
if(find(u)==find(v)) cout<<“Y”<<endl;
else cout<<“N”<<endl;
}
return 0;
}
L2-025 分而治之 (25 分)
#include <bits/stdc++.h>
#define mst(a, n) memse