#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;
#define REP(i,a,b) for(ll i=(ll) a; i<(ll) b; i++)
#define pb push_back
#define mp make_pair
#define pl pair<ll,ll>
#define ff first
#define ss second
#define whole(x) x.begin(),x.end()
#define DEBUG(i) cout<<"WAFFLES "<<i<<"<\n"
#define INF 1000000000000000000LL
#define EPS (0.00000000001L)
#define pi (3.141592653589793L)
#define VV(vvvv,NNNN,xxxx); REP(iiiii,0,NNNN) {vvvv.pb(xxxx);}
ll mod=1000000007LL;
template<class A=ll>
void Out(vector<A> a) {REP(i,0,a.size()) {cout<<a[i]<<" ";} cout<<endl;}
template<class A=ll>
void In(vector<A> &a, ll N) {A cur; REP(i,0,N) {cin>>cur; a.pb(cur);}}
vector<vector<ll>> adj;
ll n;
vector<bool> vis;
pair<ll, ll> cyc;
pair<ll,ll> dfs(ll prv, ll nd){
vis[nd] = true;
pair<ll, ll> sol = {-1, -1};
for(ll nb : adj[nd]){
if(nb == prv)
continue;
if(vis[nb]){
return {nd,nb};
}
sol = max(sol, dfs(nd, nb));
if(sol.ff > -1)
break;
}
return sol;
}
bool root_seeing, root_blue, cyc_blue;
ll tree_dfs(ll pr, ll nd, bool seeing, bool blue){
if(nd == cyc.ss){
if(blue != root_seeing){
return INF;
}
if(seeing && root_blue){
return INF;
}
if(adj[nd].size() == 2){
if(seeing || root_blue)
return blue;
else
return INF;
}
if(root_blue)
seeing = true;
}
if(adj[nd].size() == 1){
if(seeing){
return blue;
}else{
return INF;
}
}
if(seeing){
if(blue){
ll sum = 0;
for(ll ch : adj[nd]){
if(ch == pr)
continue;
if(ch == cyc.ff)
continue;
sum += tree_dfs(nd, ch, true, false);
}
return sum + 1;
}else{
ll sum = 0;
for(ll ch : adj[nd]){
if(ch == pr)
continue;
if(ch == cyc.ff)
continue;
sum += tree_dfs(nd, ch, false, false);
}
return sum;
}
}else{
if(blue){
ll sum = 0;
ll mndf = INF;
for(ll ch : adj[nd]){
if(ch == pr)
continue;
if(ch == cyc.ff)
continue;
ll nb_ntb = tree_dfs(nd, ch, true, false);
ll nb_b = tree_dfs(nd, ch, true, true);
sum += nb_ntb;
mndf = min(mndf, nb_b-nb_ntb);
}
return sum + mndf + 1;
}else{
ll sum = 0;
ll mndf = INF;
for(ll ch : adj[nd]){
if(ch == pr)
continue;
if(ch == cyc.ff)
continue;
ll nb_ntb = tree_dfs(nd, ch, false, false);
ll nb_b = tree_dfs(nd, ch, false, true);
sum += nb_ntb;
mndf = min(mndf, nb_b-nb_ntb);
}
return sum + mndf;
}
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cout.precision(20);
cin >> n;
adj.assign(n,{});
ll a, b;
for(ll i = 0; i < n; ++i){
cin >> a >> b;
a--;b--;
adj[a].pb(b);
adj[b].pb(a);
}
vis.assign(n, 0);
cyc = dfs(-1, 0);
ll mn = INF;
root_seeing = false;
root_blue = false;
mn = min(mn, tree_dfs(cyc.ss, cyc.ff, false, false));
root_seeing = false;
root_blue = true;
mn = min(mn, tree_dfs(cyc.ss, cyc.ff, false, true));
root_seeing = true;
root_blue = false;
mn = min(mn, tree_dfs(cyc.ss, cyc.ff, true, false));
root_seeing = true;
root_blue = true;
mn = min(mn, tree_dfs(cyc.ss, cyc.ff, true, true));
if(mn >= INF){
cout << "-1\n";
}else{
cout << mn << "\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
232 KB |
Output is correct |
5 |
Execution timed out |
1095 ms |
16232 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
328 KB |
Output is correct |
9 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
328 KB |
Output is correct |
9 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
232 KB |
Output is correct |
5 |
Execution timed out |
1095 ms |
16232 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |