#include <bits/stdc++.h>
using namespace std;
#define ll uint_fast64_t
const ll base = 1e6;
const int MAX = 100001;
int n,edges[MAX],value;
string s,t;
map <string,int> m;
map <ll,bool> if_match;
bool if_deleted[MAX],if_paired[MAX];
string graph[MAX];
queue <int> q;
int dfs(int a,int c,int length)
{
if_deleted[a] = true;
if (c == m[graph[a]])return length + 1;
if(if_deleted[m[graph[a]]]||if_paired[m[graph[a]]])return length + 1;
return dfs(m[graph[a]], c, length + 1);
return length + 1;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin>>n;
if(n%2)
{
printf("-1");
return 0;
}
for(int i = 1;i<=n;++i)
{
cin>>s>>t;
m[s] = i;
graph[m[s]] = t;
}
for(int i = 1;i<=n;++i) {
if(if_match[ll(m[graph[i]]) * base + ll(i)])
{
if_deleted[i] = true;
if_deleted[m[graph[i]]] = true;
continue;
}
if_match[ll(i) * base + ll(m[graph[i]])] = true;
}
for(int i = 1;i<=n;++i)
{
if(if_deleted[i])continue;
if(!if_deleted[m[graph[i]]])edges[m[graph[i]]]++;
}
for(int i = 1;i<=n;++i)if(!edges[i]&&!if_deleted[i])q.push(i);
while(!q.empty())
{
int p = q.front();
q.pop();
if(if_paired[p]||if_deleted[p])continue;
if(!if_paired[m[graph[p]]])
{
if_paired[p] = true;
if_paired[m[graph[p]]] = true;
++value;
--edges[m[graph[m[graph[p]]]]];
if(!edges[m[graph[m[graph[p]]]]]) q.push(m[graph[m[graph[p]]]]);
}
}
//cout<<value<<endl;
for(int i = 1 ;i<=n;++i)
{
if(!if_deleted[i]&&!if_paired[i])
{
int h = dfs(i,i,0);
// cout<<h<<endl;
if(h%2)value+= (h>>1) +1;
else value += (h>>1);
}
}
cout<<value<<endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3412 KB |
Output is correct |
2 |
Correct |
2 ms |
3412 KB |
Output is correct |
3 |
Correct |
2 ms |
3412 KB |
Output is correct |
4 |
Correct |
2 ms |
3412 KB |
Output is correct |
5 |
Correct |
2 ms |
3412 KB |
Output is correct |
6 |
Correct |
2 ms |
3412 KB |
Output is correct |
7 |
Correct |
2 ms |
3412 KB |
Output is correct |
8 |
Correct |
2 ms |
3412 KB |
Output is correct |
9 |
Correct |
2 ms |
3412 KB |
Output is correct |
10 |
Correct |
2 ms |
3412 KB |
Output is correct |
11 |
Correct |
2 ms |
3456 KB |
Output is correct |
12 |
Correct |
2 ms |
3412 KB |
Output is correct |
13 |
Correct |
2 ms |
3464 KB |
Output is correct |
14 |
Correct |
2 ms |
3412 KB |
Output is correct |
15 |
Correct |
2 ms |
3456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
3412 KB |
Output is correct |
2 |
Correct |
2 ms |
3412 KB |
Output is correct |
3 |
Correct |
2 ms |
3456 KB |
Output is correct |
4 |
Correct |
432 ms |
26380 KB |
Output is correct |
5 |
Correct |
557 ms |
26128 KB |
Output is correct |
6 |
Correct |
449 ms |
26428 KB |
Output is correct |
7 |
Correct |
2 ms |
3412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
540 ms |
26544 KB |
Output is correct |
2 |
Correct |
420 ms |
26584 KB |
Output is correct |
3 |
Correct |
333 ms |
20152 KB |
Output is correct |
4 |
Correct |
3 ms |
3412 KB |
Output is correct |
5 |
Correct |
470 ms |
26344 KB |
Output is correct |
6 |
Correct |
441 ms |
26640 KB |
Output is correct |
7 |
Correct |
465 ms |
26584 KB |
Output is correct |
8 |
Correct |
399 ms |
23480 KB |
Output is correct |
9 |
Correct |
540 ms |
26844 KB |
Output is correct |
10 |
Correct |
259 ms |
26420 KB |
Output is correct |
11 |
Correct |
2 ms |
3412 KB |
Output is correct |
12 |
Correct |
2 ms |
3412 KB |
Output is correct |
13 |
Correct |
2 ms |
3412 KB |
Output is correct |
14 |
Correct |
3 ms |
3460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3412 KB |
Output is correct |
2 |
Correct |
2 ms |
3412 KB |
Output is correct |
3 |
Correct |
2 ms |
3412 KB |
Output is correct |
4 |
Correct |
2 ms |
3412 KB |
Output is correct |
5 |
Correct |
2 ms |
3412 KB |
Output is correct |
6 |
Correct |
2 ms |
3412 KB |
Output is correct |
7 |
Correct |
2 ms |
3412 KB |
Output is correct |
8 |
Correct |
2 ms |
3412 KB |
Output is correct |
9 |
Correct |
2 ms |
3412 KB |
Output is correct |
10 |
Correct |
2 ms |
3412 KB |
Output is correct |
11 |
Correct |
2 ms |
3456 KB |
Output is correct |
12 |
Correct |
2 ms |
3412 KB |
Output is correct |
13 |
Correct |
2 ms |
3464 KB |
Output is correct |
14 |
Correct |
2 ms |
3412 KB |
Output is correct |
15 |
Correct |
2 ms |
3456 KB |
Output is correct |
16 |
Correct |
3 ms |
3412 KB |
Output is correct |
17 |
Correct |
2 ms |
3412 KB |
Output is correct |
18 |
Correct |
2 ms |
3456 KB |
Output is correct |
19 |
Correct |
432 ms |
26380 KB |
Output is correct |
20 |
Correct |
557 ms |
26128 KB |
Output is correct |
21 |
Correct |
449 ms |
26428 KB |
Output is correct |
22 |
Correct |
2 ms |
3412 KB |
Output is correct |
23 |
Correct |
540 ms |
26544 KB |
Output is correct |
24 |
Correct |
420 ms |
26584 KB |
Output is correct |
25 |
Correct |
333 ms |
20152 KB |
Output is correct |
26 |
Correct |
3 ms |
3412 KB |
Output is correct |
27 |
Correct |
470 ms |
26344 KB |
Output is correct |
28 |
Correct |
441 ms |
26640 KB |
Output is correct |
29 |
Correct |
465 ms |
26584 KB |
Output is correct |
30 |
Correct |
399 ms |
23480 KB |
Output is correct |
31 |
Correct |
540 ms |
26844 KB |
Output is correct |
32 |
Correct |
259 ms |
26420 KB |
Output is correct |
33 |
Correct |
2 ms |
3412 KB |
Output is correct |
34 |
Correct |
2 ms |
3412 KB |
Output is correct |
35 |
Correct |
2 ms |
3412 KB |
Output is correct |
36 |
Correct |
3 ms |
3460 KB |
Output is correct |
37 |
Correct |
506 ms |
26508 KB |
Output is correct |
38 |
Correct |
409 ms |
26608 KB |
Output is correct |
39 |
Correct |
408 ms |
26632 KB |
Output is correct |
40 |
Correct |
454 ms |
26648 KB |
Output is correct |
41 |
Correct |
487 ms |
26720 KB |
Output is correct |
42 |
Correct |
448 ms |
26604 KB |
Output is correct |
43 |
Correct |
502 ms |
26624 KB |
Output is correct |
44 |
Correct |
420 ms |
26588 KB |
Output is correct |
45 |
Correct |
473 ms |
26628 KB |
Output is correct |
46 |
Correct |
423 ms |
26568 KB |
Output is correct |
47 |
Correct |
442 ms |
26760 KB |
Output is correct |
48 |
Correct |
420 ms |
26612 KB |
Output is correct |
49 |
Correct |
548 ms |
26500 KB |
Output is correct |
50 |
Correct |
271 ms |
20300 KB |
Output is correct |
51 |
Correct |
2 ms |
3468 KB |
Output is correct |
52 |
Correct |
460 ms |
26432 KB |
Output is correct |
53 |
Correct |
411 ms |
26728 KB |
Output is correct |
54 |
Correct |
411 ms |
26628 KB |
Output is correct |
55 |
Correct |
343 ms |
23548 KB |
Output is correct |
56 |
Correct |
418 ms |
26792 KB |
Output is correct |
57 |
Correct |
245 ms |
26572 KB |
Output is correct |
58 |
Correct |
2 ms |
3456 KB |
Output is correct |
59 |
Correct |
2 ms |
3460 KB |
Output is correct |
60 |
Correct |
2 ms |
3460 KB |
Output is correct |
61 |
Correct |
2 ms |
3412 KB |
Output is correct |
62 |
Correct |
2 ms |
3412 KB |
Output is correct |
63 |
Correct |
2 ms |
3456 KB |
Output is correct |
64 |
Correct |
2 ms |
3464 KB |
Output is correct |
65 |
Correct |
402 ms |
26432 KB |
Output is correct |
66 |
Correct |
424 ms |
26016 KB |
Output is correct |
67 |
Correct |
413 ms |
26336 KB |
Output is correct |
68 |
Correct |
2 ms |
3412 KB |
Output is correct |
69 |
Correct |
2 ms |
3412 KB |
Output is correct |
70 |
Correct |
2 ms |
3412 KB |
Output is correct |
71 |
Correct |
2 ms |
3412 KB |
Output is correct |
72 |
Correct |
2 ms |
3412 KB |
Output is correct |
73 |
Correct |
2 ms |
3412 KB |
Output is correct |
74 |
Correct |
2 ms |
3412 KB |
Output is correct |
75 |
Correct |
2 ms |
3412 KB |
Output is correct |
76 |
Correct |
2 ms |
3456 KB |
Output is correct |
77 |
Correct |
2 ms |
3384 KB |
Output is correct |
78 |
Correct |
2 ms |
3412 KB |
Output is correct |
79 |
Correct |
2 ms |
3368 KB |
Output is correct |
80 |
Correct |
2 ms |
3412 KB |
Output is correct |
81 |
Correct |
2 ms |
3412 KB |
Output is correct |
82 |
Correct |
2 ms |
3412 KB |
Output is correct |
83 |
Correct |
2 ms |
3412 KB |
Output is correct |