#include <bits/stdc++.h>
using namespace std;
const int nx=1e5+5;
int n, t, vs[nx], l[nx], res, cnt, lv[nx], used[nx];
string su[nx], sv[nx];
map<string, int> mp;
vector<int> d[nx];
void dfs(int u, int p)
{
for (auto v:d[u]) if (v!=p) dfs(v, u);
if (u!=p&&!used[u]&&!used[p]) res--, used[u]=used[p]=1;
}
int main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin>>n;
res=n;
for (int i=1; i<=n; i++) cin>>su[i]>>sv[i], mp[su[i]]=mp[sv[i]]=0;
if (n%2) return cout<<-1, 0;
for (auto &[x, y]:mp) y=++t;
for (int i=1; i<=n; i++)
{
l[mp[su[i]]]=mp[sv[i]];
if (mp[su[i]]!=mp[sv[i]]) lv[mp[sv[i]]]=1, d[mp[su[i]]].push_back(mp[sv[i]]);
}
for (int i=1; i<=n; i++) if (!lv[i]) dfs(i, i);
cout<<res;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
9048 KB |
Output is correct |
2 |
Correct |
2 ms |
9052 KB |
Output is correct |
3 |
Correct |
2 ms |
9052 KB |
Output is correct |
4 |
Runtime error |
644 ms |
1048576 KB |
Execution killed with signal 9 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
9048 KB |
Output is correct |
2 |
Incorrect |
3 ms |
9048 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
321 ms |
23064 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
9048 KB |
Output is correct |
2 |
Correct |
2 ms |
9052 KB |
Output is correct |
3 |
Correct |
2 ms |
9052 KB |
Output is correct |
4 |
Runtime error |
644 ms |
1048576 KB |
Execution killed with signal 9 |
5 |
Halted |
0 ms |
0 KB |
- |