#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define ff first
#define ss second
ll ttt;
const ll INF = 1e18;
const ll MOD = 1e9 + 7;
const int N = 100007;
ll n, m, k;
string name[N];
map<string, int> revname;
vector<int>g[N];
// topological order
vector<int>order;
bool vis[N];
bool used[N];
void dfs(int u){
vis[u]=true;
for(auto v:g[u]){
if(!vis[v]){
dfs(v);
}
}
order.push_back(u);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n;
vector<pair<string,string>>vec;
for(int i=0;i<n;i++){
string a,b;
cin>>a>>b;
vec.push_back({a,b});
name[i]=a;
revname[a]=i;
}
if(n%2==1){
cout<<-1<<endl;
return 0;
}
for(auto e:vec){
string a=e.ff;
string b=e.ss;
int u=revname[a];
int v=revname[b];
g[u].push_back(v);
}
for(int i=0;i<n;i++){
if(!vis[i]){
dfs(i);
}
}
reverse(order.begin(),order.end());
for(int v=0;v<n;v++){
if(g[g[v][0]][0]==v&&g[v][0]!=v){
used[v]=true;
}
}
int tuzik=0;
int ans=0;
for(int v:order){
if(!used[v]){
int to=g[v][0];
if(used[to]){
tuzik++;
}
else{
ans++;
used[to]=true;
}
used[v]=true;
}
}
cout<<ans+tuzik<<endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5716 KB |
Output is correct |
2 |
Correct |
4 ms |
5804 KB |
Output is correct |
3 |
Correct |
4 ms |
5804 KB |
Output is correct |
4 |
Correct |
3 ms |
5716 KB |
Output is correct |
5 |
Correct |
3 ms |
5800 KB |
Output is correct |
6 |
Correct |
3 ms |
5804 KB |
Output is correct |
7 |
Correct |
3 ms |
5716 KB |
Output is correct |
8 |
Correct |
4 ms |
5800 KB |
Output is correct |
9 |
Correct |
3 ms |
5772 KB |
Output is correct |
10 |
Correct |
5 ms |
5700 KB |
Output is correct |
11 |
Correct |
3 ms |
5808 KB |
Output is correct |
12 |
Correct |
4 ms |
5716 KB |
Output is correct |
13 |
Correct |
3 ms |
5716 KB |
Output is correct |
14 |
Correct |
3 ms |
5800 KB |
Output is correct |
15 |
Correct |
3 ms |
5716 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5716 KB |
Output is correct |
2 |
Correct |
4 ms |
5716 KB |
Output is correct |
3 |
Correct |
3 ms |
5716 KB |
Output is correct |
4 |
Correct |
215 ms |
30232 KB |
Output is correct |
5 |
Correct |
203 ms |
26256 KB |
Output is correct |
6 |
Correct |
217 ms |
30976 KB |
Output is correct |
7 |
Correct |
88 ms |
21960 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
223 ms |
26172 KB |
Output is correct |
2 |
Correct |
197 ms |
27008 KB |
Output is correct |
3 |
Correct |
174 ms |
26280 KB |
Output is correct |
4 |
Correct |
105 ms |
21988 KB |
Output is correct |
5 |
Correct |
260 ms |
28048 KB |
Output is correct |
6 |
Correct |
230 ms |
26252 KB |
Output is correct |
7 |
Correct |
208 ms |
26408 KB |
Output is correct |
8 |
Correct |
197 ms |
26176 KB |
Output is correct |
9 |
Correct |
211 ms |
26208 KB |
Output is correct |
10 |
Correct |
181 ms |
26276 KB |
Output is correct |
11 |
Correct |
5 ms |
5716 KB |
Output is correct |
12 |
Correct |
4 ms |
5716 KB |
Output is correct |
13 |
Correct |
4 ms |
5716 KB |
Output is correct |
14 |
Correct |
4 ms |
5808 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5716 KB |
Output is correct |
2 |
Correct |
4 ms |
5804 KB |
Output is correct |
3 |
Correct |
4 ms |
5804 KB |
Output is correct |
4 |
Correct |
3 ms |
5716 KB |
Output is correct |
5 |
Correct |
3 ms |
5800 KB |
Output is correct |
6 |
Correct |
3 ms |
5804 KB |
Output is correct |
7 |
Correct |
3 ms |
5716 KB |
Output is correct |
8 |
Correct |
4 ms |
5800 KB |
Output is correct |
9 |
Correct |
3 ms |
5772 KB |
Output is correct |
10 |
Correct |
5 ms |
5700 KB |
Output is correct |
11 |
Correct |
3 ms |
5808 KB |
Output is correct |
12 |
Correct |
4 ms |
5716 KB |
Output is correct |
13 |
Correct |
3 ms |
5716 KB |
Output is correct |
14 |
Correct |
3 ms |
5800 KB |
Output is correct |
15 |
Correct |
3 ms |
5716 KB |
Output is correct |
16 |
Correct |
3 ms |
5716 KB |
Output is correct |
17 |
Correct |
4 ms |
5716 KB |
Output is correct |
18 |
Correct |
3 ms |
5716 KB |
Output is correct |
19 |
Correct |
215 ms |
30232 KB |
Output is correct |
20 |
Correct |
203 ms |
26256 KB |
Output is correct |
21 |
Correct |
217 ms |
30976 KB |
Output is correct |
22 |
Correct |
88 ms |
21960 KB |
Output is correct |
23 |
Correct |
223 ms |
26172 KB |
Output is correct |
24 |
Correct |
197 ms |
27008 KB |
Output is correct |
25 |
Correct |
174 ms |
26280 KB |
Output is correct |
26 |
Correct |
105 ms |
21988 KB |
Output is correct |
27 |
Correct |
260 ms |
28048 KB |
Output is correct |
28 |
Correct |
230 ms |
26252 KB |
Output is correct |
29 |
Correct |
208 ms |
26408 KB |
Output is correct |
30 |
Correct |
197 ms |
26176 KB |
Output is correct |
31 |
Correct |
211 ms |
26208 KB |
Output is correct |
32 |
Correct |
181 ms |
26276 KB |
Output is correct |
33 |
Correct |
5 ms |
5716 KB |
Output is correct |
34 |
Correct |
4 ms |
5716 KB |
Output is correct |
35 |
Correct |
4 ms |
5716 KB |
Output is correct |
36 |
Correct |
4 ms |
5808 KB |
Output is correct |
37 |
Correct |
264 ms |
26288 KB |
Output is correct |
38 |
Incorrect |
218 ms |
26972 KB |
Output isn't correct |
39 |
Halted |
0 ms |
0 KB |
- |