#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5+10;
int f[maxn];
int comp[maxn], head[maxn], nivel[maxn], cc;
int ini[maxn], fim[maxn];
int mark[maxn];
bool done[maxn];
void dfs(int u)
{
mark[u] = 1;
if (mark[f[u]] == 1)
{
++cc;
head[cc] = u, mark[u] = 2;
ini[cc] = f[u], fim[cc] = u;
return;
}
if (!mark[f[u]]) dfs(f[u]);
nivel[u] = nivel[f[u]]+1, comp[u] = comp[f[u]];
mark[u] = 2;
}
int main(void)
{
int n;
cin >> n;
map<string, int> mp;
int ind = 0;
for (int i = 1; i <= n; i++)
{
string s1, s2;
cin >> s1 >> s2;
if (mp.find(s1) == mp.end()) mp[s1] = ++ind;
if (mp.find(s2) == mp.end()) mp[s2] = ++ind;
f[mp[s1]] = mp[s2];
}
if (n&1)
{
cout << "-1\n";
return 0;
}
for (int i = 1; i <= n; i++)
if (!mark[i])
dfs(i);
int ans = n;
vector<int> v;
for (int i = 1; i <= n; i++)
if (nivel[i])
v.push_back(i);
sort(v.begin(), v.end(), [&] (int a, int b) {return nivel[a] > nivel[b];});
for (int i = 1; i <= cc; i++)
{
for (int u = ini[cc]; u <= fim[cc]; u++)
{
if (done[u] || f[u] == u) continue;
if (!done[f[u]])
{
ans -= (f[f[u]] == u ? 2 : 1);
done[u] = done[f[u]] = 1;
}
}
}
for (auto u: v)
{
if (done[u]) continue;
if (!done[f[u]])
{
ans--;
done[u] = done[f[u]] = 1;
}
}
cout << ans << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
320 ms |
12132 KB |
Output is correct |
2 |
Correct |
326 ms |
12572 KB |
Output is correct |
3 |
Correct |
264 ms |
11756 KB |
Output is correct |
4 |
Correct |
304 ms |
10372 KB |
Output is correct |
5 |
Correct |
313 ms |
13800 KB |
Output is correct |
6 |
Correct |
307 ms |
12264 KB |
Output is correct |
7 |
Correct |
303 ms |
12136 KB |
Output is correct |
8 |
Correct |
287 ms |
12396 KB |
Output is correct |
9 |
Correct |
293 ms |
12264 KB |
Output is correct |
10 |
Correct |
246 ms |
12020 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |