#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int>vi;
#define pb push_back
#define sz(x) (int)x.size()
#define all(x) begin(x),end(x)
typedef string str;
#define FOR(i,a,b) for(int i=a; i<b; i++)
#define ROF(i,a,b) for(int i=b-1; i>=a; i--)
void ckmin(int &x, int y){x=min(x,y);}
//------------------------------------
int main(){
int N; cin>>N;
if(N&1){
cout << -1 << endl;
return 0;
}
map<string,int>mp;
int cnt=-1;
vi nxt(N);
FOR(i,0,N){
str s,t; cin>>s>>t;
if(!mp.count(s)) mp[s]=++cnt;
if(!mp.count(t)) mp[t]=++cnt;
int u=mp[s],v=mp[t];
nxt[u]=v;
}
int ans=N;
FOR(m,0,1<<N){
int val=1;
vi taken(N,0);
FOR(i,0,N) if(!((m>>i)&1)){
val&=((nxt[nxt[i]]==i && nxt[i]!=i) || (m>>nxt[i])&1 && !taken[nxt[i]]);
taken[nxt[i]]=1;
}
if(val) ckmin(ans,__builtin_popcount(m));
}
cout << ans << endl;
}
Compilation message
polygon.cpp: In function 'int main()':
polygon.cpp:45:57: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
45 | val&=((nxt[nxt[i]]==i && nxt[i]!=i) || (m>>nxt[i])&1 && !taken[nxt[i]]);
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
132 ms |
264 KB |
Output is correct |
2 |
Correct |
103 ms |
204 KB |
Output is correct |
3 |
Correct |
117 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
113 ms |
268 KB |
Output is correct |
8 |
Correct |
111 ms |
324 KB |
Output is correct |
9 |
Correct |
106 ms |
204 KB |
Output is correct |
10 |
Correct |
115 ms |
272 KB |
Output is correct |
11 |
Correct |
110 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
288 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Incorrect |
318 ms |
10948 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
288 ms |
8900 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
132 ms |
264 KB |
Output is correct |
2 |
Correct |
103 ms |
204 KB |
Output is correct |
3 |
Correct |
117 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
113 ms |
268 KB |
Output is correct |
8 |
Correct |
111 ms |
324 KB |
Output is correct |
9 |
Correct |
106 ms |
204 KB |
Output is correct |
10 |
Correct |
115 ms |
272 KB |
Output is correct |
11 |
Correct |
110 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
288 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Incorrect |
318 ms |
10948 KB |
Output isn't correct |
20 |
Halted |
0 ms |
0 KB |
- |