#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define name "main"
const int N=(int)1e5;
vector<string>nen;
string s1[N+2],s2[N+2];
int deg[N+2],num[N+2],low[N+2],id[N+2],scc=0,timedfs=0;
int u[N+2],v[N+2],to[N+2],dp[N+2][2],n;
vector<int>ord,bag[N+2],ke[N+2];
bool used[N+2],del[N+2];
void buildup(int u){
num[u]=low[u]=++timedfs;
ord.push_back(u);
if (!num[to[u]]&&!del[to[u]]){
buildup(to[u]);
low[u]=min(low[u],low[to[u]]);
}
else if (!del[to[u]]) low[u]=min(low[u],num[to[u]]);
if (low[u]==num[u]){
int v;
++scc;
do{
v=ord.back();
ord.pop_back();
id[v]=scc;
del[v]=true;
bag[id[v]].push_back(v);
}while(v!=u);
}
}
void dfs(int u,int ban){
if (ke[u].size()==0){
dp[u][0]=0;
dp[u][1]=1;
return;
}
dp[u][0]=dp[u][1]=0;
for(auto&v:ke[u]){
if (v==ban) continue;
dfs(v,ban);
dp[u][1]+=dp[v][0];
}
dp[u][1]++;
for(auto&v:ke[u]){
if (v==ban) continue;
dp[u][0]=max(dp[u][0],dp[u][1]-1+max(0,dp[v][1]-dp[v][0]));
}
}
int tinhdfs(int u){
if (bag[u].size()==1){
dfs(bag[u][0],-1);
return dp[bag[u][0]][0];
}
if (bag[u].size()==2){
int ans=2;
int x1=bag[u][0],x2=bag[u][1];
for(auto&x:ke[x1]) {
if (x==x2) continue;
dfs(x,-1);
ans+=dp[x][0];
}
for(auto&x:ke[x2]){
if (x==x1) continue;
dfs(x,-1);
ans+=dp[x][0];
}
return ans;
}
int w1=1;
int nn=bag[u][0],tv=to[nn];
for(auto&v:ke[nn]){
dfs(v,tv);
w1+=dp[v][0];
}
for(auto&v:ke[tv]){
if (v==nn) continue;
dfs(v,nn);
w1+=dp[v][0];
}
dfs(nn,nn);
int w2=dp[nn][0];
return max(w1,w2);
}
int32_t main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin>>n;
if (n%2==1){
cout<<-1;
exit(0);
}
for(int i=1;i<=n;++i){
cin>>s1[i]>>s2[i];
nen.push_back(s1[i]);
nen.push_back(s2[i]);
}
sort(nen.begin(),nen.end());
nen.resize(unique(nen.begin(),nen.end())-nen.begin());
for(int i=1;i<=n;++i){
u[i]=upper_bound(nen.begin(),nen.end(),s1[i])-nen.begin();
v[i]=upper_bound(nen.begin(),nen.end(),s2[i])-nen.begin();
to[u[i]]=v[i];
if (v[i]!=u[i]) ke[v[i]].push_back(u[i]);
}
for(int i=1;i<=n;++i) if (!num[i]) buildup(i);
for(int i=1;i<=scc;++i){
for(auto&u:bag[i])
if (id[to[u]]!=i) ++deg[i];
}
int ans=0;
for(int i=1;i<=scc;++i){
if (deg[i]==0){
ans+=tinhdfs(i);
}
}
cout<<n-ans;
exit(0);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
14928 KB |
Output is correct |
2 |
Correct |
3 ms |
14928 KB |
Output is correct |
3 |
Correct |
3 ms |
14928 KB |
Output is correct |
4 |
Correct |
3 ms |
14928 KB |
Output is correct |
5 |
Correct |
3 ms |
12880 KB |
Output is correct |
6 |
Correct |
4 ms |
12924 KB |
Output is correct |
7 |
Correct |
3 ms |
14928 KB |
Output is correct |
8 |
Correct |
4 ms |
15096 KB |
Output is correct |
9 |
Correct |
3 ms |
14928 KB |
Output is correct |
10 |
Correct |
3 ms |
14928 KB |
Output is correct |
11 |
Correct |
4 ms |
14928 KB |
Output is correct |
12 |
Correct |
4 ms |
14936 KB |
Output is correct |
13 |
Correct |
3 ms |
14928 KB |
Output is correct |
14 |
Correct |
5 ms |
15100 KB |
Output is correct |
15 |
Correct |
3 ms |
14928 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
14928 KB |
Output is correct |
2 |
Correct |
3 ms |
14928 KB |
Output is correct |
3 |
Correct |
4 ms |
14928 KB |
Output is correct |
4 |
Correct |
150 ms |
34224 KB |
Output is correct |
5 |
Correct |
149 ms |
29104 KB |
Output is correct |
6 |
Correct |
152 ms |
35328 KB |
Output is correct |
7 |
Correct |
3 ms |
13048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
138 ms |
30696 KB |
Output is correct |
2 |
Correct |
136 ms |
32620 KB |
Output is correct |
3 |
Correct |
98 ms |
27992 KB |
Output is correct |
4 |
Correct |
3 ms |
12880 KB |
Output is correct |
5 |
Correct |
148 ms |
35504 KB |
Output is correct |
6 |
Correct |
133 ms |
30128 KB |
Output is correct |
7 |
Correct |
136 ms |
29952 KB |
Output is correct |
8 |
Correct |
113 ms |
27568 KB |
Output is correct |
9 |
Correct |
122 ms |
29464 KB |
Output is correct |
10 |
Correct |
110 ms |
26544 KB |
Output is correct |
11 |
Correct |
3 ms |
14928 KB |
Output is correct |
12 |
Correct |
3 ms |
14928 KB |
Output is correct |
13 |
Correct |
4 ms |
14940 KB |
Output is correct |
14 |
Correct |
4 ms |
14928 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
14928 KB |
Output is correct |
2 |
Correct |
3 ms |
14928 KB |
Output is correct |
3 |
Correct |
3 ms |
14928 KB |
Output is correct |
4 |
Correct |
3 ms |
14928 KB |
Output is correct |
5 |
Correct |
3 ms |
12880 KB |
Output is correct |
6 |
Correct |
4 ms |
12924 KB |
Output is correct |
7 |
Correct |
3 ms |
14928 KB |
Output is correct |
8 |
Correct |
4 ms |
15096 KB |
Output is correct |
9 |
Correct |
3 ms |
14928 KB |
Output is correct |
10 |
Correct |
3 ms |
14928 KB |
Output is correct |
11 |
Correct |
4 ms |
14928 KB |
Output is correct |
12 |
Correct |
4 ms |
14936 KB |
Output is correct |
13 |
Correct |
3 ms |
14928 KB |
Output is correct |
14 |
Correct |
5 ms |
15100 KB |
Output is correct |
15 |
Correct |
3 ms |
14928 KB |
Output is correct |
16 |
Correct |
3 ms |
14928 KB |
Output is correct |
17 |
Correct |
3 ms |
14928 KB |
Output is correct |
18 |
Correct |
4 ms |
14928 KB |
Output is correct |
19 |
Correct |
150 ms |
34224 KB |
Output is correct |
20 |
Correct |
149 ms |
29104 KB |
Output is correct |
21 |
Correct |
152 ms |
35328 KB |
Output is correct |
22 |
Correct |
3 ms |
13048 KB |
Output is correct |
23 |
Correct |
138 ms |
30696 KB |
Output is correct |
24 |
Correct |
136 ms |
32620 KB |
Output is correct |
25 |
Correct |
98 ms |
27992 KB |
Output is correct |
26 |
Correct |
3 ms |
12880 KB |
Output is correct |
27 |
Correct |
148 ms |
35504 KB |
Output is correct |
28 |
Correct |
133 ms |
30128 KB |
Output is correct |
29 |
Correct |
136 ms |
29952 KB |
Output is correct |
30 |
Correct |
113 ms |
27568 KB |
Output is correct |
31 |
Correct |
122 ms |
29464 KB |
Output is correct |
32 |
Correct |
110 ms |
26544 KB |
Output is correct |
33 |
Correct |
3 ms |
14928 KB |
Output is correct |
34 |
Correct |
3 ms |
14928 KB |
Output is correct |
35 |
Correct |
4 ms |
14940 KB |
Output is correct |
36 |
Correct |
4 ms |
14928 KB |
Output is correct |
37 |
Correct |
146 ms |
31152 KB |
Output is correct |
38 |
Correct |
146 ms |
30712 KB |
Output is correct |
39 |
Correct |
130 ms |
29560 KB |
Output is correct |
40 |
Correct |
136 ms |
30128 KB |
Output is correct |
41 |
Correct |
138 ms |
30072 KB |
Output is correct |
42 |
Correct |
129 ms |
30320 KB |
Output is correct |
43 |
Correct |
126 ms |
30384 KB |
Output is correct |
44 |
Correct |
135 ms |
30576 KB |
Output is correct |
45 |
Correct |
136 ms |
30384 KB |
Output is correct |
46 |
Correct |
153 ms |
30328 KB |
Output is correct |
47 |
Correct |
123 ms |
29628 KB |
Output is correct |
48 |
Correct |
131 ms |
30896 KB |
Output is correct |
49 |
Correct |
145 ms |
32688 KB |
Output is correct |
50 |
Correct |
121 ms |
28524 KB |
Output is correct |
51 |
Correct |
3 ms |
12880 KB |
Output is correct |
52 |
Correct |
135 ms |
37564 KB |
Output is correct |
53 |
Correct |
130 ms |
30128 KB |
Output is correct |
54 |
Correct |
117 ms |
30128 KB |
Output is correct |
55 |
Correct |
143 ms |
29552 KB |
Output is correct |
56 |
Correct |
126 ms |
29556 KB |
Output is correct |
57 |
Correct |
83 ms |
28792 KB |
Output is correct |
58 |
Correct |
3 ms |
14928 KB |
Output is correct |
59 |
Correct |
4 ms |
15184 KB |
Output is correct |
60 |
Correct |
3 ms |
14928 KB |
Output is correct |
61 |
Correct |
4 ms |
14928 KB |
Output is correct |
62 |
Correct |
3 ms |
14928 KB |
Output is correct |
63 |
Correct |
3 ms |
14928 KB |
Output is correct |
64 |
Correct |
3 ms |
14928 KB |
Output is correct |
65 |
Correct |
153 ms |
36460 KB |
Output is correct |
66 |
Correct |
155 ms |
29104 KB |
Output is correct |
67 |
Correct |
153 ms |
37620 KB |
Output is correct |
68 |
Correct |
3 ms |
12880 KB |
Output is correct |
69 |
Correct |
3 ms |
14928 KB |
Output is correct |
70 |
Correct |
3 ms |
14928 KB |
Output is correct |
71 |
Correct |
3 ms |
14928 KB |
Output is correct |
72 |
Correct |
3 ms |
14928 KB |
Output is correct |
73 |
Correct |
3 ms |
12880 KB |
Output is correct |
74 |
Correct |
4 ms |
12880 KB |
Output is correct |
75 |
Correct |
3 ms |
14928 KB |
Output is correct |
76 |
Correct |
3 ms |
14928 KB |
Output is correct |
77 |
Correct |
3 ms |
14928 KB |
Output is correct |
78 |
Correct |
3 ms |
14968 KB |
Output is correct |
79 |
Correct |
3 ms |
14928 KB |
Output is correct |
80 |
Correct |
3 ms |
14972 KB |
Output is correct |
81 |
Correct |
3 ms |
14928 KB |
Output is correct |
82 |
Correct |
3 ms |
14928 KB |
Output is correct |
83 |
Correct |
3 ms |
14972 KB |
Output is correct |