#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]) 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 |
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 |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
1 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2684 KB |
Output is correct |
7 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
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 |
Incorrect |
227 ms |
27920 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
188 ms |
20464 KB |
Output is correct |
2 |
Correct |
201 ms |
22100 KB |
Output is correct |
3 |
Correct |
168 ms |
21188 KB |
Output is correct |
4 |
Correct |
83 ms |
16804 KB |
Output is correct |
5 |
Correct |
212 ms |
29296 KB |
Output is correct |
6 |
Correct |
198 ms |
21852 KB |
Output is correct |
7 |
Correct |
196 ms |
21872 KB |
Output is correct |
8 |
Correct |
195 ms |
22276 KB |
Output is correct |
9 |
Correct |
186 ms |
21352 KB |
Output is correct |
10 |
Correct |
134 ms |
21032 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Correct |
1 ms |
2680 KB |
Output is correct |
13 |
Correct |
2 ms |
2644 KB |
Output is correct |
14 |
Correct |
2 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 |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
1 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2684 KB |
Output is correct |
7 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |