Codeforces 467D. Fedor and Essay, codeforces467d

Source: Internet
Author: User

Codeforces 467D. Fedor and Essay, codeforces467d


Tarjan shrink circle + dfs + map

D. Fedor and Essaytime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard 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 wocould 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.

Input

The first line contains a single integerM(1 digit ≤ DigitMLimit ≤ limit 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 integerN(0 bytes ≤ bytesNLimit ≤ limit 105)-the number of pairs of words in synonym dictionary.I-Th of the nextNLines contains two space-separated non-empty wordsXIAndYI. They mean that wordXICan be replaced with wordYI(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.

Output

Print two integers-the minimum number of letters «R» in an optimal essay and the minimum length of an optimal essay.

Sample test (s) input
3AbRb r Zz4xR abRbaA xrzz Zxr y
Output
2 6
Input
2RuruRu fedya1ruruRU fedor
Output
1 10

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <string>#include <map>#include <vector>using namespace std;typedef long long int LL;const int maxn=100100;int n,m,id=1;LL Len[maxn],R[maxn];map<string,int> mp;typedef pair<int,int> PII;vector<PII> bian;struct Edge{int from,to,next;}edge[maxn],edge2[maxn];int Adj[maxn],Size;int Adj2[maxn],Size2;void init(){Size=0; memset(Adj,-1,sizeof(Adj));}void init2(){Size2=0; memset(Adj2,-1,sizeof(Adj2));}void Add_Edge(int u,int v){edge[Size].from=u; edge[Size].to=v; edge[Size].next=Adj[u]; Adj[u]=Size++;}void Add_Edge2(int u,int v){edge2[Size2].from=u; edge2[Size2].to=v; edge2[Size2].next=Adj2[u]; Adj2[u]=Size2++;}int Low[maxn],DFN[maxn],Stack[maxn],Belong[maxn];int Index,top,scc;bool Instack[maxn]; int num[maxn];LL GoalR[maxn],GoalLen[maxn];void tarjan(int u){int v;Low[u]=DFN[u]=++Index;Stack[top++]=u;Instack[u]=true;for(int i=Adj[u];~i;i=edge[i].next){v=edge[i].to;if(!DFN[v]){tarjan(v);Low[u]=min(Low[u],Low[v]);}else if(Instack[v]){Low[u]=min(Low[u],DFN[v]);}}if(Low[u]==DFN[u]){scc++;do{v=Stack[--top];Instack[v]=false;num[scc]++;Belong[v]=scc;if(R[v]<GoalR[scc]){GoalR[scc]=R[v];GoalLen[scc]=Len[v];}else if(R[v]==GoalR[scc]&&Len[v]<GoalLen[scc]){GoalLen[scc]=Len[v];}}while(v!=u);}}void scc_solve(int n)///id{memset(DFN,0,sizeof(DFN));memset(Instack,0,sizeof(Instack));memset(num,0,sizeof(num));memset(GoalR,63,sizeof(GoalR));memset(GoalLen,63,sizeof(GoalLen));Index=scc=top=0;for(int i=1;i<=n;i++)if(!DFN[i]) tarjan(i);}void ReBuild(){init2();for(int i=0;i<m;i++){int u=bian[i].first,v=bian[i].second;int U=Belong[u],V=Belong[v];if(U==V) continue;Add_Edge2(U,V);}}vector<string> str;bool vis[maxn];LL dp_r[maxn],dp_l[maxn];void dfs(int u){if(vis[u]==true) return ;vis[u]=true;LL R=GoalR[u],L=GoalLen[u];for(int i=Adj2[u];~i;i=edge2[i].next){int v=edge2[i].to;dfs(v);if(dp_r[v]<R){R=dp_r[v]; L=dp_l[v];}else if(dp_r[v]==R&&dp_l[v]<L){L=dp_l[v];}}dp_r[u]=R; dp_l[u]=L;}int main(){scanf("%d",&n);string word;for(int i=0;i<n;i++){cin>>word;for(int i=0,sz=word.size();i<sz;i++){if(word[i]>='A'&&word[i]<='Z')word[i]=word[i]-'A'+'a';}str.push_back(word);if(mp[word]==0) mp[word]=id++;else continue;int it=mp[word];Len[it]=word.size();for(int i=0;i<Len[it];i++){if(word[i]=='r')R[it]++;}}scanf("%d",&m);string Fr,To;init();for(int _=0;_<m;_++){cin>>Fr>>To;int r1=0,r2=0;for(int i=0,sz=Fr.size();i<sz;i++){if(Fr[i]>='A'&&Fr[i]<='Z')Fr[i]=Fr[i]-'A'+'a';if(Fr[i]=='r') r1++;}if(mp[Fr]==0) {Len[id]=Fr.size();R[id]=r1;mp[Fr]=id++;}for(int i=0,sz=To.size();i<sz;i++){if(To[i]>='A'&&To[i]<='Z')To[i]=To[i]-'A'+'a';if(To[i]=='r') r2++;}if(mp[To]==0) {Len[id]=To.size();R[id]=r2;mp[To]=id++;}bian.push_back(make_pair(mp[Fr],mp[To]));Add_Edge(mp[Fr],mp[To]);}scc_solve(id);ReBuild();LL RRR=0,LLL=0;for(int i=0;i<n;i++){int id=mp[str[i]];dfs(Belong[id]);RRR+=dp_r[Belong[id]];LLL+=dp_l[Belong[id]];}cout<<RRR<<" "<<LLL<<endl;return 0;}








Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.