#include <bits/stdc++.h>
#define SQR(x) (1LL * ((x) * (x)))
#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)
#define fi first
#define se second
#define pb push_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(s) (int)s.size()
#define prev __prev
#define next __next
#define left __left
#define right __right
#define mp make_pair
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vll vector<ll>
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef unsigned int ui;
using namespace std;
const int mod = 1e9 + 7;
const int INF = 1e9 + 7;
const ll INFLL = (ll)2e18 + 7LL;
const ld PI = acos(-1);
const int dx[] = {1, -1, 0, 0, -1, 1, 1, -1};
const int dy[] = {0, 0, 1, -1, -1, -1, 1, 1};
template<class BUI, class TRONG>
bool minimize(BUI &x, const TRONG y){
if(x > y){
x = y;
return true;
} else return false;
}
template<class BUI, class TRONG>
bool maximize(BUI &x, const TRONG y){
if(x < y){
x = y;
return true;
} else return false;
}
/* Author : Bui Nguyen Duc Trong, Luong Van Chanh High School for the gifted*/
/* Template is copied by Trong */
/** Losing in Provincial Contests **/
/** TRYING HARDER**/
/** ORZ **/
/* -----------------[ MAIN CODE GOES HERE ]----------------- */
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int N = 1e5 + 10;
int n;
vector<int> adj[N];
int c[N];
int specNode, root;
int f[N][2][2][2][2];
void preDfs(int u, int p){
c[u] = 1;
for(int v : adj[u]){
if(v == p) continue;
if(c[v] == 0) preDfs(v, u);
else if(c[v] == 1){
specNode = v;
root = u;
return;
}
}
c[u] = 2;
}
void dfs(int u, int p, int me, int up, int rt, int sp){
if(u == root && rt != me) return;
if(u == specNode && sp != me) return;
if(u == specNode && rt && up) return;
for(int v : adj[u]){
if(v == p || (u == specNode && v == root) || (u == root && v == specNode)) continue;
dfs(v, u, 0, me, rt, sp);
dfs(v, u, 1, me, rt, sp);
}
ll tot = 0;
for(int v : adj[u]){
if(v == p || (u == specNode && v == root) || (u == root && v == specNode)) continue;
tot += f[v][0][me][rt][sp];
}
if(up){
if(tot < INF) f[u][me][up][rt][sp] = tot + me;
} else{
for(int v : adj[u]){
if(v == p || (u == specNode && v == root) || (u == root && v == specNode)) continue;
ll z = tot - f[v][0][me][rt][sp] + f[v][1][me][rt][sp];
if(z < INF) minimize(f[u][me][up][rt][sp], z + me);
}
}
}
void solve(){
cin >> n;
for(int i = 1; i <= n; i++){
int u, v; cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
for(int i = 1; i <= n; i++) c[i] = 0;
preDfs(1, 0);
int root = 1 != specNode ? 1 : 2;
int ans = INF;
memset(f, 0x3f, sizeof f);
for(int x = 0; x < 2; x++){
for(int y = 0; y < 2; y++){
dfs(root, specNode, x, y, x, y);
minimize(ans, f[root][x][y][x][y]);
}
}
cout << (ans < INF ? ans : -1) << '\n';
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
const bool multitest = 0;
int tt = 1; if(multitest) cin >> tt;
while( tt-- ){
solve();
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
9304 KB |
Output is correct |
2 |
Correct |
2 ms |
9304 KB |
Output is correct |
3 |
Correct |
2 ms |
9308 KB |
Output is correct |
4 |
Correct |
2 ms |
9308 KB |
Output is correct |
5 |
Execution timed out |
1088 ms |
21908 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
9308 KB |
Output is correct |
2 |
Correct |
2 ms |
9308 KB |
Output is correct |
3 |
Incorrect |
2 ms |
9304 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
9308 KB |
Output is correct |
2 |
Correct |
2 ms |
9308 KB |
Output is correct |
3 |
Incorrect |
2 ms |
9304 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
9304 KB |
Output is correct |
2 |
Correct |
2 ms |
9304 KB |
Output is correct |
3 |
Correct |
2 ms |
9308 KB |
Output is correct |
4 |
Correct |
2 ms |
9308 KB |
Output is correct |
5 |
Execution timed out |
1088 ms |
21908 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |