QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#111070 | #6559. A Tree and Two Edges | KhNURE_KIVI# | WA | 80ms | 12032kb | C++20 | 5.1kb | 2023-06-05 19:04:31 | 2023-06-05 19:04:37 |
Judging History
answer
//#pragma GCC optimize("Ofast", "unroll-loops")
//#pragma GCC target("sse", "sse2", "sse3", "ssse3", "sse4")
#include <bits/stdc++.h>
#define all(a) a.begin(),a.end()
#define len(a) (int)(a.size())
#define mp make_pair
#define pb push_back
#define fir first
#define sec second
#define fi first
#define se second
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;
template<typename T>
inline bool umin(T &a, T b) {
if (b < a) {
a = b;
return true;
}
return false;
}
template<typename T>
inline bool umax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
#ifdef LOCAL
#define D for (bool _FLAG = true; _FLAG; _FLAG = false)
#define LOG(...) print(#__VA_ARGS__" ::", __VA_ARGS__) << endl
template <class ...Ts> auto &print(Ts ...ts) { return ((cerr << ts << " "), ...); }
#else
#define D while (false)
#define LOG(...)
#endif // LOCAL
const int max_n = 1e5 + 11, inf = 1000111222;
struct dsu {
public:
int n;
vector <int> p, cnt;
inline void make_set (int v) {
p[v] = v;
}
dsu (int n) : n(n) {
p.resize(n);
cnt.assign(n, 1);
for (int i = 0; i < n; i++) {
make_set(i);
}
}
inline int get (int v) {
if (p[v] == v) return v;
return p[v] = get(p[v]); /// compressing path
}
inline bool unite (int a, int b) {
a = get(a);
b = get(b);
if (a == b) return false;
if (cnt[a] > cnt[b]) {
swap(a, b);
}
p[a] = b;
cnt[b] += cnt[a];
return true;
}
};
int L = 0, n, timer = 0;
vector <vector <int> > edge, up;
vector <int> tin, tout;
inline void dfs_lca (int v, int p = -1) {
tin[v] = timer++;
up[v].resize(L + 1);
up[v][0] = p == -1 ? v : p;
for (int i = 1; i <= L; i++) {
up[v][i] = up[up[v][i - 1]][i - 1];
}
for (int to : edge[v]) {
if (to == p) continue;
dfs_lca(to, v);
}
tout[v] = timer;
}
inline bool upper (int a, int b) {
return tin[a] <= tin[b] && tout[b] <= tout[a];
}
inline int lca (int a, int b) {
if (upper(a, b)) return a;
if (upper(b, a)) return b;
for (int i = L; i >= 0; i--) {
if (!upper(up[a][i], b)) {
a = up[a][i];
}
}
return up[a][0];
}
inline void set_L () {
while ((1 << L) <= n) ++L;
}
inline void lca_prepare (int root = 0) {
set_L();
dfs_lca(root);
}
inline bool cmp (int a, int b) {
return tin[a] < tin[b];
}
vector <vector <int> > e_vt;
vector <int> vv;
inline int virt_tree (vector <int> ver) {
/// lca_prepare() at first
sort(all(ver), cmp);
int k = len(ver);
for (int i = 0; i + 1 < k; i++) {
ver.pb(lca(ver[i], ver[i + 1]));
}
sort(all(ver), cmp);
ver.erase(unique(all(ver)), ver.end());
vector <int> q;
q.pb(ver[0]);
for (int i = 1; i < len(ver); i++) {
int u = ver[i];
while (len(q) >= 2 && !upper(q.back(), u)) {
e_vt[q[len(q) - 2]].pb(q.back());
e_vt[q.back()].pb(q[len(q) - 2]);
q.pop_back();
}
q.pb(u);
}
while (len(q) >= 2) {
e_vt[q[len(q) - 2]].pb(q.back());
e_vt[q.back()].pb(q[len(q) - 2]);
q.pop_back();
}
vv = ver;
return q[0];
}
bool used[max_n] = {};
inline int go (int a, vector <pii> &have, int b) {
if (a == b) {
return 1;
}
used[a] = 1;
int cnt = 0;
for (int to : e_vt[a]) {
if (!used[to]) {
cnt += go(to, have, b);
}
}
for (auto &j : have) {
if (j.first == a && !used[j.second]) {
cnt += go(j.second, have, b);
}
if (j.second == a && !used[j.first]) {
cnt += go(j.first, have, b);
}
}
used[a] = 0;
return cnt;
}
int main() {
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, q;
cin >> n >> q;
int m = n + 1;
edge.resize(n);
up.resize(n);
tin.resize(n);
tout.resize(n);
e_vt.resize(n);
vector <pii> have;
set <int> good;
dsu t(n);
for (int i = 0, a, b; i < m; i++) {
cin >> a >> b;
--a, --b;
if (!t.unite(a, b)) {
have.pb({a, b});
good.insert(a);
good.insert(b);
}
else {
edge[a].pb(b);
edge[b].pb(a);
}
}
lca_prepare();
for (int i = 0, a, b; i < q; i++) {
vv.clear();
cin >> a >> b;
--a, --b;
vector <int> c(all(good));
c.pb(a);
c.pb(b);
sort(all(c));
c.erase(unique(all(c)), c.end());
int root = virt_tree(c);
int ans = go(a, have, b);
for (auto &j : vv) {
e_vt[j].clear();
}
cout << ans << '\n';
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 3520kb
input:
4 6 1 2 1 3 1 4 2 3 2 4 1 2 1 3 1 4 2 3 2 4 3 4
output:
3 3 3 3 3 4
result:
ok 6 lines
Test #2:
score: 0
Accepted
time: 0ms
memory: 3524kb
input:
6 4 1 2 1 3 1 6 2 3 3 4 3 5 4 5 1 2 1 3 1 4 1 6
output:
2 2 4 1
result:
ok 4 lines
Test #3:
score: -100
Wrong Answer
time: 80ms
memory: 12032kb
input:
50000 50000 11561 23122 14261 28523 24407 48814 17947 35894 14803 29607 19557 39115 12415 24830 9583 19167 18397 36794 439 878 18040 36080 17150 34300 7922 15845 18938 37877 18625 37250 6459 12919 9818 19636 3579 7158 21323 42646 23882 47764 13603 27207 8353 16707 15907 31814 20935 41871 11686 23372...
output:
2 2 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 2 1 2 1 2 2 1 1 1 2 1 2 1 2 2 1 1 1 1 1 1 2 2 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 2 2 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 2 1 2 1 2 1 1 1 1 1 1 2 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 2 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 2 ...
result:
wrong answer 1st lines differ - expected: '4', found: '2'