Pagini recente » Cod sursa (job #1363410) | Cod sursa (job #2329561) | Cod sursa (job #2611723) | Cod sursa (job #1976187) | Cod sursa (job #3138622)
{\rtf1\ansi\ansicpg1252\cocoartf2636
\cocoatextscaling0\cocoaplatform0{\fonttbl\f0\fswiss\fcharset0 Helvetica;}
{\colortbl;\red255\green255\blue255;}
{\*\expandedcolortbl;;}
\paperw11900\paperh16840\margl1440\margr1440\vieww11520\viewh8400\viewkind0
\pard\tx566\tx1133\tx1700\tx2267\tx2834\tx3401\tx3968\tx4535\tx5102\tx5669\tx6236\tx6803\pardirnatural\partightenfactor0
\f0\fs24 \cf0 #include <fstream>\
\
using namespace std;\
\
ifstream cin("disjoint.in");\
ofstream cout("disjoint.out");\
\
const int nmax = 1e5 + 9;\
\
int root[nmax], sz[nmax];\
\
int find(int x) \{\
if(root[x] == x) \{\
return x;\
\}\
root[x] = find(root[x]);\
return root[x];\
\}\
\
void comb(int a, int b) \{\
if(sz[a] < sz[b]) \{\
swap(a, b);\
\}\
sz[a] += sz[b];\
root[a] = b;\
\}\
\
int main() \{\
int n, m;\
cin >> n >> m;\
for(int i = 1; i <= n; i ++) \{\
root[i] = i, sz[i] = 1;\
\}\
for(int i = 1; i <= m; i ++) \{\
int op, a, b;\
cin >> op >> a >> b;\
if(op == 1) \{\
comb(find(a), find(b));\
\} else \{\
if(find(a) == find(b)) \{\
cout << "DA\\n";\
\} else \{\
cout << "NU\\n";\
\}\
\}\
\}\
return 0;\
\}}