#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int long long
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (b); i >= (a); i --)
#define REP(i, a) for (int i = 0; i < (a); ++i)
#define REPD(i, a) for (int i = (a) - 1; i >= 0; --i)
#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)
constexpr ll LINF = (1ll << 60);
constexpr int INF = (1ll << 30);
constexpr int Mod = 1e9 + 7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
/*
Phu Trong from Nguyen Tat Thanh High School for gifted student
*/
template <class X, class Y>
bool minimize(X &x, const Y &y){
X eps = 1e-9;
if (x > y + eps) {x = y; return 1;}
return 0;
}
template <class X, class Y>
bool maximize(X &x, const Y &y){
X eps = 1e-9;
if (x + eps < y) {x = y; return 1;}
return 0;
}
#define MAX 100005
int numNode;
vector<int> G[MAX];
//dp(u, me, pa, root, special)
int vis[MAX];
int special = -1, root = -1;
void pre_dfs(int u, int p = -1){
vis[u] = 1;
for (int &v : G[u]) if(v ^ p){
if(vis[v]){
special = v, root = u;
}
if(!vis[v])
pre_dfs(v, u);
}
}
int dp[MAX][2][2][2][2];
bool bad(int u, int self, int pa, int rt, int sc){
if (u == root && self != rt) return true;
if (u == special && self != sc) return true;
if (u == special && (pa & root) == 1) return true;
return false;
}
bool check (int u, int v){
return ((u != special && v != special) || (u != root && v != root));
}
int F(int u, int p, int self, int pa, int rt, int sc){
int &ret = dp[u][self][pa][rt][sc];
if(~ret) return ret;
ret = INF;
if (bad(u, self, pa, rt, sc)) {
return ret;
}
bool must = 0;
must |= pa;
must |= (u == special && root == 1);
must |= (u == root && special == 1);
int sm = 0;
if (self) ++sm;
for (int&v : G[u]) if(v ^ p){
if(!check(u, v)) continue;
sm += F(v, u, 0, self, rt, sc);
}
if (must){
minimize(ret, sm);
}
if (!must){
for (int &v : G[u]) if(v ^ p){
if(!check(u, v)) continue;
int cur = sm - F(v, u, 0, self, rt, sc) + F(v, u, 1, self, rt, sc);
minimize(ret, cur);
}
}
if (ret > INF) ret = INF;
return ret;
}
void process(void){
cin >> numNode;
for (int i = 1; i <= numNode; ++i){
int u, v; cin >> u >> v;
G[u].emplace_back(v);
G[v].emplace_back(u);
}
pre_dfs(1);
int ans = INF;
REP(i, 2) REP(j, 2){
memset(dp, -1, sizeof dp);
minimize(ans, F(root, 0, i, 0, i, j));
}
if(ans == INF) ans = -1;
cout << ans;
}
signed main(){
#define name "Whisper"
cin.tie(nullptr) -> sync_with_stdio(false);
//freopen(name".inp", "r", stdin);
//freopen(name".out", "w", stdout);
process();
return (0 ^ 0);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
16216 KB |
Output is correct |
2 |
Correct |
5 ms |
15964 KB |
Output is correct |
3 |
Incorrect |
5 ms |
15964 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
15964 KB |
Output is correct |
2 |
Incorrect |
5 ms |
16052 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
15964 KB |
Output is correct |
2 |
Incorrect |
5 ms |
16052 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
16216 KB |
Output is correct |
2 |
Correct |
5 ms |
15964 KB |
Output is correct |
3 |
Incorrect |
5 ms |
15964 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |