Codeforces Round #267 (Div. 2) D. Fedor and Essay tarjan缩点
2 seconds
256 megabytes
standard input
standard output
After you had helped Fedor to find friends in the «Call of Soldiers 3» game, he stopped studying completely. Today, the English teacher told him to prepare an essay. Fedor didn't want to prepare the essay, so he asked Alex for help. Alex came to help and wrote the essay for Fedor. But Fedor didn't like the essay at all. Now Fedor is going to change the essay using the synonym dictionary of the English language.
Fedor does not want to change the meaning of the essay. So the only change he would do: change a word from essay to one of its synonyms, basing on a replacement rule from the dictionary. Fedor may perform this operation any number of times.
As a result, Fedor wants to get an essay which contains as little letters «R» (the case doesn't matter) as possible. If there are multiple essays with minimum number of «R»s he wants to get the one with minimum length (length of essay is the sum of the lengths of all the words in it). Help Fedor get the required essay.
Please note that in this problem the case of letters doesn't matter. For example, if the synonym dictionary says that word cat can be replaced with word DOG, then it is allowed to replace the word Cat with the word doG.
The first line contains a single integer m (1 ≤ m ≤ 105) — the number of words in the initial essay. The second line contains words of the essay. The words are separated by a single space. It is guaranteed that the total length of the words won't exceed 105 characters.
The next line contains a single integer n (0 ≤ n ≤ 105) — the number of pairs of words in synonym dictionary. The i-th of the next n lines contains two space-separated non-empty words xi and yi. They mean that word xi can be replaced with word yi (but not vise versa). It is guaranteed that the total length of all pairs of synonyms doesn't exceed 5·105 characters.
All the words at input can only consist of uppercase and lowercase letters of the English alphabet.
Print two integers — the minimum number of letters «R» in an optimal essay and the minimum length of an optimal essay.
3
AbRb r Zz
4
xR abRb
aA xr
zz Z
xr y
2 6
2
RuruRu fedya
1
ruruRU fedor
1 10
题意:给你n个字符串,你可以把字符串转换成另外的一个,需要求使得‘R’数量最小的字符串,求‘R‘最小的数量,同样数量求最小的长度;
m个转换,两个字符串 a,b;指可以单向传递;不区分大小写;
思路:有向图,环找出来,找这个环最小的’R‘,R相同,长度小;
形成无环DAG,在dfs找可以转换的最小的'R’;
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
#include<bitset>
#include<time.h>
using namespace std;
#define LL long long
#define pi (4*atan(1.0))
#define eps 1e-4
#define bug(x) cout<<"bug"<<x<<endl;
const int N=1e5+,M=1e6+,inf=1e9+,MOD=1e9+;
const LL INF=1e18+,mod=1e9+; struct is
{
int u,v;
int next;
}edge[N];
int head[N];
int belong[N];
int dfn[N];
int low[N];
int stackk[N];
int instack[N];
int number[N];
int jiedge,lu,bel,top; int tot,r[N],l[N];
pair<int,int>f[N];
void update(int u,int v)
{
jiedge++;
edge[jiedge].u=u;
edge[jiedge].v=v;
edge[jiedge].next=head[u];
head[u]=jiedge;
}
void dfs(int x)
{
dfn[x]=low[x]=++lu;
stackk[++top]=x;
instack[x]=;
for(int i=head[x];i;i=edge[i].next)
{
if(!dfn[edge[i].v])
{
dfs(edge[i].v);
low[x]=min(low[x],low[edge[i].v]);
}
else if(instack[edge[i].v])
low[x]=min(low[x],dfn[edge[i].v]);
}
if(low[x]==dfn[x])
{
int sum=;
bel++;
int ne,r1=inf,l1=inf;
do
{
sum++;
ne=stackk[top--];
if(r[ne]<r1)
{
r1=r[ne];
l1=l[ne];
}
else if(r[ne]==r1)
l1=min(l1,l[ne]);
belong[ne]=bel;
instack[ne]=;
}while(x!=ne);
f[bel]=make_pair(r1,l1);
number[bel]=sum;
}
}
void tarjan()
{
memset(dfn,,sizeof(dfn));
bel=lu=top=;
for(int i=;i<=tot;i++)
if(!dfn[i])
dfs(i);
} vector<int> edge2[N];
int vis2[N];
void dfs2(int u,int fa)
{
for(int i=;i<edge2[u].size();i++)
{
int v=edge2[u][i];
if(v==fa)continue;
dfs2(v,u);
if(f[u].first>f[v].first)
f[u]=f[v];
else if(f[u].first==f[v].first&&f[u].second>f[v].second)
f[u]=f[v];
}
}
string s[N];
map<string,int>mp;
int si[N];
int main()
{
int n;
scanf("%d",&n);
for(int i=;i<=n;i++)
{
cin>>s[i];
int ri=;
for(int j=;j<s[i].size();j++)
{
if(s[i][j]>='A'&&s[i][j]<='Z')
s[i][j]=s[i][j]-'A'+'a';
if(s[i][j]=='r')ri++;
}
si[i]=ri;
}
int m;
scanf("%d",&m);
for(int i=;i<=m;i++)
{
string a,b;
cin>>a>>b;
int ri=;
for(int j=;j<a.size();j++)
{
if(a[j]>='A'&&a[j]<='Z')
a[j]=a[j]-'A'+'a';
if(a[j]=='r')ri++;
}
if(mp.find(a)==mp.end())mp[a]=++tot;
r[mp[a]]=ri;l[mp[a]]=a.size();
ri=;
for(int j=;j<b.size();j++)
{
if(b[j]>='A'&&b[j]<='Z')
b[j]=b[j]-'A'+'a';
if(b[j]=='r')ri++;
}
if(mp.find(b)==mp.end())mp[b]=++tot;
r[mp[b]]=ri;l[mp[b]]=b.size();
update(mp[a],mp[b]);
}
tarjan();
memset(head,,sizeof(head));
for(int i=;i<=jiedge;i++)
{
if(belong[edge[i].u]!=belong[edge[i].v])
{
edge2[belong[edge[i].u]].push_back(belong[edge[i].v]);
vis2[belong[edge[i].v]]=;
}
}
//for(int i=1;i<=tot;i++)
//cout<<belong[i]<<endl;
//cout<<bel<<endl;
for(int i=;i<=bel;i++)
if(!vis2[i])
dfs2(i,-);
LL ans=,out=;
for(int i=;i<=n;i++)
{
if(mp.find(s[i])==mp.end())ans+=si[i],out+=s[i].size();
else
{
ans+=f[belong[mp[s[i]]]].first;
out+=f[belong[mp[s[i]]]].second;
}
}
printf("%lld %lld\n",ans,out);
return ;
}
Codeforces Round #267 (Div. 2) D. Fedor and Essay tarjan缩点的更多相关文章
- Codeforces Round #267 Div.2 D Fedor and Essay -- 强连通 DFS
题意:给一篇文章,再给一些单词替换关系a b,表示单词a可被b替换,可多次替换,问最后把这篇文章替换后(或不替换)能达到的最小的'r'的个数是多少,如果'r'的个数相等,那么尽量是文章最短. 解法:易 ...
- Codeforces Round #267 (Div. 2) B. Fedor and New Game【位运算/给你m+1个数让你判断所给数的二进制形式与第m+1个数不相同的位数是不是小于等于k,是的话就累计起来】
After you had helped George and Alex to move in the dorm, they went to help their friend Fedor play ...
- Codeforces Round #267 (Div. 2) B. Fedor and New Game
After you had helped George and Alex to move in the dorm, they went to help their friend Fedor play ...
- 01背包 Codeforces Round #267 (Div. 2) C. George and Job
题目传送门 /* 题意:选择k个m长的区间,使得总和最大 01背包:dp[i][j] 表示在i的位置选或不选[i-m+1, i]这个区间,当它是第j个区间. 01背包思想,状态转移方程:dp[i][j ...
- Codeforces Round #267 (Div. 2) C. George and Job(DP)补题
Codeforces Round #267 (Div. 2) C. George and Job题目链接请点击~ The new ITone 6 has been released recently ...
- Codeforces Round #267 (Div. 2)D(DFS+单词hash+简单DP)
D. Fedor and Essay time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces Round #390 (Div. 2) D. Fedor and coupons(区间最大交集+优先队列)
http://codeforces.com/contest/754/problem/D 题意: 给定几组区间,找k组区间,使得它们的公共交集最大. 思路: 在k组区间中,它们的公共交集=k组区间中右端 ...
- Codeforces Round #267 (Div. 2)
A #include <iostream> #include<cstdio> #include<cstring> #include<algorithm> ...
- Codeforces Round #267 (Div. 2) C. George and Job DP
C. George and Job The new ITone 6 has been released ...
随机推荐
- JXNU暑期选拔赛
最小的数 Time Limit : 3000/1000ms (Java/Other) Memory Limit : 65535/32768K (Java/Other) Total Submissi ...
- Redis内存分析方法
一般会采用 bgsave 生成 dump.rdb 文件,再结合 redis-rdb-tools 和 sqlite 来进行静态分析. BGSAVE:在后台异步(Asynchronously)保存当前数据 ...
- linux 下面压缩、解压.rar文件
一,解压问题 在网上下东西的时候,经常会遇到.rar后缀的文件,我用tar解压,解压不出,上网找啊找,一直没找到什么合适的工具来压缩和解压.rar后缀的文件,现在我找到了. 二,rar和unrar安装 ...
- m3u8文件下载合并的一种方法
# -*- coding: utf-8 -*- """ Created on Wed Mar 14 15:09:14 2018 @author: Y "&quo ...
- FileZilla连接腾讯云Centos7
现在需要使用ftp快速上传资料去云机备份, 于是想到FileZilla. 生成密匙文件 登录腾讯云--ssh密匙 FileZilla Client 导入密匙文件 填写登录信息 连接 另外记得开放22端 ...
- 20145308 《网络对抗》 Web应用 学习总结
20145308 <网络对抗> Web应用 学习总结 实验内容 (1)Web前端HTML (2)Web前端javascipt (3)Web后端:MySQL基础:正常安装.启动MySQL,建 ...
- vue中如何使用echarts
在vue中使用echarts主要是注意如何与vue生命周期相结合,从而做到数据驱动视图刷新 主要是以下几步: echarts的option配置项放在在data(){}或者computed(){}中 在 ...
- 【Python56--爬取妹子图】
爬取网站的思路 第一步:首先分析爬取网站的连接地址特性,发现翻页图片的时候连接:http://www.mmjpg.com/mm/1570 ,http://www.mmjpg.com/mm/1569, ...
- topcoder srm 681 div1
problem1 link 二分答案.然后判断.将所有的机器按照$a_{i}$排序,$a_{i}$相同的按照$b_{i}$排序.用一个优先队列维护这些机器.这样对于第$i$个部分,拿出队列开始的机器来 ...
- topcoder srm 704 div1
1.对于一棵树上的一个节点$u$,定义$f(u)$表示树上距离$u$最远的节点到$u$的距离.给出每个节点的$f$值,构造出这棵树. 思路:找到树的主干,然后不在主干上的节点一定可以连接到主干的某个节 ...