Submission #1272069

#TimeUsernameProblemLanguageResultExecution timeMemory
1272069thesenLogičari (COCI21_logicari)C++20
10 / 110
22 ms5916 KiB
#include <bits/stdc++.h> #define pb push_back #define ll long long #define vll vector <ll> #define vbool vector<bool> #define pairll pair<ll,ll> #define fi first #define sc second #define rever greater<ll>() using namespace std; ll res = 0; void dfs(vector<vll> &path, vll &par, vbool &vis, ll x){ if(vis[x])return; vis[x] = true; bool ans = 1; for(ll j : path[x]){ if(par[j] != 0 && par[j] != x){ans = false; break;} } if(ans){ res++; for(ll j : path[x])par[j] = x; } for(ll j : path[x])dfs(path, par, vis, j); } void solve(){ ll n; cin >> n; vector<vll> path(n+1); for(ll i = 0, u, v; i < n; i++){ cin >> u >> v; path[u].pb(v); path[v].pb(u); } ll a = 0, b = 0; for(ll i = 1; i <= n; i++){ if(path[i].size() == 1){ if(a)b = i; else a = i; } } if(a == 0){ if(n%4)cout << -1 << endl; else cout << n/2 << endl; return; } if(a)a = path[a][0]; if(b)b = path[b][0]; ll tot = -1; for(ll st : path[a]){ vll par(n+1); for(ll i : path[a])par[i] = a; if(b){ for(ll i : path[b]){ if(par[i] != 0){ cout << -1 << endl; return; }par[i] = b; } } vbool vis(n+1); vis[a] = vis[b] = vis[st] = true; res = 1; if(a)res++; if(b)res++; bool ans = true; for(ll i : path[st]){ if(par[i]){ ans = false; break; }par[i] = st; } for(ll i : path[a])dfs(path, par, vis, i); for(ll i : path[st])dfs(path, par, vis, st); if(b){ for(ll i : path[b])dfs(path, par, vis, i); } for(ll i = 1; i <= n; i++){ if(!par[i])ans = false; } if(ans){ if(tot == -1)tot = res; else tot = min(tot, res); } }cout << tot << endl; } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); ll t=1; //cin >> t; for(int i = 1; i <= t; i++){ solve(); } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...