#include <bits/stdc++.h>
#define pb push_back
#define F first
#define S second
#define debug(x) cout << #x << "= " << x << ", "
#define ll long long
#define fast ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define SZ(x) (int) x.size()
#define wall cout << endl;
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int maxn = 1e5 + 10;
int n , ar[maxn] , dp[maxn][2] , super_man;
map <string , int> mp;
vector <pair<string , string>> edges;
vector <int> adj[maxn] , trav;
bool vis[maxn] , marked[maxn];
int Find_root(int v)
{
vis[v] = true;
if(vis[ar[v]]) return v;
return Find_root(ar[v]);
}
void dfs(int v)
{
marked[v] = true;
bool flg = false;
for(auto u : adj[v]) if(!marked[u])
{
dfs(u);
dp[v][0] += dp[u][1];
dp[v][1] += dp[u][1];
if(dp[u][1] == dp[u][0]) flg = true;
}
dp[v][1] += flg;
}
void dfs2(int v , int root)
{
bool flg = false;
dp[v][0] = dp[v][1] = 0;
for(auto u : adj[v]) if(u != root)
{
dfs2(u , root);
dp[v][0] += dp[u][1];
dp[v][1] += dp[u][1];
if(dp[u][1] == dp[u][0] && u != super_man) flg = true;
}
dp[v][1] += flg;
if(v == super_man) dp[v][1] = dp[v][0];
}
int solve(int root)
{
dfs(root);
int res = dp[root][1];
if(ar[root] == root) return res;
super_man = ar[root];
dfs2(root , root);
int tmp = (ar[ar[root]] == root ? 2 : 1);
res = max(res , dp[root][0] + tmp);
return res;
}
int32_t main()
{
fast;
cin >> n;
for(int i = 0 ; i < n ; i++)
{
string a , b; cin >> a >> b;
edges.pb({a , b});
mp[a] = i;
}
if(n & 1)
{
cout << -1 << endl;
return 0;
}
for(auto u : edges)
{
int s = mp[u.F] , e = mp[u.S];
ar[s] = e;
adj[e].pb(s);
}
int ans = n;
for(int i = 0 ; i < n ; i++) if(!marked[i])
{
int r = Find_root(i);
ans -= solve(r);
}
cout << ans << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
1 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2644 KB |
Output is correct |
8 |
Correct |
2 ms |
2700 KB |
Output is correct |
9 |
Correct |
2 ms |
2772 KB |
Output is correct |
10 |
Correct |
2 ms |
2684 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Correct |
2 ms |
2644 KB |
Output is correct |
13 |
Correct |
2 ms |
2644 KB |
Output is correct |
14 |
Correct |
2 ms |
2644 KB |
Output is correct |
15 |
Correct |
3 ms |
2644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
213 ms |
27980 KB |
Output is correct |
5 |
Correct |
219 ms |
23324 KB |
Output is correct |
6 |
Correct |
258 ms |
31164 KB |
Output is correct |
7 |
Correct |
84 ms |
18896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
219 ms |
20516 KB |
Output is correct |
2 |
Correct |
229 ms |
22092 KB |
Output is correct |
3 |
Correct |
150 ms |
21192 KB |
Output is correct |
4 |
Correct |
79 ms |
16676 KB |
Output is correct |
5 |
Correct |
205 ms |
27016 KB |
Output is correct |
6 |
Correct |
181 ms |
19592 KB |
Output is correct |
7 |
Correct |
197 ms |
19856 KB |
Output is correct |
8 |
Correct |
181 ms |
20192 KB |
Output is correct |
9 |
Correct |
173 ms |
19116 KB |
Output is correct |
10 |
Correct |
135 ms |
18980 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Correct |
2 ms |
2644 KB |
Output is correct |
13 |
Correct |
1 ms |
2644 KB |
Output is correct |
14 |
Correct |
1 ms |
2644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
1 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2644 KB |
Output is correct |
8 |
Correct |
2 ms |
2700 KB |
Output is correct |
9 |
Correct |
2 ms |
2772 KB |
Output is correct |
10 |
Correct |
2 ms |
2684 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Correct |
2 ms |
2644 KB |
Output is correct |
13 |
Correct |
2 ms |
2644 KB |
Output is correct |
14 |
Correct |
2 ms |
2644 KB |
Output is correct |
15 |
Correct |
3 ms |
2644 KB |
Output is correct |
16 |
Correct |
2 ms |
2644 KB |
Output is correct |
17 |
Correct |
2 ms |
2644 KB |
Output is correct |
18 |
Correct |
2 ms |
2644 KB |
Output is correct |
19 |
Correct |
213 ms |
27980 KB |
Output is correct |
20 |
Correct |
219 ms |
23324 KB |
Output is correct |
21 |
Correct |
258 ms |
31164 KB |
Output is correct |
22 |
Correct |
84 ms |
18896 KB |
Output is correct |
23 |
Correct |
219 ms |
20516 KB |
Output is correct |
24 |
Correct |
229 ms |
22092 KB |
Output is correct |
25 |
Correct |
150 ms |
21192 KB |
Output is correct |
26 |
Correct |
79 ms |
16676 KB |
Output is correct |
27 |
Correct |
205 ms |
27016 KB |
Output is correct |
28 |
Correct |
181 ms |
19592 KB |
Output is correct |
29 |
Correct |
197 ms |
19856 KB |
Output is correct |
30 |
Correct |
181 ms |
20192 KB |
Output is correct |
31 |
Correct |
173 ms |
19116 KB |
Output is correct |
32 |
Correct |
135 ms |
18980 KB |
Output is correct |
33 |
Correct |
2 ms |
2644 KB |
Output is correct |
34 |
Correct |
2 ms |
2644 KB |
Output is correct |
35 |
Correct |
1 ms |
2644 KB |
Output is correct |
36 |
Correct |
1 ms |
2644 KB |
Output is correct |
37 |
Correct |
210 ms |
23268 KB |
Output is correct |
38 |
Correct |
210 ms |
23296 KB |
Output is correct |
39 |
Correct |
205 ms |
21928 KB |
Output is correct |
40 |
Correct |
194 ms |
21848 KB |
Output is correct |
41 |
Correct |
198 ms |
21824 KB |
Output is correct |
42 |
Correct |
216 ms |
22256 KB |
Output is correct |
43 |
Correct |
190 ms |
22316 KB |
Output is correct |
44 |
Correct |
217 ms |
22252 KB |
Output is correct |
45 |
Correct |
229 ms |
22232 KB |
Output is correct |
46 |
Correct |
216 ms |
22224 KB |
Output is correct |
47 |
Correct |
176 ms |
21448 KB |
Output is correct |
48 |
Correct |
208 ms |
22708 KB |
Output is correct |
49 |
Correct |
197 ms |
24332 KB |
Output is correct |
50 |
Correct |
152 ms |
23332 KB |
Output is correct |
51 |
Correct |
94 ms |
18932 KB |
Output is correct |
52 |
Correct |
203 ms |
29188 KB |
Output is correct |
53 |
Correct |
197 ms |
21796 KB |
Output is correct |
54 |
Correct |
198 ms |
21796 KB |
Output is correct |
55 |
Correct |
171 ms |
22244 KB |
Output is correct |
56 |
Correct |
167 ms |
21308 KB |
Output is correct |
57 |
Correct |
148 ms |
21028 KB |
Output is correct |
58 |
Correct |
2 ms |
2644 KB |
Output is correct |
59 |
Correct |
2 ms |
2644 KB |
Output is correct |
60 |
Correct |
2 ms |
2644 KB |
Output is correct |
61 |
Correct |
1 ms |
2644 KB |
Output is correct |
62 |
Correct |
2 ms |
2644 KB |
Output is correct |
63 |
Correct |
2 ms |
2644 KB |
Output is correct |
64 |
Correct |
2 ms |
2644 KB |
Output is correct |
65 |
Correct |
239 ms |
30196 KB |
Output is correct |
66 |
Correct |
201 ms |
23364 KB |
Output is correct |
67 |
Correct |
230 ms |
31140 KB |
Output is correct |
68 |
Correct |
78 ms |
18900 KB |
Output is correct |
69 |
Correct |
2 ms |
2732 KB |
Output is correct |
70 |
Correct |
2 ms |
2644 KB |
Output is correct |
71 |
Correct |
2 ms |
2644 KB |
Output is correct |
72 |
Correct |
2 ms |
2644 KB |
Output is correct |
73 |
Correct |
2 ms |
2644 KB |
Output is correct |
74 |
Correct |
2 ms |
2644 KB |
Output is correct |
75 |
Correct |
3 ms |
2680 KB |
Output is correct |
76 |
Correct |
2 ms |
2688 KB |
Output is correct |
77 |
Correct |
2 ms |
2644 KB |
Output is correct |
78 |
Correct |
2 ms |
2644 KB |
Output is correct |
79 |
Correct |
2 ms |
2644 KB |
Output is correct |
80 |
Correct |
2 ms |
2688 KB |
Output is correct |
81 |
Correct |
2 ms |
2680 KB |
Output is correct |
82 |
Correct |
2 ms |
2644 KB |
Output is correct |
83 |
Correct |
2 ms |
2684 KB |
Output is correct |