This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int mxn = 2e5+5;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
typedef pair<int,int>pii;
typedef pair<int,pii>piii;
#define sf scanf
#define pf printf
#define input freopen("in.txt","r",stdin)
#define output freopen("out.txt","w",stdout)
#define inf 1e18
#define ff first
#define ss second
#define MP make_pair
#define pb push_back
#define all(v) v.begin(), v.end()
#define printcase(cases) printf("Case %d:", cases);
#define Unique(a) a.erase(unique(a.begin(),a.end()),a.end())
#define FAST ios_base::sync_with_stdio(0);cout.tie(0)
#define endl printf("\n")
#define __lcm(a, b) ((a*b)/__gcd(a, b))
map<string,int>mark;
vector<pii>edge;
int vis[30];
int cnt = 0;
vector<int>adj[30];
void clear_all()
{
for(int i = 0; i < 25; i++) adj[i].clear();
memset(vis, 0, sizeof vis);
}
void dfs(int u)
{
vis[u] = 1;
cnt++;
for(auto v : adj[u])
{
if(vis[v]) continue;
dfs(v);
}
}
int main()
{
// input;
// output;
int n;
scanf("%d", &n);
int pos = 1;
for(int i = 0; i < n; i++)
{
string a, b;
cin >> a >> b;
if(!mark[a]) mark[a] = pos++;
if(!mark[b]) mark[b] = pos++;
int u = mark[a];
int v = mark[b];
//cout << u << ' ' << v << '\n';
edge.push_back({u, v});
}
if(n&1)
{
cout << "-1\n";
return 0;
}
int lim = (1<<n);
int ans = 50;
for(int b = 0; b < lim; b++)
{
clear_all();
int ok = true;
for(int i = 0; i < n; i++)
{
int x = b&(1<<i);
if(!x && edge[i].ff == edge[i].ss)
{
ok = false;
break;
}
if(!x)
{
adj[edge[i].ff].push_back(edge[i].ss);
adj[edge[i].ss].push_back(edge[i].ff);
}
}
if(!ok) continue;
ok = true;
for(int i = 1; i <= n; i++)
{
if(!vis[i])
{
cnt = 0;
dfs(i);
if(cnt>2)
{
ok = false;
break;
}
}
}
if(ok)
{
int ase = __builtin_popcount(b);
//cout << b << '\n';
ans = min(ans, ase);
}
}
cout << ans << '\n';
return 0;
}
Compilation message (stderr)
polygon.cpp: In function 'int main()':
polygon.cpp:57:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
57 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |