Pagini recente » Cod sursa (job #3227290) | Cod sursa (job #1551017) | Cod sursa (job #3281265) | Cod sursa (job #1092827) | Cod sursa (job #3179159)
#include <fstream>
#include <vector>
#include <cstring>
const int NMAX = 1e5;
class InParser {
private:
std::vector<char> str;
int ptr;
std::ifstream fin;
private:
char get_char() {
if (ptr == (int)str.size()) {
fin.read(str.data(), (int)str.size());
ptr = 0;
}
return str[ptr++];
}
template<class T>
T get_int() {
char chr;
do {
chr = get_char();
} while (!isdigit(chr) && (chr != '-'));
int sgn = +1;
if (chr == '-') {
sgn = -1;
chr = get_char();
}
T num = 0;
while (isdigit(chr)) {
num = num * 10 + (chr - '0');
chr = get_char();
}
return sgn * num;
}
public:
InParser(const char *name) : str((int)1e5), ptr((int)str.size()), fin(name) { }
~InParser() { fin.close(); }
template<class T>
friend InParser &operator >> (InParser &in, T &num) {
num = in.get_int<T>();
return in;
}
};
class OutParser {
private:
std::vector<char> str;
int ptr;
std::ofstream fout;
private:
void put_char(char chr) {
if (ptr == (int)str.size()) {
fout.write(str.data(), (int)str.size());
ptr = 0;
}
str[ptr++] = chr;
}
template<class T>
void put_int(T num) {
if (num < 0) {
put_char('-');
num *= -1;
}
if (num > 9)
put_int(num / 10);
put_char(num % 10 + '0');
}
public:
OutParser(const char *name) : str((int)1e5), ptr(0), fout(name) { }
~OutParser() { fout.write(str.data(), ptr); fout.close(); }
template<class T>
friend OutParser &operator << (OutParser &out, const T num) {
out.put_int(num);
return out;
}
friend OutParser &operator << (OutParser &out, const char chr) {
out.put_char(chr);
return out;
}
friend OutParser &operator << (OutParser &out, const char *str) {
for (int i = 0; str[i]; i++)
out.put_char(str[i]);
return out;
}
};
int father[3 * NMAX];
int root( int node ) {
int f, x, aux;
for ( f = node; father[f] > 0; f = father[f] );
for ( x = node; father[x] > 0; ) {
aux = father[x];
father[x] = f;
x = aux;
}
return f;
}
int main() {
InParser fin("disjoint.in");
OutParser fout("disjoint.out");
int n, q;
fin >> n >> q;
for ( int i = 1, a, b, tip; i <= q; i ++ ) {
fin >> tip >> a >> b;
switch( tip ) {
case 1:
a = root( a );
b = root( b );
if ( father[a] > father[b] ) std::swap( a, b );
father[a] += father[b] - 1;
father[b] = a;
break;
default:
if ( root( a ) == root( b ) ) {
fout << "DA\n";
} else {
fout << "NU\n";
}
}
}
return 0;
}