src2020/4083_mit/mit.cpp

41 lines
1012 B
C++
Raw Normal View History

2020-04-08 17:39:43 +08:00
#include <iostream>
using namespace std;
int main()
{
int n, m, ans = 0;
int pa[100001] = { 0 }, zhi[100001] = { 0 };
cin >> n >> m;
for (int i = 1; i <= m; i++) {
char u;
int a, b;
cin >> u >> a >> b;
//00
if (pa[a] == 0 && pa[b] == 0) {
ans++;
pa[a] = ans;
pa[b] = ans;
zhi[ans] = ans;
} else {
//11
while(zhi[pa[a]] !=pa[a]){
a=zhi[pa[a]]
}
if (zhi[pa[a]] != zhi[pa[b]] && pa[a] > 0 && pa[b] > 0) {
ans--;
zhi[pa[a]] = zhi[pa[b]];
} else {
//10
if (zhi[pa[a]] > 0) {
pa[b] = pa[a];
} else {
//01
pa[a] = pa[b];
}
}
}
}
cout << "1";
for (int i = 1; i <= ans; i++) {
cout << "0";
}
}