Problem Link
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 30;
int parent[MAXN];
void opt()
{
for(int i = 1; i < MAXN; i++)
parent[i] = i;
}
int find_parent(int x)
{
if(parent[x]==x)
return x;
return parent[x] = find_parent(parent[x]);
}
void make_union(int x, int y)
{
parent[find_parent(x)] = find_parent(y);
}
bool isUnion(int x, int y)
{
return find_parent(x) == find_parent(y);
}
int main()
{
int T, t;
cin>>T;
t = T;
getchar();
while(T--)
{
if(t-1!=T)
cout<<endl;
opt();
int n;
char ch;
string s;
cin>>ch;
getchar();
n = 1 + (ch - 'A');
while(getline(cin,s) && s!="")
{
int a, b;
a = 1 + (s[0] - 'A');
b = 1 + (s[1] - 'A');
if(!isUnion(a,b))
{
make_union(a,b);
n--;
}
}
cout<<n<<endl;
}
return 0;
}
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 30;
int parent[MAXN];
void opt()
{
for(int i = 1; i < MAXN; i++)
parent[i] = i;
}
int find_parent(int x)
{
if(parent[x]==x)
return x;
return parent[x] = find_parent(parent[x]);
}
void make_union(int x, int y)
{
parent[find_parent(x)] = find_parent(y);
}
bool isUnion(int x, int y)
{
return find_parent(x) == find_parent(y);
}
int main()
{
int T, t;
cin>>T;
t = T;
getchar();
while(T--)
{
if(t-1!=T)
cout<<endl;
opt();
int n;
char ch;
string s;
cin>>ch;
getchar();
n = 1 + (ch - 'A');
while(getline(cin,s) && s!="")
{
int a, b;
a = 1 + (s[0] - 'A');
b = 1 + (s[1] - 'A');
if(!isUnion(a,b))
{
make_union(a,b);
n--;
}
}
cout<<n<<endl;
}
return 0;
}
No comments:
Post a Comment