-
Notifications
You must be signed in to change notification settings - Fork 0
/
267D.cpp
134 lines (132 loc) · 2.79 KB
/
267D.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <iostream>
#include <sstream>
#include <fstream>
#include <cstring>
#include <numeric>
#include <ctype.h>
#define MOD 1000000007
#define MN(a,b)a<b?a:b
#define PS system("pause")
#define exit return 0
#define inf 1000000000000000000LL
#define left (v<<1)
#define right (left|1)
#define tm ((tl+tr)>>1)
typedef long long ll;
#define S 525231
#define R0(i,n) for(int i=0;i<(int)n;i++)
#define R1(i,n) for(int i=1;i<=(int)n;i++)
using namespace std;
//vector<ll> dataMul(N), dataAdd(N);
int n, i, m, k, h, p, l, r;
string in, t;
map<string, int>MP;
vector<vector<int>>gr(222333);
vector < pair<ll, ll>>val;
vector<int>txt;
bool vis[223457];
ll dp[223457][2];
int col[223457];
int low[223457];
int timer, timein[234578], mark;
int N, M;
vector<int>ST;
void tarjan(int u, int p = -1){
ST.push_back(u);
vis[u] = 1;
low[u] = timein[u] = ++timer;
for (int &x : gr[u]){
if (vis[x]){
if (col[x] == 0){
low[u] = min(low[u], timein[x]);
}
}
else{
tarjan(x);
low[u] = min(low[u], low[x]);
}
if (dp[col[x]][0] == val[u].first){
val[u].second = min(val[u].second, dp[col[x]][1]);
}
else if (dp[col[x]][0] < val[u].first){
val[u].first = dp[col[x]][0];
val[u].second = dp[col[x]][1];
}
}
if (timein[u] == low[u]){
while (!ST.empty()){
int v = ST.back();
ST.pop_back();
col[v] = mark;
if (dp[mark][0] == val[v].first)dp[mark][1] = min(dp[mark][1], val[v].second);
else if (dp[mark][0] > val[v].first)dp[mark][0] = val[v].first, dp[mark][1] = val[v].second;
if (v == u)break;
}
mark += 1;
}
}
int main(){
cin >> N;
R0(i, N){
cin >> in;
int cnt = 0;
for (int j = 0; j < in.size(); j++){
in[j] = tolower(in[j]);
if (in[j] == 'r')++cnt;
}
if (MP.find(in) == MP.end()){
MP[in] = n++;
val.push_back({ cnt, in.size() });
}
txt.push_back(MP[in]);
}
cin >> M;
for (int i = 0; i < M; i++){
cin >> in >> t;
int cnt = 0;
for (int j = 0; j < in.size(); j++){
in[j] = tolower(in[j]);
if (in[j] == 'r')++cnt;
}
if (MP.find(in) == MP.end()){
MP[in] = n++;
val.push_back({ cnt, in.size() });
}
cnt = 0;
for (int j = 0; j < t.size(); j++){
t[j] = tolower(t[j]);
if (t[j] == 'r')++cnt;
}
if (MP.find(t) == MP.end()){
MP[t] = n++;
val.push_back({ cnt, t.size() });
}
int a = MP[in], b = MP[t];
gr[a].push_back(b);
}
for (int i = 0; i <= 200000; i++){
for (int j = 0; j < 2; j += 1){
dp[i][j] = inf;
}
}
ll len = 0, pas = 0;
mark = 1;
for (int i = 0; i < txt.size(); i++){
if (!vis[txt[i]])tarjan(txt[i]);
pas += dp[col[txt[i]]][0], len += dp[col[txt[i]]][1];
}
cout << pas << " " << len << endl;
exit;
//PS;
}