#include <bits/stdc++.h>
#include <math.h>
//in the name of god,aka allah
//**gray sety orz**
using namespace std;
#define pi pair<long long , long long>
#define pii pair<long long , pair<long long , long long>>
const int maxm = 5e5;
const long long mod = 1e9 + 7 ;
typedef long long ll;
ll l,r,mid;
ll n,m;
ll dis[maxm] , sum[maxm];
bool isval(int mid){
//cout << mid <<" " << mid*mid-mid <<endl;
if (((mid-1)*mid)/2 < m) return 0;
return 1;
}
ll darage[maxm] , ss , mm;
queue<int> q;
vector<pi> g[maxm] , z[maxm];
ll sath[maxm];
bool vis[maxm] , gos[maxm];
ll pedaret[maxm];
ll get_par(ll v){
if (pedaret[v]==v) return v;
return pedaret[v] = get_par(pedaret[v]);
}
void merge(ll r , ll q){
if (get_par(r)!=get_par(q))l+=max(darage[r],darage[q])*1ll*sath[r]*1ll*sath[q];
r = get_par(r) , q = get_par(q);
if (r!=q){
if (sath[r]<sath[q]) swap(r,q);
pedaret[q] = r;
sath[r] += sath[q];
}
return ;
}
ll pars1[maxm] , pars2[maxm];
vector<ll> se[maxm];
set<string> st;
ll rp[maxm];
pi w[maxm];
ll dp[maxm];
//ll rw[maxm][maxm];
map<string,ll> mp;
const int MM = 1e5+5;
int ind[MM],nxt[MM];
bool done[MM];
vector<int> adj[MM];
string back[MM];
int mx = 0;
int conv(string s){
if(mp.find(s) ==mp.end()) mp.insert({s,mp.size()}), back[mp.size()-1] = s; // 0-index
return mp[s];
}
int main()
{
cin.tie(0)->sync_with_stdio(0);
cin >> n;
if(n & 1) return 0*printf("-1\n");
for(int i = 0; i < n; i++){
string a,b;
cin >> a >> b;
int aa = conv(a), bb = conv(b);
nxt[aa] = bb;
ind[bb]++;
}
for(int i = 0; i < n; i++) if(nxt[nxt[i]] == i && nxt[i] != i) done[i] = 1, done[nxt[i]] = 1;
queue<int> q;
for(int i = 0; i < n; i++) if(!done[i] && ind[i] == 0) q.push(i);
int ans = 0;
while(!q.empty())
{
int u = q.front(); q.pop();
//db(back[u])
ans++; done[u] = 1;
if(!done[nxt[u]])
{
if(--ind[nxt[nxt[u]]] == 0 && !done[nxt[nxt[u]]]) q.push(nxt[nxt[u]]);
//db(back[nxt[u]])
done[nxt[u]] = 1;
}
}
//db(done[conv("d")])
for(int i = 0; i < n; i++)
{
if(!done[i])
{
int len = 1;
done[i] = 1;
for(int pos = nxt[i]; pos != i; done[pos] = 1, pos = nxt[pos]) len++;
//db(len)
ans += (len+1)/2;
}
}
cout << ans << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
40980 KB |
Output is correct |
2 |
Correct |
24 ms |
41044 KB |
Output is correct |
3 |
Correct |
20 ms |
41024 KB |
Output is correct |
4 |
Correct |
20 ms |
41056 KB |
Output is correct |
5 |
Correct |
20 ms |
40948 KB |
Output is correct |
6 |
Correct |
20 ms |
41064 KB |
Output is correct |
7 |
Correct |
21 ms |
41056 KB |
Output is correct |
8 |
Correct |
22 ms |
41044 KB |
Output is correct |
9 |
Correct |
20 ms |
41044 KB |
Output is correct |
10 |
Correct |
19 ms |
41020 KB |
Output is correct |
11 |
Correct |
21 ms |
41064 KB |
Output is correct |
12 |
Correct |
20 ms |
41044 KB |
Output is correct |
13 |
Correct |
23 ms |
41044 KB |
Output is correct |
14 |
Correct |
20 ms |
41060 KB |
Output is correct |
15 |
Correct |
20 ms |
41044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
41044 KB |
Output is correct |
2 |
Correct |
22 ms |
41020 KB |
Output is correct |
3 |
Correct |
20 ms |
41044 KB |
Output is correct |
4 |
Correct |
184 ms |
49720 KB |
Output is correct |
5 |
Correct |
187 ms |
49688 KB |
Output is correct |
6 |
Correct |
200 ms |
49740 KB |
Output is correct |
7 |
Correct |
21 ms |
41044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
194 ms |
49852 KB |
Output is correct |
2 |
Correct |
189 ms |
51972 KB |
Output is correct |
3 |
Correct |
168 ms |
51780 KB |
Output is correct |
4 |
Correct |
26 ms |
41084 KB |
Output is correct |
5 |
Correct |
201 ms |
51800 KB |
Output is correct |
6 |
Correct |
184 ms |
52028 KB |
Output is correct |
7 |
Correct |
205 ms |
52064 KB |
Output is correct |
8 |
Correct |
184 ms |
52040 KB |
Output is correct |
9 |
Correct |
189 ms |
52184 KB |
Output is correct |
10 |
Correct |
152 ms |
51872 KB |
Output is correct |
11 |
Correct |
22 ms |
41044 KB |
Output is correct |
12 |
Correct |
22 ms |
40980 KB |
Output is correct |
13 |
Correct |
20 ms |
41036 KB |
Output is correct |
14 |
Correct |
21 ms |
41044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
40980 KB |
Output is correct |
2 |
Correct |
24 ms |
41044 KB |
Output is correct |
3 |
Correct |
20 ms |
41024 KB |
Output is correct |
4 |
Correct |
20 ms |
41056 KB |
Output is correct |
5 |
Correct |
20 ms |
40948 KB |
Output is correct |
6 |
Correct |
20 ms |
41064 KB |
Output is correct |
7 |
Correct |
21 ms |
41056 KB |
Output is correct |
8 |
Correct |
22 ms |
41044 KB |
Output is correct |
9 |
Correct |
20 ms |
41044 KB |
Output is correct |
10 |
Correct |
19 ms |
41020 KB |
Output is correct |
11 |
Correct |
21 ms |
41064 KB |
Output is correct |
12 |
Correct |
20 ms |
41044 KB |
Output is correct |
13 |
Correct |
23 ms |
41044 KB |
Output is correct |
14 |
Correct |
20 ms |
41060 KB |
Output is correct |
15 |
Correct |
20 ms |
41044 KB |
Output is correct |
16 |
Correct |
20 ms |
41044 KB |
Output is correct |
17 |
Correct |
22 ms |
41020 KB |
Output is correct |
18 |
Correct |
20 ms |
41044 KB |
Output is correct |
19 |
Correct |
184 ms |
49720 KB |
Output is correct |
20 |
Correct |
187 ms |
49688 KB |
Output is correct |
21 |
Correct |
200 ms |
49740 KB |
Output is correct |
22 |
Correct |
21 ms |
41044 KB |
Output is correct |
23 |
Correct |
194 ms |
49852 KB |
Output is correct |
24 |
Correct |
189 ms |
51972 KB |
Output is correct |
25 |
Correct |
168 ms |
51780 KB |
Output is correct |
26 |
Correct |
26 ms |
41084 KB |
Output is correct |
27 |
Correct |
201 ms |
51800 KB |
Output is correct |
28 |
Correct |
184 ms |
52028 KB |
Output is correct |
29 |
Correct |
205 ms |
52064 KB |
Output is correct |
30 |
Correct |
184 ms |
52040 KB |
Output is correct |
31 |
Correct |
189 ms |
52184 KB |
Output is correct |
32 |
Correct |
152 ms |
51872 KB |
Output is correct |
33 |
Correct |
22 ms |
41044 KB |
Output is correct |
34 |
Correct |
22 ms |
40980 KB |
Output is correct |
35 |
Correct |
20 ms |
41036 KB |
Output is correct |
36 |
Correct |
21 ms |
41044 KB |
Output is correct |
37 |
Correct |
198 ms |
51948 KB |
Output is correct |
38 |
Correct |
185 ms |
52092 KB |
Output is correct |
39 |
Correct |
211 ms |
52084 KB |
Output is correct |
40 |
Correct |
185 ms |
52048 KB |
Output is correct |
41 |
Correct |
182 ms |
52048 KB |
Output is correct |
42 |
Correct |
212 ms |
51984 KB |
Output is correct |
43 |
Correct |
194 ms |
52024 KB |
Output is correct |
44 |
Correct |
200 ms |
51972 KB |
Output is correct |
45 |
Correct |
195 ms |
52020 KB |
Output is correct |
46 |
Correct |
219 ms |
52040 KB |
Output is correct |
47 |
Correct |
213 ms |
52132 KB |
Output is correct |
48 |
Correct |
201 ms |
52008 KB |
Output is correct |
49 |
Correct |
192 ms |
51876 KB |
Output is correct |
50 |
Correct |
178 ms |
51788 KB |
Output is correct |
51 |
Correct |
21 ms |
41044 KB |
Output is correct |
52 |
Correct |
216 ms |
52048 KB |
Output is correct |
53 |
Correct |
190 ms |
52008 KB |
Output is correct |
54 |
Correct |
212 ms |
52060 KB |
Output is correct |
55 |
Correct |
194 ms |
51952 KB |
Output is correct |
56 |
Correct |
173 ms |
52140 KB |
Output is correct |
57 |
Correct |
146 ms |
51836 KB |
Output is correct |
58 |
Correct |
21 ms |
41060 KB |
Output is correct |
59 |
Correct |
21 ms |
40996 KB |
Output is correct |
60 |
Correct |
21 ms |
41044 KB |
Output is correct |
61 |
Correct |
22 ms |
41044 KB |
Output is correct |
62 |
Correct |
25 ms |
41080 KB |
Output is correct |
63 |
Correct |
22 ms |
41044 KB |
Output is correct |
64 |
Correct |
21 ms |
40980 KB |
Output is correct |
65 |
Correct |
180 ms |
51796 KB |
Output is correct |
66 |
Correct |
186 ms |
51796 KB |
Output is correct |
67 |
Correct |
229 ms |
51848 KB |
Output is correct |
68 |
Correct |
21 ms |
41044 KB |
Output is correct |
69 |
Correct |
21 ms |
41044 KB |
Output is correct |
70 |
Correct |
23 ms |
41028 KB |
Output is correct |
71 |
Correct |
20 ms |
41044 KB |
Output is correct |
72 |
Correct |
20 ms |
41044 KB |
Output is correct |
73 |
Correct |
20 ms |
41020 KB |
Output is correct |
74 |
Correct |
21 ms |
41072 KB |
Output is correct |
75 |
Correct |
20 ms |
41044 KB |
Output is correct |
76 |
Correct |
20 ms |
41044 KB |
Output is correct |
77 |
Correct |
20 ms |
41044 KB |
Output is correct |
78 |
Correct |
25 ms |
41044 KB |
Output is correct |
79 |
Correct |
24 ms |
41064 KB |
Output is correct |
80 |
Correct |
24 ms |
41028 KB |
Output is correct |
81 |
Correct |
20 ms |
41032 KB |
Output is correct |
82 |
Correct |
21 ms |
41064 KB |
Output is correct |
83 |
Correct |
20 ms |
41044 KB |
Output is correct |