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
题意:用最少的字母对,使得两个字符串相等。字母对之间的字母可以相互转换。
解题思路:把每一对字母之间建边,然后求最小生成树即可。注意,边是双向的。
#include <bits/stdc++.h>
3 abb dadoutput
2 a d b ainputCopy
8 drpepper cocacolaoutput
7 l e e d d c c p p o o r r aNoteIn 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;
}
相关文章推荐
- CodeForces 125E MST Company(最小度限制生成树)
- CodeForces - 891C: Envy(可撤销的并查集&最小生成树)
- Codeforces 892E - Envy 【最小生成树】
- codeforces 76A Gift 最小生成树
- codeforces 733 F. Drivers Dissatisfaction(最小生成树+lca+倍增去环)
- Codeforces 827 D 最小生成树+倍增 解题报告
- CodeForces 141E: ...(最小生成树)
- codeforces 632F. Magic Matrix (最小生成树)
- CodeForces 76A Gift - 最小生成树
- CodeForces 609 E.Minimum spanning tree for each edge(最小生成树-Kruskal+在线倍增LCA)
- [LCT维护最小生成树 || CDQ分治 || 线段树 并查集 dfs树] Codeforces 603E #334 (Div. 1) E. Pastoral Oddities
- CodeForces - 472D Design Tutorial: Inverse the Problem——最小生成树
- 校内赛 codeforces 827D【最小生成树】【树链剖分】 解题报告
- CodeForces 472D Design Tutorial: Inverse the Problem (最小生成树)
- Codeforces 609E Minimum spanning tree for each edge 树链剖分+RMQ(st算法)+最小生成树
- 最小生成树(kruskal)Codeforces 472D
- Codeforces 125E MST Company (单度限制最小生成树)
- CodeForces 733 F.Drivers Dissatisfaction(最小生成树-Kruskal+在线倍增法)
- CodeForces 472D Design Tutorial: Inverse the Problem (最小生成树+lca)
- Codeforces 892/E Envy 最小生成树的query