#include <bits/stdc++.h>
#define speed ios_base::sync_with_stdio(0); cin.tie(0)
#define all(x) (x).begin(),(x).end()
#define F first
#define S second
//#pragma GCC optimize("O3,unroll-loops")
//#pragma GCC target("avx,avx2")
//#pragma GCC target("popcnt")
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const ll MAX=2e5+10,P=1e9+7;
const ll INF=0x3f3f3f3f,oo=0x3f3f3f3f3f3f3f3f;
const ldb eps=1e-6;
const ldb PI=acos(-1.0);
const int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
template<typename T>
using vvector = vector<vector<T>>;
int main() {
speed;
int n;
vector<pair<string,string>> e;
vector<string> d;
cin>>n;
for (int i=0;i<n;i++) {
string a,b;
cin>>a>>b;
e.emplace_back(a,b);
d.push_back(a);
d.push_back(b);
}
if (n&1) {
cout<<"-1\n";
return 0;
}
sort(all(d));
d.resize(unique(all(d))-d.begin());
n=d.size();
vector<int> G(n+1);
vector<int> ind(n+1),vis(n+1);
int ans=0;
for (int i=0;i<n;i++) {
int a=lower_bound(all(d),e[i].F)-d.begin()+1,b=lower_bound(all(d),e[i].S)-d.begin()+1;
// if (a==b) G[a]=0;
// else {
G[a]=b;
ind[b]++;
// }
}
queue<int> q;
for (int i=1;i<=n;i++) {
if (ind[i]==0) q.push(i);
}
while (!q.empty()) {
int v=q.front();
q.pop();
// cout<<v<<" v\n"<<flush;
ans++;
if (!vis[G[v]]) {
// cout<<v<<" "<<G[v]<<" v gv\n";
vis[G[v]]=true;
ind[G[G[v]]]--;
if (ind[G[G[v]]]==0 and !vis[G[G[v]]]) q.push(G[G[v]]);
}
vis[v]=true;
}
for (int i=1;i<=n;i++) {
if (vis[i]) continue;
vector<int> ccl;
ccl.push_back(i);
int now=G[i];
while (now!=ccl[0]) {
// cout<<now<<" now\n"<<flush;
ccl.push_back(now);
now=G[now];
}
for (int i:ccl) vis[i]=true;
if (ccl.size()!=2) {
ans+=(ccl.size()/2)+(ccl.size()&1);
}
}
cout<<ans<<"\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
110 ms |
20876 KB |
Output is correct |
5 |
Correct |
106 ms |
20112 KB |
Output is correct |
6 |
Correct |
106 ms |
20880 KB |
Output is correct |
7 |
Correct |
18 ms |
19236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
105 ms |
20252 KB |
Output is correct |
2 |
Correct |
109 ms |
20180 KB |
Output is correct |
3 |
Correct |
100 ms |
20112 KB |
Output is correct |
4 |
Correct |
20 ms |
19100 KB |
Output is correct |
5 |
Correct |
109 ms |
20100 KB |
Output is correct |
6 |
Correct |
121 ms |
20256 KB |
Output is correct |
7 |
Correct |
106 ms |
20264 KB |
Output is correct |
8 |
Correct |
98 ms |
20196 KB |
Output is correct |
9 |
Correct |
97 ms |
20368 KB |
Output is correct |
10 |
Correct |
78 ms |
20376 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |