This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "Joi.h"
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cmath>
#include <iomanip>
#include <cassert>
#include <functional>
using namespace std;
typedef pair<int, int> P;
#define rep(i, n) for (int i=0; i<(n); i++)
#define all(c) (c).begin(), (c).end()
#define uniq(c) c.erase(unique(all(c)), (c).end())
#define _1 first
#define _2 second
#define pb push_back
#define INF 1145141919
#define MOD 1000000007
struct UF {
int N;
vector<int> U, R;
UF(int N) : N(N), U(N), R(N, 1) {
rep(i, N) U[i] = i;
}
int find(int x) {
if (U[x] == x) return x;
return U[x] = find(U[x]);
}
void unite(int x, int y) {
x = find(x), y = find(y);
if (x == y) return;
if (R[x] < R[y]) swap(x, y);
U[y] = x;
R[x] += R[y];
}
bool same(int x, int y) {
return find(x) == find(y);
}
};
void Joi(int N, int M, int GA[], int GB[], long long X, int T) {
int A[10000] = {};
vector<int> G[10000];
int deg[10000] = {};
set<int> S[10000], leaf[10000];
UF uf(N);
rep(i, M) {
int a = GA[i], b = GB[i];
if (!uf.same(a, b)) {
G[a].pb(b), G[b].pb(a);
uf.unite(a, b);
}
}
rep(i, N) A[i] = -1, sort(all(G[i]));
set<int> used, lf;
queue<P> q;
q.push(P(0, -1));
rep(i, 60) {
int x = q.front()._1, p = q.front()._2; q.pop();
A[x] = i;
used.insert(x);
if (p != -1) deg[x]++, deg[p]++;
for (int t : G[x]) if (t != p) q.push(P(t, x));
}
rep(i, N) if (deg[i] == 1) lf.insert(i);
for (int x : used) S[x] = used, leaf[x] = lf;
auto degree = [&](set<int> &S, int x) {
int d = 0, ch = -1;
for (int t : S) if (binary_search(all(G[t]), x)) d++, ch = t;
return P(d, ch);
};
while (!q.empty()) {
int x = q.front()._1, p = q.front()._2; q.pop();
S[x] = set<int>(S[p]);
S[x].insert(x);
leaf[x] = set<int>(leaf[p]);
if (leaf[x].find(p) != leaf[x].end()) leaf[x].erase(p);
assert(!leaf[x].empty());
int from = *leaf[x].begin();
leaf[x].erase(from);
assert(S[x].find(from) != S[x].end());
S[x].erase(from);
int ch = degree(S[x], from)._2;
int d = degree(S[x], ch)._1;
if (d == 1) leaf[x].insert(ch);
A[x] = A[from];
leaf[x].insert(x);
for (int l : leaf[x]) assert(degree(S[x], l)._1 == 1);
for (int t : G[x]) if (t != p) q.push(P(t, x));
}
rep(i, N) assert(S[i].size() == 60);
rep(i, N) MessageBoard(i, (X>>A[i])&1);
}
#include "Ioi.h"
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cmath>
#include <iomanip>
#include <cassert>
#include <functional>
using namespace std;
typedef pair<int, int> P;
#define rep(i, n) for (int i=0; i<(n); i++)
#define all(c) (c).begin(), (c).end()
#define uniq(c) c.erase(unique(all(c)), (c).end())
#define _1 first
#define _2 second
#define pb push_back
#define INF 1145141919
#define MOD 1000000007
struct UF {
int N;
vector<int> U, R;
UF(int N) : N(N), U(N), R(N, 1) {
rep(i, N) U[i] = i;
}
int find(int x) {
if (U[x] == x) return x;
return U[x] = find(U[x]);
}
void unite(int x, int y) {
x = find(x), y = find(y);
if (x == y) return;
if (R[x] < R[y]) swap(x, y);
U[y] = x;
R[x] += R[y];
}
bool same(int x, int y) {
return find(x) == find(y);
}
};
long long Ioi(int N, int M, int GA[], int GB[], int start, int V, int T) {
int A[10000] = {};
vector<int> G[10000];
int deg[10000] = {};
set<int> S[10000], leaf[10000];
UF uf(N);
rep(i, M) {
int a = GA[i], b = GB[i];
if (!uf.same(a, b)) {
G[a].pb(b), G[b].pb(a);
uf.unite(a, b);
}
}
rep(i, N) A[i] = -1, sort(all(G[i]));
set<int> used, lf;
queue<P> q;
q.push(P(0, -1));
rep(i, 60) {
int x = q.front()._1, p = q.front()._2; q.pop();
A[x] = i;
used.insert(x);
if (p != -1) deg[x]++, deg[p]++;
for (int t : G[x]) if (t != p) q.push(P(t, x));
}
rep(i, N) if (deg[i] == 1) lf.insert(i);
for (int x : used) S[x] = used, leaf[x] = lf;
auto degree = [&](set<int> &S, int x) {
int d = 0, ch = -1;
for (int t : S) if (binary_search(all(G[t]), x)) d++, ch = t;
return P(d, ch);
};
while (!q.empty()) {
int x = q.front()._1, p = q.front()._2; q.pop();
S[x] = set<int>(S[p]);
S[x].insert(x);
leaf[x] = set<int>(leaf[p]);
if (leaf[x].find(p) != leaf[x].end()) leaf[x].erase(p);
assert(!leaf[x].empty());
int from = *leaf[x].begin();
leaf[x].erase(from);
assert(S[x].find(from) != S[x].end());
S[x].erase(from);
int ch = degree(S[x], from)._2;
int d = degree(S[x], ch)._1;
if (d == 1) leaf[x].insert(ch);
A[x] = A[from];
leaf[x].insert(x);
for (int l : leaf[x]) assert(degree(S[x], l)._1 == 1);
for (int t : G[x]) if (t != p) q.push(P(t, x));
}
rep(i, N) assert(S[i].size() == 60);
bool ok[10000] = {};
for (int t : S[start]) ok[t] = true;
long long X = 0;
function<void(int, int, int)> dfs = [&](int x, int p, int v) {
X |= ((long long)v)<<A[x];
for (int t : G[x]) if (t != p && ok[t]) {
dfs(t, x, Move(t));
Move(x);
}
};
dfs(start, -1, V);
return X;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |