#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 >> n;
if(n%2) return cout<<-1,0;
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 |
19 ms |
41044 KB |
Output is correct |
2 |
Correct |
20 ms |
41044 KB |
Output is correct |
3 |
Correct |
19 ms |
40976 KB |
Output is correct |
4 |
Correct |
20 ms |
41044 KB |
Output is correct |
5 |
Correct |
19 ms |
41020 KB |
Output is correct |
6 |
Correct |
21 ms |
41044 KB |
Output is correct |
7 |
Correct |
22 ms |
41000 KB |
Output is correct |
8 |
Correct |
21 ms |
41036 KB |
Output is correct |
9 |
Correct |
20 ms |
40956 KB |
Output is correct |
10 |
Correct |
20 ms |
41044 KB |
Output is correct |
11 |
Correct |
20 ms |
41044 KB |
Output is correct |
12 |
Correct |
20 ms |
41044 KB |
Output is correct |
13 |
Correct |
20 ms |
40908 KB |
Output is correct |
14 |
Correct |
19 ms |
40956 KB |
Output is correct |
15 |
Correct |
19 ms |
41048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
41052 KB |
Output is correct |
2 |
Correct |
19 ms |
41044 KB |
Output is correct |
3 |
Correct |
19 ms |
41044 KB |
Output is correct |
4 |
Correct |
239 ms |
49812 KB |
Output is correct |
5 |
Correct |
229 ms |
49740 KB |
Output is correct |
6 |
Correct |
252 ms |
49700 KB |
Output is correct |
7 |
Correct |
20 ms |
40956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
231 ms |
49836 KB |
Output is correct |
2 |
Correct |
229 ms |
49788 KB |
Output is correct |
3 |
Correct |
206 ms |
49632 KB |
Output is correct |
4 |
Correct |
21 ms |
41044 KB |
Output is correct |
5 |
Correct |
227 ms |
49624 KB |
Output is correct |
6 |
Correct |
228 ms |
49868 KB |
Output is correct |
7 |
Correct |
239 ms |
49992 KB |
Output is correct |
8 |
Correct |
218 ms |
49868 KB |
Output is correct |
9 |
Correct |
215 ms |
49968 KB |
Output is correct |
10 |
Correct |
197 ms |
49740 KB |
Output is correct |
11 |
Correct |
20 ms |
41044 KB |
Output is correct |
12 |
Correct |
20 ms |
40924 KB |
Output is correct |
13 |
Correct |
21 ms |
41044 KB |
Output is correct |
14 |
Correct |
20 ms |
40928 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
41044 KB |
Output is correct |
2 |
Correct |
20 ms |
41044 KB |
Output is correct |
3 |
Correct |
19 ms |
40976 KB |
Output is correct |
4 |
Correct |
20 ms |
41044 KB |
Output is correct |
5 |
Correct |
19 ms |
41020 KB |
Output is correct |
6 |
Correct |
21 ms |
41044 KB |
Output is correct |
7 |
Correct |
22 ms |
41000 KB |
Output is correct |
8 |
Correct |
21 ms |
41036 KB |
Output is correct |
9 |
Correct |
20 ms |
40956 KB |
Output is correct |
10 |
Correct |
20 ms |
41044 KB |
Output is correct |
11 |
Correct |
20 ms |
41044 KB |
Output is correct |
12 |
Correct |
20 ms |
41044 KB |
Output is correct |
13 |
Correct |
20 ms |
40908 KB |
Output is correct |
14 |
Correct |
19 ms |
40956 KB |
Output is correct |
15 |
Correct |
19 ms |
41048 KB |
Output is correct |
16 |
Correct |
19 ms |
41052 KB |
Output is correct |
17 |
Correct |
19 ms |
41044 KB |
Output is correct |
18 |
Correct |
19 ms |
41044 KB |
Output is correct |
19 |
Correct |
239 ms |
49812 KB |
Output is correct |
20 |
Correct |
229 ms |
49740 KB |
Output is correct |
21 |
Correct |
252 ms |
49700 KB |
Output is correct |
22 |
Correct |
20 ms |
40956 KB |
Output is correct |
23 |
Correct |
231 ms |
49836 KB |
Output is correct |
24 |
Correct |
229 ms |
49788 KB |
Output is correct |
25 |
Correct |
206 ms |
49632 KB |
Output is correct |
26 |
Correct |
21 ms |
41044 KB |
Output is correct |
27 |
Correct |
227 ms |
49624 KB |
Output is correct |
28 |
Correct |
228 ms |
49868 KB |
Output is correct |
29 |
Correct |
239 ms |
49992 KB |
Output is correct |
30 |
Correct |
218 ms |
49868 KB |
Output is correct |
31 |
Correct |
215 ms |
49968 KB |
Output is correct |
32 |
Correct |
197 ms |
49740 KB |
Output is correct |
33 |
Correct |
20 ms |
41044 KB |
Output is correct |
34 |
Correct |
20 ms |
40924 KB |
Output is correct |
35 |
Correct |
21 ms |
41044 KB |
Output is correct |
36 |
Correct |
20 ms |
40928 KB |
Output is correct |
37 |
Correct |
231 ms |
49752 KB |
Output is correct |
38 |
Correct |
231 ms |
49908 KB |
Output is correct |
39 |
Correct |
226 ms |
49868 KB |
Output is correct |
40 |
Correct |
221 ms |
49928 KB |
Output is correct |
41 |
Correct |
233 ms |
49868 KB |
Output is correct |
42 |
Correct |
240 ms |
49876 KB |
Output is correct |
43 |
Correct |
228 ms |
49824 KB |
Output is correct |
44 |
Correct |
231 ms |
49768 KB |
Output is correct |
45 |
Correct |
224 ms |
49868 KB |
Output is correct |
46 |
Correct |
226 ms |
49864 KB |
Output is correct |
47 |
Correct |
229 ms |
50004 KB |
Output is correct |
48 |
Correct |
232 ms |
49800 KB |
Output is correct |
49 |
Correct |
224 ms |
49740 KB |
Output is correct |
50 |
Correct |
203 ms |
49712 KB |
Output is correct |
51 |
Correct |
21 ms |
41040 KB |
Output is correct |
52 |
Correct |
232 ms |
49708 KB |
Output is correct |
53 |
Correct |
220 ms |
49868 KB |
Output is correct |
54 |
Correct |
235 ms |
49872 KB |
Output is correct |
55 |
Correct |
238 ms |
49904 KB |
Output is correct |
56 |
Correct |
217 ms |
49996 KB |
Output is correct |
57 |
Correct |
191 ms |
49684 KB |
Output is correct |
58 |
Correct |
20 ms |
41044 KB |
Output is correct |
59 |
Correct |
20 ms |
40952 KB |
Output is correct |
60 |
Correct |
20 ms |
41044 KB |
Output is correct |
61 |
Correct |
20 ms |
41044 KB |
Output is correct |
62 |
Correct |
20 ms |
40968 KB |
Output is correct |
63 |
Correct |
20 ms |
41044 KB |
Output is correct |
64 |
Correct |
20 ms |
41044 KB |
Output is correct |
65 |
Correct |
227 ms |
49624 KB |
Output is correct |
66 |
Correct |
255 ms |
49760 KB |
Output is correct |
67 |
Correct |
239 ms |
49740 KB |
Output is correct |
68 |
Correct |
20 ms |
41044 KB |
Output is correct |
69 |
Correct |
20 ms |
40996 KB |
Output is correct |
70 |
Correct |
20 ms |
41056 KB |
Output is correct |
71 |
Correct |
21 ms |
41044 KB |
Output is correct |
72 |
Correct |
20 ms |
41032 KB |
Output is correct |
73 |
Correct |
20 ms |
41032 KB |
Output is correct |
74 |
Correct |
21 ms |
40972 KB |
Output is correct |
75 |
Correct |
20 ms |
40964 KB |
Output is correct |
76 |
Correct |
20 ms |
40984 KB |
Output is correct |
77 |
Correct |
21 ms |
41032 KB |
Output is correct |
78 |
Correct |
20 ms |
41056 KB |
Output is correct |
79 |
Correct |
19 ms |
41044 KB |
Output is correct |
80 |
Correct |
19 ms |
41024 KB |
Output is correct |
81 |
Correct |
20 ms |
40972 KB |
Output is correct |
82 |
Correct |
20 ms |
41024 KB |
Output is correct |
83 |
Correct |
20 ms |
41044 KB |
Output is correct |