#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,roots;
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);
}
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++){
mrk[i]=0;
if(love[i]==i) roots.push_back(i);
}
int ans=n;
for (int j=0;j<(int)roots.size();j++){
int i=roots[j];
//if (mrk[]) continue;
int root=i;
//mark[i]=1;
//if (!mark[love[root]]){
// root=love[root];
// mark[root]=1;
//}
ord.clear();dfs(root);
ans-=solve1(root);
}
cout<<ans<<'\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5100 KB |
Output is correct |
2 |
Correct |
4 ms |
5100 KB |
Output is correct |
3 |
Correct |
3 ms |
5100 KB |
Output is correct |
4 |
Incorrect |
4 ms |
5100 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5100 KB |
Output is correct |
2 |
Incorrect |
4 ms |
5100 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
154 ms |
17948 KB |
Output is correct |
2 |
Correct |
166 ms |
22800 KB |
Output is correct |
3 |
Correct |
110 ms |
19344 KB |
Output is correct |
4 |
Correct |
4 ms |
5100 KB |
Output is correct |
5 |
Correct |
166 ms |
29712 KB |
Output is correct |
6 |
Correct |
150 ms |
19108 KB |
Output is correct |
7 |
Correct |
179 ms |
18576 KB |
Output is correct |
8 |
Correct |
128 ms |
18676 KB |
Output is correct |
9 |
Correct |
124 ms |
17808 KB |
Output is correct |
10 |
Correct |
74 ms |
16904 KB |
Output is correct |
11 |
Correct |
4 ms |
5100 KB |
Output is correct |
12 |
Correct |
4 ms |
5100 KB |
Output is correct |
13 |
Correct |
4 ms |
5100 KB |
Output is correct |
14 |
Correct |
4 ms |
5248 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5100 KB |
Output is correct |
2 |
Correct |
4 ms |
5100 KB |
Output is correct |
3 |
Correct |
3 ms |
5100 KB |
Output is correct |
4 |
Incorrect |
4 ms |
5100 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |