Pagini recente » Cod sursa (job #3363714) | Cod sursa (job #3361812) | Cod sursa (job #3362093) | Cod sursa (job #3363675) | Cod sursa (job #3363712)
#include <fstream>
#include <vector>
#include <unordered_map>
using namespace std;
ifstream cin("disjoint.in");
ofstream cout("disjoint.out");
unordered_map<int,int> parent;
unordered_map<int,int> sz;
vector<bool> sol;
void make_group(int x)
{
if(parent.find(x)==parent.end())
{
parent[x]=x;
sz[x]=1;
}
}
int reprezentant(int x)
{
if(parent[x]==x) return x;
return parent[x]=reprezentant(parent[x]);
}
bool union_(int x,int y)
{
make_group(x);make_group(y);
int rx=reprezentant(x), ry=reprezentant(y);
if(rx==ry) return 0;
if(sz[rx]<sz[ry]) swap(rx,ry);
parent[ry]=rx;
sz[rx]+=sz[ry]; return 1;
}
int main()
{
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++) make_group(i);
for(int i=1;i<=m;i++)
{
int x,y,op; cin>>op>>x>>y;
bool ok=union_(x,y);
if(op==1) continue;
sol.push_back(ok);
}
for(auto ok:sol) if(ok) cout<<"NU"<<'\n';
else cout<<"DA"<<'\n';
return 0;
}