您的位置:首页 > 产品设计 > UI/UE

CodeForces - 939D Love Rescue (最小生成树)

2018-03-30 15:06 417 查看
Love Rescuetime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputValya and Tolya are an ideal pair, but they quarrel sometimes. Recently, Valya took offense at her boyfriend because he came to her in t-shirt with lettering that differs from lettering on her pullover. Now she doesn't want to see him and Tolya is seating at his room and crying at her photos all day long.This story could be very sad but fairy godmother (Tolya's grandmother) decided to help them and restore their relationship. She secretly took Tolya's t-shirt and Valya's pullover and wants to make the letterings on them same. In order to do this, for one unit of mana she can buy a spell that can change some letters on the clothes. Your task is calculate the minimum amount of mana that Tolya's grandmother should spend to rescue love of Tolya and Valya.More formally, letterings on Tolya's t-shirt and Valya's pullover are two strings with same length n consisting only of lowercase English letters. Using one unit of mana, grandmother can buy a spell of form (c1, c2) (where c1 and c2 are some lowercase English letters), which can arbitrary number of times transform a single letter c1 to c2 and vise-versa on both Tolya's t-shirt and Valya's pullover. You should find the minimum amount of mana that grandmother should spend to buy a set of spells that can make the letterings equal. In addition you should output the required set of spells.InputThe first line contains a single integer n (1 ≤ n ≤ 105) — the length of the letterings.The second line contains a string with length n, consisting of lowercase English letters — the lettering on Valya's pullover.The third line contains the lettering on Tolya's t-shirt in the same format.OutputIn the first line output a single integer — the minimum amount of mana t required for rescuing love of Valya and Tolya.In the next t lines output pairs of space-separated lowercase English letters — spells that Tolya's grandmother should buy. Spells and letters in spells can be printed in any order.If there are many optimal answers, output any.ExamplesinputCopy
3
abb
dad
output
2
a d
b a
inputCopy
8
drpepper
cocacola
output
7
l e
e d
d c
c p
p o
o r
r a
NoteIn first example it's enough to buy two spells: ('a','d') and ('b','a'). Then first letters will coincide when we will replace letter 'a' with 'd'. Second letters will coincide when we will replace 'b' with 'a'. Third letters will coincide when we will at first replace 'b' with 'a' and then 'a' with 'd'.
题意:用最少的字母对,使得两个字符串相等。字母对之间的字母可以相互转换。
解题思路:把每一对字母之间建边,然后求最小生成树即可。注意,边是双向的。

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int MOD=1e9+7;
struct edge{
int v1;
int v2;
int next;
}e[100005];
int head[100005];
int edge_num;
void insert_edge(int v1,int v2){
e[edge_num].v1=v1;
e[edge_num].v2=v2;
e[edge_num].next=head[v1];
head[v1]=edge_num++;
}
int G[255][255];
int pre[500];
int find(int x){
return pre[x]==x?x:pre[x]=find(pre[x]);
}
void join(int x,int y){
int fx=find(x);
int fy=find(y);
if(fy!=fx)
pre[fy]=fx;
}
int main()
{
memset(head,-1,sizeof(head));
edge_num=0;
for(int i=0;i<500;i++)
pre[i]=i;
int len;
cin>>len;
string a,b;
cin>>a>>b;
for(int i=0;i<len;i++){
if(a[i]!=b[i]){
if(G[a[i]][b[i]]==0){
G[a[i]][b[i]]=G[b[i]][a[i]]=1;
insert_edge(a[i],b[i]);
insert_edge(b[i],a[i]);
}
}
}
vector<pair<char,char> >ans;
for(int i=0;i<edge_num;i++){
int u=e[i].v1;
int v=e[i].v2;
if(find(u)==find(v))
continue;
else{
join(u,v);
ans.push_back(make_pair(u,v));
}
}
cout<<ans.size()<<endl;
for(auto &t:ans){
cout<<t.first<<" "<<t.second<<endl;
}
return 0;
}

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: