Skip to content

Instantly share code, notes, and snippets.

/1034.cpp Secret

Created March 4, 2015 14:37
Show Gist options
  • Save anonymous/75cc1851547db7db790f to your computer and use it in GitHub Desktop.
Save anonymous/75cc1851547db7db790f to your computer and use it in GitHub Desktop.
#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