#pragma GCC optimize "Ofast"
#pragma GCC optimize "unroll-loops"
#pragma GCC optimize "omit-frame-pointer"
#pragma GCC optimize "prefetch-loop-arrays"
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/priority_queue.hpp>
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using pi = pair<int, int>;
using pll = pair<ll, ll>;
constexpr int INF = 0x3f3f3f3f; constexpr ll LLINF = 0x3f3f3f3f3f3f3f3f;
#ifdef __MINGW32__ /* CF */
inline int getchar_unlocked() { return _getchar_nolock(); }
inline int putchar_unlocked(int c) { return _putchar_nolock(c); }
#endif
#define gc getchar_unlocked()
#define pc(x) putchar_unlocked(x)
template<typename T> void scan(T &x){x = 0;bool _=0; T c=gc;_=c==45;c=_?gc:c;while(c<48||c>57)c=gc;for(;c<48||c>57;c=gc);for(;c>47&&c<58;c=gc)x=(x<<3)+(x<<1)+(c&15);x=_?-x:x;}
template<typename T> void printn(T n){bool _=0;_=n<0;n=_?-n:n;char snum[65];int i=0;do{snum[i++]=n%10+48;n/= 10;}while(n);--i;if (_)pc(45);while(i>=0)pc(snum[i--]);}
template<typename First, typename ... Ints> void scan(First &arg, Ints&... rest){scan(arg);scan(rest...);}
template<typename T> void print(T n){printn(n);pc(10);}
template<typename First, typename ... Ints> void print(First arg, Ints... rest){printn(arg);pc(32);print(rest...);}
#define db(x) { cerr << #x << " = " << x << endl; }
template <typename T> void _dbarr(T* a, size_t sz){ for(int i = 0; i < sz; i++) cerr << a[i] << " \n"[i == sz-1]; }
template <typename T> void _dbarr(vector<T> a, size_t sz){ for(int i = 0; i < sz; i++) cerr << a[i] << " \n"[i == sz-1]; }
#define dbarr(x, n) {cerr << #x << ": "; _dbarr((x),(n));}
#define all(x) begin(x), end(x)
#define pb push_back
#define eb emplace_back
//#define mp make_pair
#define fs first
#define sn second
const int MM = 1e5+5;
int n,ind[MM],nxt[MM];
bool done[MM];
vector<int> adj[MM];
string back[MM];
map<string,int> mp;
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");
// even n is always possible
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(auto&[k,v]:mp) cout << k << " " << v << "\n";
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 |
4 ms |
5888 KB |
Output is correct |
2 |
Correct |
5 ms |
5888 KB |
Output is correct |
3 |
Correct |
4 ms |
5888 KB |
Output is correct |
4 |
Correct |
4 ms |
5888 KB |
Output is correct |
5 |
Correct |
4 ms |
5888 KB |
Output is correct |
6 |
Correct |
5 ms |
5760 KB |
Output is correct |
7 |
Correct |
4 ms |
5888 KB |
Output is correct |
8 |
Correct |
5 ms |
5888 KB |
Output is correct |
9 |
Correct |
5 ms |
5888 KB |
Output is correct |
10 |
Correct |
4 ms |
5888 KB |
Output is correct |
11 |
Correct |
5 ms |
5888 KB |
Output is correct |
12 |
Correct |
5 ms |
5888 KB |
Output is correct |
13 |
Correct |
4 ms |
5888 KB |
Output is correct |
14 |
Correct |
4 ms |
5888 KB |
Output is correct |
15 |
Correct |
4 ms |
5888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5760 KB |
Output is correct |
2 |
Correct |
4 ms |
5888 KB |
Output is correct |
3 |
Correct |
4 ms |
5888 KB |
Output is correct |
4 |
Correct |
241 ms |
16632 KB |
Output is correct |
5 |
Correct |
283 ms |
16860 KB |
Output is correct |
6 |
Correct |
262 ms |
16632 KB |
Output is correct |
7 |
Correct |
6 ms |
5888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
268 ms |
15324 KB |
Output is correct |
2 |
Correct |
292 ms |
15224 KB |
Output is correct |
3 |
Correct |
218 ms |
15212 KB |
Output is correct |
4 |
Correct |
5 ms |
5888 KB |
Output is correct |
5 |
Correct |
256 ms |
15100 KB |
Output is correct |
6 |
Correct |
249 ms |
15352 KB |
Output is correct |
7 |
Correct |
252 ms |
15368 KB |
Output is correct |
8 |
Correct |
210 ms |
15328 KB |
Output is correct |
9 |
Correct |
217 ms |
15608 KB |
Output is correct |
10 |
Correct |
179 ms |
15224 KB |
Output is correct |
11 |
Correct |
4 ms |
5888 KB |
Output is correct |
12 |
Correct |
4 ms |
5888 KB |
Output is correct |
13 |
Correct |
4 ms |
5760 KB |
Output is correct |
14 |
Correct |
4 ms |
5888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5888 KB |
Output is correct |
2 |
Correct |
5 ms |
5888 KB |
Output is correct |
3 |
Correct |
4 ms |
5888 KB |
Output is correct |
4 |
Correct |
4 ms |
5888 KB |
Output is correct |
5 |
Correct |
4 ms |
5888 KB |
Output is correct |
6 |
Correct |
5 ms |
5760 KB |
Output is correct |
7 |
Correct |
4 ms |
5888 KB |
Output is correct |
8 |
Correct |
5 ms |
5888 KB |
Output is correct |
9 |
Correct |
5 ms |
5888 KB |
Output is correct |
10 |
Correct |
4 ms |
5888 KB |
Output is correct |
11 |
Correct |
5 ms |
5888 KB |
Output is correct |
12 |
Correct |
5 ms |
5888 KB |
Output is correct |
13 |
Correct |
4 ms |
5888 KB |
Output is correct |
14 |
Correct |
4 ms |
5888 KB |
Output is correct |
15 |
Correct |
4 ms |
5888 KB |
Output is correct |
16 |
Correct |
4 ms |
5760 KB |
Output is correct |
17 |
Correct |
4 ms |
5888 KB |
Output is correct |
18 |
Correct |
4 ms |
5888 KB |
Output is correct |
19 |
Correct |
241 ms |
16632 KB |
Output is correct |
20 |
Correct |
283 ms |
16860 KB |
Output is correct |
21 |
Correct |
262 ms |
16632 KB |
Output is correct |
22 |
Correct |
6 ms |
5888 KB |
Output is correct |
23 |
Correct |
268 ms |
15324 KB |
Output is correct |
24 |
Correct |
292 ms |
15224 KB |
Output is correct |
25 |
Correct |
218 ms |
15212 KB |
Output is correct |
26 |
Correct |
5 ms |
5888 KB |
Output is correct |
27 |
Correct |
256 ms |
15100 KB |
Output is correct |
28 |
Correct |
249 ms |
15352 KB |
Output is correct |
29 |
Correct |
252 ms |
15368 KB |
Output is correct |
30 |
Correct |
210 ms |
15328 KB |
Output is correct |
31 |
Correct |
217 ms |
15608 KB |
Output is correct |
32 |
Correct |
179 ms |
15224 KB |
Output is correct |
33 |
Correct |
4 ms |
5888 KB |
Output is correct |
34 |
Correct |
4 ms |
5888 KB |
Output is correct |
35 |
Correct |
4 ms |
5760 KB |
Output is correct |
36 |
Correct |
4 ms |
5888 KB |
Output is correct |
37 |
Correct |
261 ms |
16712 KB |
Output is correct |
38 |
Correct |
252 ms |
16888 KB |
Output is correct |
39 |
Correct |
290 ms |
16888 KB |
Output is correct |
40 |
Correct |
248 ms |
16888 KB |
Output is correct |
41 |
Correct |
256 ms |
17020 KB |
Output is correct |
42 |
Correct |
263 ms |
16832 KB |
Output is correct |
43 |
Correct |
251 ms |
16888 KB |
Output is correct |
44 |
Correct |
259 ms |
17016 KB |
Output is correct |
45 |
Correct |
275 ms |
16888 KB |
Output is correct |
46 |
Correct |
289 ms |
17144 KB |
Output is correct |
47 |
Correct |
245 ms |
17148 KB |
Output is correct |
48 |
Correct |
255 ms |
16764 KB |
Output is correct |
49 |
Correct |
262 ms |
16760 KB |
Output is correct |
50 |
Correct |
205 ms |
16632 KB |
Output is correct |
51 |
Correct |
4 ms |
5888 KB |
Output is correct |
52 |
Correct |
250 ms |
16760 KB |
Output is correct |
53 |
Correct |
275 ms |
16888 KB |
Output is correct |
54 |
Correct |
284 ms |
16892 KB |
Output is correct |
55 |
Correct |
246 ms |
17016 KB |
Output is correct |
56 |
Correct |
235 ms |
17016 KB |
Output is correct |
57 |
Correct |
203 ms |
16800 KB |
Output is correct |
58 |
Correct |
4 ms |
5888 KB |
Output is correct |
59 |
Correct |
4 ms |
5888 KB |
Output is correct |
60 |
Correct |
4 ms |
5760 KB |
Output is correct |
61 |
Correct |
4 ms |
5888 KB |
Output is correct |
62 |
Correct |
4 ms |
5792 KB |
Output is correct |
63 |
Correct |
4 ms |
5760 KB |
Output is correct |
64 |
Correct |
4 ms |
5888 KB |
Output is correct |
65 |
Correct |
240 ms |
16760 KB |
Output is correct |
66 |
Correct |
240 ms |
16632 KB |
Output is correct |
67 |
Correct |
232 ms |
16632 KB |
Output is correct |
68 |
Correct |
4 ms |
5888 KB |
Output is correct |
69 |
Correct |
4 ms |
5888 KB |
Output is correct |
70 |
Correct |
4 ms |
5888 KB |
Output is correct |
71 |
Correct |
5 ms |
5888 KB |
Output is correct |
72 |
Correct |
4 ms |
5888 KB |
Output is correct |
73 |
Correct |
4 ms |
5760 KB |
Output is correct |
74 |
Correct |
5 ms |
5760 KB |
Output is correct |
75 |
Correct |
5 ms |
5888 KB |
Output is correct |
76 |
Correct |
4 ms |
5888 KB |
Output is correct |
77 |
Correct |
5 ms |
5888 KB |
Output is correct |
78 |
Correct |
4 ms |
5888 KB |
Output is correct |
79 |
Correct |
4 ms |
5888 KB |
Output is correct |
80 |
Correct |
4 ms |
5888 KB |
Output is correct |
81 |
Correct |
4 ms |
5760 KB |
Output is correct |
82 |
Correct |
5 ms |
5888 KB |
Output is correct |
83 |
Correct |
4 ms |
5888 KB |
Output is correct |