#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int n,love[N];
int ind;
unordered_map<string,int>id;
vector <int> g[N],adj[N];
bool mrk[N];
vector<int>ord;
deque<int>dag;
void dfs(int v){
mrk[v]=1;
for (int i=0;i<(int)g[v].size();i++){
int u=g[v][i];
if (!mrk[u]){
adj[v].push_back(u);
dfs(u);
}
}
ord.push_back(v);
return;
}
int used[N];
int max_maching;
bool mark[N];
void maching_dfs(int v, int par){
for(int i=0;i<(int)adj[v].size();i++){
int u=adj[v][i];
maching_dfs(u,v);
}
if(!used[v] && !used[par] && par!=0){
max_maching++;
used[v]=used[par]=1;
}
}
inline int solve1(int root){
max_maching = 0;
for(int i=0;i<(int)ord.size();i++){
int v=ord[i];
used[v]=0;
}
maching_dfs(root,0);
return max_maching;
}
inline int solve2(int root){
if(love[root]==root) return 0;
for(int i=0;i<(int)ord.size();i++){
int v=ord[i];
used[v]=0;
}
used[root]=used[love[root]]=1;
max_maching=0;
maching_dfs(root,0);
return max_maching+1+(int)(love[love[root]]==root);
}
void DFS(int v){
mrk[v]=1;
for(int i=0;i<(int)g[v].size();i++){
int u=g[v][i];
if(mrk[u]) continue;
DFS(u);
}
dag.push_back(v);
return;
}
void fsd(int v){
mrk[v]=1;
for(int i=0;i<(int)g[v].size();i++){
int u=g[v][i];
if(!mrk[u]){
adj[v].push_back(u);
fsd(u);
}
}
ord.push_back(v);
return;
}
int main() {
ios_base::sync_with_stdio(0),cin.tie(0);cout.tie(0);
cin>>n;
if(n%2){
cout<<"-1\n";
return 0;
}
for(int i=1;i<=n;i++){
string a,b;
cin>>a>>b;
if(id.count(a)==0){
id[a]=++ind;
}
if(id.count(b)==0){
id[b]=++ind;
}
int u=id[a];
int v=id[b];
love[u]=v;
g[v].push_back(u);
}
for(int i=1;i<=n;i++){
if(!mrk[i]){
DFS(i);
}
}
for(int i=1;i<=n;i++){mrk[i]=0;}
int ans=n;
while(dag.size()){
int v=dag.back();dag.pop_back();
if(mrk[v])continue;
ord.clear();fsd(v);
ans-=max(solve1(v),solve2(v));
}
cout<<ans<<'\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5248 KB |
Output is correct |
2 |
Correct |
4 ms |
5100 KB |
Output is correct |
3 |
Correct |
4 ms |
5100 KB |
Output is correct |
4 |
Correct |
4 ms |
5132 KB |
Output is correct |
5 |
Correct |
4 ms |
5100 KB |
Output is correct |
6 |
Correct |
4 ms |
5100 KB |
Output is correct |
7 |
Correct |
3 ms |
5100 KB |
Output is correct |
8 |
Correct |
3 ms |
5100 KB |
Output is correct |
9 |
Correct |
3 ms |
5100 KB |
Output is correct |
10 |
Correct |
4 ms |
5100 KB |
Output is correct |
11 |
Correct |
5 ms |
5100 KB |
Output is correct |
12 |
Correct |
5 ms |
5100 KB |
Output is correct |
13 |
Correct |
4 ms |
5100 KB |
Output is correct |
14 |
Correct |
5 ms |
5100 KB |
Output is correct |
15 |
Correct |
4 ms |
5100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5100 KB |
Output is correct |
2 |
Correct |
3 ms |
5100 KB |
Output is correct |
3 |
Correct |
5 ms |
5100 KB |
Output is correct |
4 |
Correct |
178 ms |
27408 KB |
Output is correct |
5 |
Correct |
168 ms |
18852 KB |
Output is correct |
6 |
Correct |
196 ms |
28560 KB |
Output is correct |
7 |
Correct |
3 ms |
5100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
171 ms |
18212 KB |
Output is correct |
2 |
Correct |
173 ms |
21008 KB |
Output is correct |
3 |
Correct |
95 ms |
16656 KB |
Output is correct |
4 |
Correct |
5 ms |
5100 KB |
Output is correct |
5 |
Correct |
176 ms |
27664 KB |
Output is correct |
6 |
Correct |
146 ms |
17168 KB |
Output is correct |
7 |
Correct |
161 ms |
16464 KB |
Output is correct |
8 |
Correct |
124 ms |
16272 KB |
Output is correct |
9 |
Correct |
129 ms |
15888 KB |
Output is correct |
10 |
Correct |
85 ms |
15240 KB |
Output is correct |
11 |
Correct |
4 ms |
5100 KB |
Output is correct |
12 |
Correct |
3 ms |
5100 KB |
Output is correct |
13 |
Correct |
4 ms |
5100 KB |
Output is correct |
14 |
Correct |
3 ms |
5100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5248 KB |
Output is correct |
2 |
Correct |
4 ms |
5100 KB |
Output is correct |
3 |
Correct |
4 ms |
5100 KB |
Output is correct |
4 |
Correct |
4 ms |
5132 KB |
Output is correct |
5 |
Correct |
4 ms |
5100 KB |
Output is correct |
6 |
Correct |
4 ms |
5100 KB |
Output is correct |
7 |
Correct |
3 ms |
5100 KB |
Output is correct |
8 |
Correct |
3 ms |
5100 KB |
Output is correct |
9 |
Correct |
3 ms |
5100 KB |
Output is correct |
10 |
Correct |
4 ms |
5100 KB |
Output is correct |
11 |
Correct |
5 ms |
5100 KB |
Output is correct |
12 |
Correct |
5 ms |
5100 KB |
Output is correct |
13 |
Correct |
4 ms |
5100 KB |
Output is correct |
14 |
Correct |
5 ms |
5100 KB |
Output is correct |
15 |
Correct |
4 ms |
5100 KB |
Output is correct |
16 |
Correct |
4 ms |
5100 KB |
Output is correct |
17 |
Correct |
3 ms |
5100 KB |
Output is correct |
18 |
Correct |
5 ms |
5100 KB |
Output is correct |
19 |
Correct |
178 ms |
27408 KB |
Output is correct |
20 |
Correct |
168 ms |
18852 KB |
Output is correct |
21 |
Correct |
196 ms |
28560 KB |
Output is correct |
22 |
Correct |
3 ms |
5100 KB |
Output is correct |
23 |
Correct |
171 ms |
18212 KB |
Output is correct |
24 |
Correct |
173 ms |
21008 KB |
Output is correct |
25 |
Correct |
95 ms |
16656 KB |
Output is correct |
26 |
Correct |
5 ms |
5100 KB |
Output is correct |
27 |
Correct |
176 ms |
27664 KB |
Output is correct |
28 |
Correct |
146 ms |
17168 KB |
Output is correct |
29 |
Correct |
161 ms |
16464 KB |
Output is correct |
30 |
Correct |
124 ms |
16272 KB |
Output is correct |
31 |
Correct |
129 ms |
15888 KB |
Output is correct |
32 |
Correct |
85 ms |
15240 KB |
Output is correct |
33 |
Correct |
4 ms |
5100 KB |
Output is correct |
34 |
Correct |
3 ms |
5100 KB |
Output is correct |
35 |
Correct |
4 ms |
5100 KB |
Output is correct |
36 |
Correct |
3 ms |
5100 KB |
Output is correct |
37 |
Correct |
188 ms |
21392 KB |
Output is correct |
38 |
Correct |
189 ms |
20496 KB |
Output is correct |
39 |
Correct |
157 ms |
18704 KB |
Output is correct |
40 |
Correct |
167 ms |
19364 KB |
Output is correct |
41 |
Correct |
164 ms |
18704 KB |
Output is correct |
42 |
Correct |
173 ms |
20228 KB |
Output is correct |
43 |
Correct |
176 ms |
19984 KB |
Output is correct |
44 |
Correct |
174 ms |
20112 KB |
Output is correct |
45 |
Correct |
172 ms |
20112 KB |
Output is correct |
46 |
Correct |
167 ms |
20240 KB |
Output is correct |
47 |
Correct |
142 ms |
18320 KB |
Output is correct |
48 |
Correct |
164 ms |
20112 KB |
Output is correct |
49 |
Correct |
180 ms |
23312 KB |
Output is correct |
50 |
Correct |
91 ms |
18832 KB |
Output is correct |
51 |
Correct |
4 ms |
5100 KB |
Output is correct |
52 |
Correct |
191 ms |
29840 KB |
Output is correct |
53 |
Correct |
160 ms |
19344 KB |
Output is correct |
54 |
Correct |
146 ms |
18576 KB |
Output is correct |
55 |
Correct |
122 ms |
18448 KB |
Output is correct |
56 |
Correct |
144 ms |
18192 KB |
Output is correct |
57 |
Correct |
82 ms |
17344 KB |
Output is correct |
58 |
Correct |
4 ms |
5100 KB |
Output is correct |
59 |
Correct |
4 ms |
5100 KB |
Output is correct |
60 |
Correct |
4 ms |
5100 KB |
Output is correct |
61 |
Correct |
4 ms |
5100 KB |
Output is correct |
62 |
Correct |
4 ms |
5100 KB |
Output is correct |
63 |
Correct |
4 ms |
5100 KB |
Output is correct |
64 |
Correct |
4 ms |
5100 KB |
Output is correct |
65 |
Correct |
194 ms |
29712 KB |
Output is correct |
66 |
Correct |
158 ms |
21008 KB |
Output is correct |
67 |
Correct |
183 ms |
30776 KB |
Output is correct |
68 |
Correct |
4 ms |
5100 KB |
Output is correct |
69 |
Correct |
4 ms |
5100 KB |
Output is correct |
70 |
Correct |
4 ms |
5100 KB |
Output is correct |
71 |
Correct |
4 ms |
5100 KB |
Output is correct |
72 |
Correct |
4 ms |
5100 KB |
Output is correct |
73 |
Correct |
4 ms |
5100 KB |
Output is correct |
74 |
Correct |
4 ms |
5100 KB |
Output is correct |
75 |
Correct |
4 ms |
5100 KB |
Output is correct |
76 |
Correct |
4 ms |
5100 KB |
Output is correct |
77 |
Correct |
5 ms |
5100 KB |
Output is correct |
78 |
Correct |
4 ms |
5228 KB |
Output is correct |
79 |
Correct |
4 ms |
5100 KB |
Output is correct |
80 |
Correct |
4 ms |
5100 KB |
Output is correct |
81 |
Correct |
4 ms |
5100 KB |
Output is correct |
82 |
Correct |
4 ms |
5100 KB |
Output is correct |
83 |
Correct |
4 ms |
5100 KB |
Output is correct |