[BOJ] 6264 Sub-dictionary

Sub-dictionary

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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include <bits/stdc++.h>

#define MAX_N 100001
#define ll long long
#define pll pair<ll, ll>
#define vpll vector<pll>
#define vll vector<ll>
#define vvll vector<vll>
#define vs vector<string>
#define vvs vector<vs>
#define usll unordered_set<ll>
#define all(a) begin(a), end(a)
using namespace std;

ll rep[MAX_N];
bool vis1[MAX_N], vis2[MAX_N], vis3[MAX_N], vis4[MAX_N];
vll st;

ll n;
void dfs1(ll u, vvll& adj) {
vis1[u] = true;
for(auto& v : adj[u])
if(!vis1[v])
dfs1(v, adj);
st.push_back(u);
}

void dfs2(ll u, ll r, vvll& radj) {
vis2[u] = true;
rep[u] = r;
for(auto& v : radj[u])
if(!vis2[v])
dfs2(v,r,radj);
}

void init() {
memset(vis1,0,sizeof(vis1));
memset(vis2,0,sizeof(vis2));
memset(vis3,0,sizeof(vis3));
memset(vis4,0,sizeof(vis4));
memset(rep, 0, sizeof(rep));
}

void dfs3(ll u, vvll& radj, usll& res) {
vis4[u] = true;
res.insert(u);
for(auto& v : radj[u]) {
if (!vis4[v] and rep[u] != rep[v])
dfs3(v, radj, res);
}
}

usll ts(ll id, vpll& edges, vvll& radj) {
unordered_map<ll,ll> ind;
unordered_map<ll, vll> repmp;
vvll compAdj(id);
for(ll i = 1; i < id; i++)
repmp[rep[i]].push_back(i);

for(auto& [u, v] : edges) {
if(rep[u] == rep[v]) continue;
ind[rep[v]] += 1;
compAdj[rep[u]].push_back(rep[v]);
}

queue<ll> q;
usll res;
for(auto& [r, mem]: repmp) {
if(ind[r] == 0)
q.push(r);
}

while(!q.empty()) {
auto r = q.front(); q.pop();
bool clear = true;
for(auto& mem : repmp[r]) {
if(vis3[mem]) continue;
bool add = true;
for(auto& v : radj[mem]) {
if(vis3[v]) continue;
add = false;
break;
}
if(add) vis3[mem] = true;
else clear = false;
}
if(!clear) {
for(auto& mem : repmp[r]) {
if(vis3[mem]) continue;
vis3[mem] = true;
dfs3(mem, radj, res);
}
}

for(auto& v : compAdj[r]) {
if(--ind[v] == 0)
q.push(v);
}
}

return res;
}

vs solve(vvs& dicts) {
ll id = 1;
init();
unordered_map<string, ll> idmp;
unordered_map<ll, string> ridmp;
for(auto& dict : dicts) {
for(auto& s : dict) {
if(idmp.count(s)) continue;
idmp[s] = id++;
ridmp[idmp[s]] = s;
}
}

vvll adj(id), radj(id);
vpll edges;
for(auto& dict: dicts) {
unordered_set<ll> vis;
ll v = idmp[dict[0]];
for(ll i = 1; i < dict.size(); i++) {
ll u = idmp[dict[i]];
if(vis.count(u)) continue;
vis.insert(u);
adj[u].push_back(v);
radj[v].push_back(u);
edges.push_back({u,v});
}
}

for(ll i = 1; i < id; i++) {
if(!vis1[i])
dfs1(i,adj);
}

while(!st.empty()) {
auto u = st.back(); st.pop_back();
if(!vis2[u])
dfs2(u,u,radj);
}

vs res;

for(auto& u : ts(id, edges, radj))
res.push_back(ridmp[u]);

sort(all(res));

return res;
}

vs split(string& dict) {
int i = 0;
string s = "";
vs res;
while(i < dict.length()) {
while(i < dict.length() and dict[i] != ' ')
s.push_back(dict[i++]);
i++;
res.push_back(s);
s = "";
}
return res;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
while(true) {
cin>>n;
if(n == 0) break;
string dict;
vvs dicts(n);
getline(cin, dict);
while(n--) {
getline(cin, dict);
dicts[n] = split(dict);
}
auto res = solve(dicts);
cout<<res.size()<<'\n';
for(auto& s : res) cout<<s<<' ';cout<<'\n';
}

return 0;
}

Author: Song Hayoung
Link: https://songhayoung.github.io/2022/04/23/PS/BOJ/6264/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.