Submission #69504

# Submission time Handle Problem Language Result Execution time Memory
69504 2018-08-21T06:07:32 Z qkxwsm Cats or Dogs (JOI18_catdog) C++17
38 / 100
3000 ms 8044 KB
#include "catdog.h"
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;

struct chash
{
	int operator()(int x) const
	{
		x ^= (x >> 20) ^ (x >> 12);
		return x ^ (x >> 7) ^ (x >> 4);
	}
	int operator()(long long x) const
	{
		return x ^ (x >> 32);
	}
};

template<typename T> using orderedset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T, typename U> using hashtable = gp_hash_table<T, U, chash>;

template<class T>
void ckmin(T &a, T b)
{
	a = min(a, b);
}
template<class T>
void ckmax(T &a, T b)
{
	a = max(a, b);
}
template<class T, class U>
T normalize(T x, U mod = 1000000007)
{
	return (((x % mod) + mod) % mod);
}
static long long randomizell(long long mod)
{
	return ((1ll << 45) * rand() + (1ll << 30) * rand() + (1ll << 15) * rand() + rand()) % mod;
}
static int randomize(int mod)
{
	return ((1ll << 15) * rand() + rand()) % mod;
}

#define y0 ___y0
#define y1 ___y1
#define MP make_pair
#define MT make_tuple
#define PB push_back
#define PF push_front
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define debug(x) cerr << #x << " = " << x << endl;

const long double PI = 4.0 * atan(1.0);
const long double EPS = 1e-10;

#define MAGIC 347
#define SINF 10007
#define CO 1000007
#define INF 1000000007
#define BIG 1000000931
#define LARGE 1696969696967ll
#define GIANT 2564008813937411ll
#define LLINF 2696969696969696969ll
#define MAXN 100013

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pdd;

int N;
vector<int> edge[MAXN];
int parent[MAXN], depth[MAXN];
int heavy[MAXN], head[MAXN], ranking[MAXN], subtree[MAXN], siz[MAXN];
int arr[MAXN];
ll dp[2][MAXN];

void dfs(int u)
{
	subtree[u] = 1;
	for (int v : edge[u])
	{
		if (v == parent[u]) continue;
		parent[v] = u;
		depth[v] = depth[u] + 1;
		dfs(v);
		subtree[u] += subtree[v];
	}
	heavy[u] = N;
	for (int v : edge[u])
	{
		if (v == parent[u]) continue;
		if (subtree[v] * 2 >= subtree[u]) heavy[u] = v;
	}
}
void dfs_heavy(int u)
{
	if (heavy[u] != N)
	{
		head[heavy[u]] = head[u];
		ranking[heavy[u]] = ranking[u] + 1;
		dfs_heavy(heavy[u]);
	}
	for (int v : edge[u])
	{
		if (v == parent[u]) continue;
		if (v == heavy[u]) continue;
		ranking[v] = 0;
		head[v] = v;
		dfs_heavy(v);
	}
	return;
}
void initialize(int n, vector<int> a, vector<int> b)
{
	N = n;
	for (int i = 0; i < N - 1; i++)
	{
		int u = a[i], v = b[i];
		u--; v--;
		edge[u].PB(v);
		edge[v].PB(u);
	}
	parent[N] = N; parent[0] = N;
	dfs(0);
	dfs_heavy(0);
	for (int i = 0; i < N; i++)
	{
		arr[i] = -1;
	}
}
void solve(int u)
{
	for (int v : edge[u])
	{
		if (v == parent[u]) continue;
		solve(v);
	}
	//this guy is a 0
	dp[0][u] = 0;
	dp[1][u] = 0;
	for (int v : edge[u])
	{
		if (v == parent[u]) continue;
		dp[0][u] += min(dp[0][v], dp[1][v] + 1);
		dp[1][u] += min(dp[1][v], dp[0][v] + 1);
	}
	if (arr[u] == 0) dp[1][u] += INF;
	if (arr[u] == 1) dp[0][u] += INF;
	return;
}
int setval(int u, int v)
{
	// for (int i = 0; i < N; i++)
	// {
	// 	cerr << arr[u] << endl;
	// }
	// cerr << "set " << u << " to " << v << endl;
	arr[u] = v;
	solve(0);
	return min(dp[0][0], dp[1][0]);
}
int cat(int u)
{
	u--;
	return setval(u, 0);
}
int dog(int u)
{
	u--;
	return setval(u, 1);
}
int neighbor(int u)
{
	u--;
	return setval(u, -1);
}

Compilation message

catdog.cpp:44:12: warning: 'int randomize(int)' defined but not used [-Wunused-function]
 static int randomize(int mod)
            ^~~~~~~~~
catdog.cpp:40:18: warning: 'long long int randomizell(long long int)' defined but not used [-Wunused-function]
 static long long randomizell(long long mod)
                  ^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2680 KB Output is correct
2 Correct 6 ms 2816 KB Output is correct
3 Correct 4 ms 2864 KB Output is correct
4 Correct 4 ms 2864 KB Output is correct
5 Correct 4 ms 2864 KB Output is correct
6 Correct 6 ms 2952 KB Output is correct
7 Correct 4 ms 2952 KB Output is correct
8 Correct 4 ms 2952 KB Output is correct
9 Correct 4 ms 2952 KB Output is correct
10 Correct 5 ms 2952 KB Output is correct
11 Correct 4 ms 2952 KB Output is correct
12 Correct 4 ms 3064 KB Output is correct
13 Correct 5 ms 3064 KB Output is correct
14 Correct 4 ms 3064 KB Output is correct
15 Correct 4 ms 3064 KB Output is correct
16 Correct 5 ms 3064 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2680 KB Output is correct
2 Correct 6 ms 2816 KB Output is correct
3 Correct 4 ms 2864 KB Output is correct
4 Correct 4 ms 2864 KB Output is correct
5 Correct 4 ms 2864 KB Output is correct
6 Correct 6 ms 2952 KB Output is correct
7 Correct 4 ms 2952 KB Output is correct
8 Correct 4 ms 2952 KB Output is correct
9 Correct 4 ms 2952 KB Output is correct
10 Correct 5 ms 2952 KB Output is correct
11 Correct 4 ms 2952 KB Output is correct
12 Correct 4 ms 3064 KB Output is correct
13 Correct 5 ms 3064 KB Output is correct
14 Correct 4 ms 3064 KB Output is correct
15 Correct 4 ms 3064 KB Output is correct
16 Correct 5 ms 3064 KB Output is correct
17 Correct 13 ms 3196 KB Output is correct
18 Correct 18 ms 3196 KB Output is correct
19 Correct 13 ms 3196 KB Output is correct
20 Correct 5 ms 3196 KB Output is correct
21 Correct 6 ms 3196 KB Output is correct
22 Correct 8 ms 3196 KB Output is correct
23 Correct 22 ms 3196 KB Output is correct
24 Correct 15 ms 3196 KB Output is correct
25 Correct 9 ms 3196 KB Output is correct
26 Correct 6 ms 3196 KB Output is correct
27 Correct 5 ms 3196 KB Output is correct
28 Correct 8 ms 3196 KB Output is correct
29 Correct 21 ms 3196 KB Output is correct
30 Correct 7 ms 3196 KB Output is correct
31 Correct 5 ms 3196 KB Output is correct
32 Correct 7 ms 3196 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2680 KB Output is correct
2 Correct 6 ms 2816 KB Output is correct
3 Correct 4 ms 2864 KB Output is correct
4 Correct 4 ms 2864 KB Output is correct
5 Correct 4 ms 2864 KB Output is correct
6 Correct 6 ms 2952 KB Output is correct
7 Correct 4 ms 2952 KB Output is correct
8 Correct 4 ms 2952 KB Output is correct
9 Correct 4 ms 2952 KB Output is correct
10 Correct 5 ms 2952 KB Output is correct
11 Correct 4 ms 2952 KB Output is correct
12 Correct 4 ms 3064 KB Output is correct
13 Correct 5 ms 3064 KB Output is correct
14 Correct 4 ms 3064 KB Output is correct
15 Correct 4 ms 3064 KB Output is correct
16 Correct 5 ms 3064 KB Output is correct
17 Correct 13 ms 3196 KB Output is correct
18 Correct 18 ms 3196 KB Output is correct
19 Correct 13 ms 3196 KB Output is correct
20 Correct 5 ms 3196 KB Output is correct
21 Correct 6 ms 3196 KB Output is correct
22 Correct 8 ms 3196 KB Output is correct
23 Correct 22 ms 3196 KB Output is correct
24 Correct 15 ms 3196 KB Output is correct
25 Correct 9 ms 3196 KB Output is correct
26 Correct 6 ms 3196 KB Output is correct
27 Correct 5 ms 3196 KB Output is correct
28 Correct 8 ms 3196 KB Output is correct
29 Correct 21 ms 3196 KB Output is correct
30 Correct 7 ms 3196 KB Output is correct
31 Correct 5 ms 3196 KB Output is correct
32 Correct 7 ms 3196 KB Output is correct
33 Execution timed out 3053 ms 8044 KB Time limit exceeded
34 Halted 0 ms 0 KB -