#include<bits/stdc++.h>
using namespace std;
map<string,int> mp;
string ss[100010],se[100010];
vector<int> g[100010],gb[100010];
queue<int> topo,bfs;
int in[100010],dp[100010][2],sum,ch[100010];
void dfs(int i,int p){
for(auto x : gb[i]){
if(x==p||in[x]>0) continue;
dfs(x,i);
}
int mx=0;
for(auto x : gb[i]){
if(x==p||in[x]>0) continue;
dp[i][1]+=dp[x][0];
mx=max(mx,dp[x][1]-dp[x][0]);
}
dp[i][0]=mx+dp[i][1];
dp[i][1]++;
printf("%d %d %d %d\n",i,p,dp[i][0],dp[i][1]);
}
int findloop(vector<int> t){
if(t.size()==1) return 0;
}
int main(){
int n,i,j;
scanf("%d",&n);
for(i=1;i<=n;i++){
cin >> ss[i] >> se[i];
mp[ss[i]]=i;
}
if(n%2!=0){
printf("-1");
return 0;
}
for(i=1;i<=n;i++){
int sss=mp[ss[i]];
int sse=mp[se[i]];
g[sss].push_back(sse);
gb[sse].push_back(sss);
in[sse]++;
}
for(i=1;i<=n;i++){
if(in[i]==0) topo.push(i);
}
while(!topo.empty()){
i=topo.front();
topo.pop();
in[i]--;
for(auto x : g[i]){
in[x]--;
if(in[x]==0){
topo.push(x);
}
}
}
int cnt=0;
int ans=0;
for(i=1;i<=n;i++){
if(in[i]>0){
dfs(i,-1);
ans+=dp[i][0];
}
}
vector<int> t;
for(i=1;i<=n;i++){
if(in[i]>0){
if(ch[i]==0){
bfs.push(i);
ch[i]=1;
t.clear();
while(!bfs.empty()){
j=bfs.front();
bfs.pop();
t.push_back(dp[j][1]-dp[j][0]);
for(auto x : g[j]){
if(ch[x]==0){
bfs.push(x);
ch[x]=1;
}
}
}
}
ans+=(max(0,findloop(t)));
}
}
printf("%d\n",n-ans);
return 0;
}
/*
8
leonard emmy
ada emmy
isaac leonard
emmy pierre
pierre bernhard
bernhard emmy
sofia karl
karl sofia
*/
Compilation message
polygon.cpp: In function 'int main()':
polygon.cpp:58:9: warning: unused variable 'cnt' [-Wunused-variable]
58 | int cnt=0;
| ^~~
polygon.cpp: In function 'int findloop(std::vector<int>)':
polygon.cpp:25:1: warning: control reaches end of non-void function [-Wreturn-type]
25 | }
| ^
polygon.cpp: In function 'int main()':
polygon.cpp:28:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
28 | scanf("%d",&n);
| ~~~~~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
7 ms |
11220 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
6 ms |
11220 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
366 ms |
27848 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
7 ms |
11220 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |