-
-
Save anonymous/75cc1851547db7db790f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
#include<cstring> | |
#include<fstream> | |
using namespace std; | |
int father[5100]; | |
string p_save[5100]; | |
int getfather(int n) | |
{ | |
if(father[n]==n) return n; | |
father[n]=getfather(father[n]); | |
return father[n]; | |
} | |
int unionfather(int u,int v) | |
{ | |
int fu,fv; | |
fu=getfather(u); | |
fv=getfather(v); | |
if(fu!=fv) | |
father[v]=father[u]; | |
} | |
int main() | |
{ | |
int n,m,p; | |
int a,b; | |
ifstream fin("relation.in"); | |
fin >>n>>m>>p; | |
int i; | |
for(i=1;i<=n;i++) | |
father[n]=n; | |
for(i=1;i<=m;i++) | |
{ | |
fin >>a>>b; | |
unionfather(a,b); | |
} | |
for(i=1;i<=p;i++) | |
{ | |
fin >>a>>b; | |
if(father[a]!=father[b]) | |
p_save[i]="No"; | |
else p_save[i]="Yes"; | |
} | |
fin.close(); | |
ofstream fout("relation.out"); | |
for(i=1;i<=p;i++) | |
fout <<p_save[i]<<endl; | |
fout.close(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment