#include <bits/stdc++.h>
#define el '\n'
#define FNAME "NAME"
#define allof(x) x.begin(),x.end()
#define allof1(x) x.begin()+1,x.end()
#define mset(x,n) memset(x,(n),sizeof(x))
using namespace std;
const long long MOD = (long long) 1e9+7;
template<class X,class Y> bool minimize(X &a,Y b){ if (a>b) {a=b; return true;} return false;}
template<class X,class Y> bool maximize(X &a,Y b){ if (a<b) {a=b; return true;} return false;}
void setup(){
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
if (fopen(FNAME".inp","r")){
freopen(FNAME".inp","r",stdin);
freopen(FNAME".out","w",stdout);
}
}
const int MAXN = 1e5 + 1;
const int INF = 1e9 + 15092007;
int n;
vector<int> graph[MAXN];
vector<pair<int,int>> edges;
void init(){
cin>>n;
for (int i=0;i<n;i++){
int u,v;
cin>>u>>v;
edges.emplace_back(u,v);
graph[u].push_back(v);
graph[v].push_back(u);
}
}
namespace SimpleCycle{
bool check(){
for (int i=1;i<=n;i++){
if (graph[i].size() != 2) return false;
}
return true;
}
void solve(){
cout<< (n%4 ? -1 : n/2);
exit(0);
}
};
namespace Bitmask{
bitset<21> visited,blueEye;
void dfs(int u,bool &seen){
if (seen) return;
visited[u]=1;
int cnt = 0;
for (int v : graph[u]){
if (blueEye[v]) cnt++;
}
if (cnt!=1) {seen=1;return;}
for (int v : graph[u]){
if (!visited[v]){
dfs(v,seen);
}
}
}
void solve(){
int MAXMASK = 1<<n;
int res = MAXMASK;
for (int mask=1;mask<MAXMASK;mask++){
int be = __builtin_popcount(mask);
visited.reset();
blueEye.reset();
for (int i=0;i<n;i++){
if (mask & (1<<i)){
blueEye[i+1]=1;
}
}
bool check=0;
dfs(1,check);
if (!check) minimize(res,be);
}
cout<< (res==MAXMASK ? -1 : res);
exit(0);
}
};
namespace SubFinal{
namespace DSU{
vector<int> parent;
vector<int> power;
void make_set(){
parent.assign(n+1,0);
power.assign(n+1,1);
for (int i=1;i<=n;i++){
parent[i] = i;
power[i] = 1;
}
}
int find(int u){
if (u==parent[u]) return u;
return parent[u] = find(parent[u]);
}
bool Union(int u,int v){
u = find(u);
v = find(v);
if (u==v) return false;
if (power[u] < power[v]) swap(u,v);
parent[v] = u;
power[u] += power[v];
return true;
}
};
int root,spe;
// ! dp[u][colourU][colourRoot][colourPar][colourSpe]
// ! number of blueEyed logician at u's child
long long dp[MAXN][2][2][2][2];
vector<int> adj[MAXN];
int parent[MAXN];
void dfs(int u,int pre){
parent[u] = pre;
for (int v : adj[u]){
if (v^pre){
dfs(v,u);
}
}
}
void initGraph(){
DSU::make_set();
for (pair<int,int> x : edges){
int u,v; tie(u,v) = x;
if (DSU::find(u) != DSU::find(v)){
DSU::Union(u,v);
adj[u].push_back(v);
adj[v].push_back(u);
} else{
root = u;
spe = v;
}
}
}
long long cal(int u,int colourU,int cR,int cP,int cS){
long long &res = dp[u][colourU][cR][cP][cS];
if (~res) return res;
bool check=1;
if (u==root and colourU ^ cR) check=0;
if (u==spe and colourU ^ cS) check=0;
if (u==spe and cP and cR) check=0;
if (!check) return res=INF;
res = INF;
long long sum = colourU;
for (int v : adj[u]){
if (v==parent[u]) continue;
sum += cal(v,0,cR,colourU,cS);
}
bool cover=0;
if (cP) cover = 1;
if (u==root and cS) cover=1;
if (u==spe and cR) cover=1;
if (cover) minimize(res,sum);
else{
for (int v : adj[u]){
if (v==parent[u]) continue;
long long val = sum - cal(v,0,cR,colourU,cS) + cal(v,1,cR,colourU,cS);
minimize(res,val);
}
}
return res;
}
void solve(){
initGraph();
dfs(root,0);
mset(dp,-1);
long long res = INF;
for (int cR:{0,1}) for (int cS:{0,1}) minimize(res,cal(root,cR,cR,0,cS));
cout<< (res==INF ? -1 : res);
exit(0);
}
};
void sol(){
if (n<=20) Bitmask::solve();
if (SimpleCycle::check()) SimpleCycle::solve();
SubFinal::solve();
}
int main(){
setup();
init();
sol();
}
Compilation message (stderr)
Main.cpp: In function 'void setup()':
Main.cpp:16:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
16 | freopen(FNAME".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:17:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
17 | freopen(FNAME".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |