#include <bits/stdc++.h>
using namespace std;
const int mxn = 2e5+5;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
typedef pair<int,int>pii;
typedef pair<int,pii>piii;
#define sf scanf
#define pf printf
#define input freopen("in.txt","r",stdin)
#define output freopen("out.txt","w",stdout)
#define inf 1e18
#define ff first
#define ss second
#define MP make_pair
#define pb push_back
#define all(v) v.begin(), v.end()
#define printcase(cases) printf("Case %d:", cases);
#define Unique(a) a.erase(unique(a.begin(),a.end()),a.end())
#define FAST ios_base::sync_with_stdio(0);cout.tie(0)
#define endl printf("\n")
#define __lcm(a, b) ((a*b)/__gcd(a, b))
map<string,int>mark;
int vis[mxn];
int in[mxn];
int dir[mxn];
int main()
{
//input;
//output;
int n;
cin >> n;
int pos = 1;
for(int i = 0; i < n; i++)
{
string a, b;
cin >> a >> b;
if(!mark[a]) mark[a] = pos++;
if(!mark[b]) mark[b] = pos++;
int u = mark[a];
int v = mark[b];
in[v]++;
dir[u] = v;
}
if(n&1)
{
cout << "-1\n";
return 0;
}
int ans = 0;
queue<int>q;
for(int i = 1; i <= n; i++)
{
if(dir[i] != i && i==dir[dir[i]])
{
vis[i] = 1;
vis[dir[i]] = 1;
}
}
for(int i = 1; i <= n; i++) if(in[i]==0 && !vis[i]) q.push(i);
while(!q.empty())
{
int u = q.front();
q.pop();
ans++;
vis[u] = 1;
if(!vis[dir[u]])
{
int v = dir[dir[u]];
in[v]--;
if(!vis[v] && in[v]==0) q.push(v);
vis[dir[u]] = 1;
}
}
for(int i = 1; i <= n; i++)
{
if(!vis[i])
{
int cnt = 1;
vis[i] = 1;
for(int cur = dir[i]; cur != i; cur = dir[cur]) cnt++, vis[cur] = 1;
ans+=(cnt+1)/2;
}
}
cout << ans << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
407 ms |
9404 KB |
Output is correct |
5 |
Correct |
420 ms |
9464 KB |
Output is correct |
6 |
Correct |
410 ms |
9464 KB |
Output is correct |
7 |
Correct |
411 ms |
9080 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
410 ms |
9592 KB |
Output is correct |
2 |
Correct |
419 ms |
9464 KB |
Output is correct |
3 |
Correct |
347 ms |
9336 KB |
Output is correct |
4 |
Correct |
404 ms |
8952 KB |
Output is correct |
5 |
Correct |
435 ms |
9468 KB |
Output is correct |
6 |
Correct |
415 ms |
9464 KB |
Output is correct |
7 |
Correct |
408 ms |
9592 KB |
Output is correct |
8 |
Correct |
369 ms |
9508 KB |
Output is correct |
9 |
Correct |
387 ms |
9596 KB |
Output is correct |
10 |
Correct |
335 ms |
9352 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
407 ms |
9404 KB |
Output is correct |
20 |
Correct |
420 ms |
9464 KB |
Output is correct |
21 |
Correct |
410 ms |
9464 KB |
Output is correct |
22 |
Correct |
411 ms |
9080 KB |
Output is correct |
23 |
Correct |
410 ms |
9592 KB |
Output is correct |
24 |
Correct |
419 ms |
9464 KB |
Output is correct |
25 |
Correct |
347 ms |
9336 KB |
Output is correct |
26 |
Correct |
404 ms |
8952 KB |
Output is correct |
27 |
Correct |
435 ms |
9468 KB |
Output is correct |
28 |
Correct |
415 ms |
9464 KB |
Output is correct |
29 |
Correct |
408 ms |
9592 KB |
Output is correct |
30 |
Correct |
369 ms |
9508 KB |
Output is correct |
31 |
Correct |
387 ms |
9596 KB |
Output is correct |
32 |
Correct |
335 ms |
9352 KB |
Output is correct |
33 |
Correct |
1 ms |
384 KB |
Output is correct |
34 |
Correct |
1 ms |
384 KB |
Output is correct |
35 |
Correct |
1 ms |
384 KB |
Output is correct |
36 |
Correct |
1 ms |
384 KB |
Output is correct |
37 |
Correct |
456 ms |
11704 KB |
Output is correct |
38 |
Correct |
402 ms |
11768 KB |
Output is correct |
39 |
Correct |
410 ms |
11864 KB |
Output is correct |
40 |
Correct |
415 ms |
11640 KB |
Output is correct |
41 |
Correct |
400 ms |
11768 KB |
Output is correct |
42 |
Correct |
398 ms |
11644 KB |
Output is correct |
43 |
Correct |
403 ms |
11876 KB |
Output is correct |
44 |
Correct |
410 ms |
11768 KB |
Output is correct |
45 |
Correct |
400 ms |
11896 KB |
Output is correct |
46 |
Correct |
405 ms |
11872 KB |
Output is correct |
47 |
Correct |
385 ms |
11768 KB |
Output is correct |
48 |
Correct |
407 ms |
11768 KB |
Output is correct |
49 |
Correct |
412 ms |
11640 KB |
Output is correct |
50 |
Correct |
346 ms |
11512 KB |
Output is correct |
51 |
Correct |
392 ms |
11256 KB |
Output is correct |
52 |
Correct |
411 ms |
11568 KB |
Output is correct |
53 |
Correct |
401 ms |
11768 KB |
Output is correct |
54 |
Correct |
390 ms |
11640 KB |
Output is correct |
55 |
Correct |
373 ms |
11768 KB |
Output is correct |
56 |
Correct |
381 ms |
11768 KB |
Output is correct |
57 |
Correct |
326 ms |
11640 KB |
Output is correct |
58 |
Correct |
1 ms |
384 KB |
Output is correct |
59 |
Correct |
1 ms |
384 KB |
Output is correct |
60 |
Correct |
1 ms |
384 KB |
Output is correct |
61 |
Correct |
1 ms |
384 KB |
Output is correct |
62 |
Correct |
1 ms |
288 KB |
Output is correct |
63 |
Correct |
1 ms |
384 KB |
Output is correct |
64 |
Correct |
1 ms |
384 KB |
Output is correct |
65 |
Correct |
413 ms |
11640 KB |
Output is correct |
66 |
Correct |
399 ms |
11512 KB |
Output is correct |
67 |
Correct |
402 ms |
11512 KB |
Output is correct |
68 |
Correct |
405 ms |
11128 KB |
Output is correct |
69 |
Correct |
1 ms |
384 KB |
Output is correct |
70 |
Correct |
1 ms |
384 KB |
Output is correct |
71 |
Correct |
1 ms |
384 KB |
Output is correct |
72 |
Correct |
1 ms |
384 KB |
Output is correct |
73 |
Correct |
1 ms |
384 KB |
Output is correct |
74 |
Correct |
1 ms |
384 KB |
Output is correct |
75 |
Correct |
1 ms |
384 KB |
Output is correct |
76 |
Correct |
1 ms |
384 KB |
Output is correct |
77 |
Correct |
1 ms |
384 KB |
Output is correct |
78 |
Correct |
1 ms |
384 KB |
Output is correct |
79 |
Correct |
1 ms |
384 KB |
Output is correct |
80 |
Correct |
1 ms |
384 KB |
Output is correct |
81 |
Correct |
1 ms |
384 KB |
Output is correct |
82 |
Correct |
1 ms |
384 KB |
Output is correct |
83 |
Correct |
1 ms |
384 KB |
Output is correct |