#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;
//vector<int>st;
//clear_all();
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;
}
//if(in[i]==0 && !vis[i]) q.push(i);
}
for(int i = 1; i <= n; i++) if(in[i]==0 && !vis[i]) q.push(i);
//cout << "cholce\n";
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;
}
}
//cout << ans << '\n';
//cout << "cholce\n";
for(int i = 1; i <= n; i++)
{
if(!vis[i])
{
int cnt = 1;
for(int cur = dir[i]; cur != i; cur = dir[cur]) cnt++;
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 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
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 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
414 ms |
9464 KB |
Output is correct |
2 |
Correct |
415 ms |
11512 KB |
Output is correct |
3 |
Correct |
345 ms |
11128 KB |
Output is correct |
4 |
Correct |
397 ms |
11128 KB |
Output is correct |
5 |
Correct |
408 ms |
11512 KB |
Output is correct |
6 |
Correct |
408 ms |
11640 KB |
Output is correct |
7 |
Correct |
394 ms |
11640 KB |
Output is correct |
8 |
Correct |
367 ms |
11696 KB |
Output is correct |
9 |
Correct |
374 ms |
11768 KB |
Output is correct |
10 |
Correct |
319 ms |
11512 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
288 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 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |