We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
define foreach(i, a) for (__typeof((a).begin()) i = (a).begin(); i != (a).end(); ++i)
define len(a) ((int) (a).size())
ifdef CUTEBMAING
define eprintf(...) fprintf(stderr, VA_ARGS)
else
define eprintf(...) 42
endif
using namespace std;
typedef long long int64;
typedef long double ld;
typedef unsigned long long lint;
const int inf = (1 << 30) - 1;
const int64 linf = (1ll << 62) - 1;
const int N = 1e5 + 100;
int n, r;
vector g[N];
bool used[N];
void dfsMark(int v, int p = -1, int d = 0) {
if (d > r) {
return;
}
used[v] = true;
for (int to : g[v]) {
if (p != to) {
dfsMark(to, v, d + 1);
}
}
}
int way[N], wayLen = 0;
int dist[N];
void findDist(int v, int p = -1, int d = 0) {
if (!used[v]) {
return ;
}
dist[v] = d;
for (int to : g[v]) {
if (to != p) {
findDist(to, v, d + 1);
}
}
}
bool findWay(int v, int x, int p = -1) {
if (!used[v]) {
return false;
}
way[wayLen++] = v;
if (v == x) {
return true;
}
for (int to : g[v]) {
if (p != to && findWay(to, x, v)) {
return true;
}
}
wayLen--;
return false;
}
inline int64 myrand() {
int64 res = 0;
for (int i = 0; i < 4; i++) {
res <<= 16;
res += rand();
}
return res;
}
inline int64 dfsHash(int v, int p = -1) {
vector go;
for (int to : g[v]) {
if (to != p && used[to]) {
go.pb(dfsHash(to, v));
}
}
sort(all(go));
int64 res = 423929593294391LL;
for (int i = 0; i < len(go); i++) {
res ^= go[i] * rnd[i];
}
return res;
}
Jenny's Subtrees
You are viewing a single comment's thread. Return to all comments →
Proper Solution in C++
include
include
include
include
include
include
include
include
include
include
include
include
include
include
include
include
include
define pb push_back
define pbk pop_back
define mp make_pair
define fs first
define sc second
define all(x) (x).begin(), (x).end()
define foreach(i, a) for (__typeof((a).begin()) i = (a).begin(); i != (a).end(); ++i)
define len(a) ((int) (a).size())
ifdef CUTEBMAING
define eprintf(...) fprintf(stderr, VA_ARGS)
else
define eprintf(...) 42
endif
using namespace std;
typedef long long int64; typedef long double ld; typedef unsigned long long lint;
const int inf = (1 << 30) - 1; const int64 linf = (1ll << 62) - 1; const int N = 1e5 + 100;
int n, r; vector g[N]; bool used[N];
void dfsMark(int v, int p = -1, int d = 0) { if (d > r) { return; } used[v] = true; for (int to : g[v]) { if (p != to) { dfsMark(to, v, d + 1); } } }
int way[N], wayLen = 0; int dist[N];
void findDist(int v, int p = -1, int d = 0) { if (!used[v]) { return ; } dist[v] = d; for (int to : g[v]) { if (to != p) { findDist(to, v, d + 1); } } }
bool findWay(int v, int x, int p = -1) { if (!used[v]) { return false; } way[wayLen++] = v; if (v == x) { return true; } for (int to : g[v]) { if (p != to && findWay(to, x, v)) { return true; } } wayLen--; return false; }
vector findCenters(int v) { fill_n(dist, n, -inf); findDist(v); v = max_element(dist, dist + n) - dist; findDist(v); int to = max_element(dist, dist + n) - dist; wayLen = 0; assert(findWay(v, to)); if (wayLen % 2 == 0) { return { way[wayLen / 2 - 1], way[wayLen / 2] }; } return { way[wayLen / 2] }; }
int64 rnd[N];
inline int64 myrand() { int64 res = 0; for (int i = 0; i < 4; i++) { res <<= 16; res += rand(); } return res; }
inline int64 dfsHash(int v, int p = -1) { vector go; for (int to : g[v]) { if (to != p && used[to]) { go.pb(dfsHash(to, v)); } } sort(all(go)); int64 res = 423929593294391LL; for (int i = 0; i < len(go); i++) { res ^= go[i] * rnd[i]; } return res; }
int main() {
ifdef XCODE
endif
}