#include <bits/stdc++.h>
#define int long long
#define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;
const int N = 1e5+5;
int visited[N], arr[N], sz[N], visited2[N], check[N], dp[N][2];
vector<int> adj[N];
void dfs(int u){
if(arr[u]==u) check[u] = 1;
sz[u] = 1;
visited[u] = 1;
for(auto v : adj[u]){
if(visited[v]) continue;
dfs(v);
sz[u] += sz[v];
check[u] |= check[v];
}
}
void dfs2(int u, int p){
visited[u] = 1;
sz[u] = 1;
int sum = 0;
for(auto v : adj[u]){
if(v==p) continue;
dfs2(v,u);
sum += dp[v][0];
sz[u] += sz[v];
}
dp[u][1] = sum+1;
int mx = 0;
for(auto v : adj[u]){
if(v==p) continue;
mx = max(dp[v][1]-dp[v][0],mx);
}
dp[u][0] = mx+dp[u][1]-1;
}
int idx;
map<string,int> m;
int assign(string x){
if(m.find(x)==m.end()){
m[x] = idx++;
}
return m[x];
}
signed main(){
fastio
int n;
cin >> n;
for(int i = 0;i < n;i++){
string a,b;
cin >> a >> b;
int x = assign(a);
int y = assign(b);
arr[x] = y;
if(x==y) continue;
adj[x].push_back(y);
adj[y].push_back(x);
}
if(n%2){
cout << -1 << "\n";
return 0;
}
int ans = 0;
for(int i = 0; i < n; i++){
if(arr[i]==i){
dfs2(i,i);
ans += sz[i]-dp[i][0];
}
}
cout << ans << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Correct |
1 ms |
2636 KB |
Output is correct |
4 |
Incorrect |
1 ms |
2636 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Incorrect |
2 ms |
2636 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
239 ms |
17860 KB |
Output is correct |
2 |
Correct |
246 ms |
21920 KB |
Output is correct |
3 |
Correct |
150 ms |
16496 KB |
Output is correct |
4 |
Correct |
271 ms |
17048 KB |
Output is correct |
5 |
Correct |
266 ms |
28504 KB |
Output is correct |
6 |
Correct |
244 ms |
20240 KB |
Output is correct |
7 |
Correct |
211 ms |
20272 KB |
Output is correct |
8 |
Correct |
230 ms |
18880 KB |
Output is correct |
9 |
Correct |
209 ms |
20472 KB |
Output is correct |
10 |
Correct |
149 ms |
20408 KB |
Output is correct |
11 |
Correct |
2 ms |
2636 KB |
Output is correct |
12 |
Correct |
2 ms |
2636 KB |
Output is correct |
13 |
Correct |
2 ms |
2672 KB |
Output is correct |
14 |
Correct |
2 ms |
2636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Correct |
1 ms |
2636 KB |
Output is correct |
4 |
Incorrect |
1 ms |
2636 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |