QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#112442 | #6559. A Tree and Two Edges | ckiseki# | WA | 26ms | 8796kb | C++20 | 3.6kb | 2023-06-11 19:01:43 | 2023-06-11 19:01:47 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#ifdef CKISEKI
#define safe cerr << __PRETTY_FUNCTION__ << " line " << __LINE__ << " safe\n"
#define debug(a...) debug_(#a, a)
#define orange(a...) orange_(#a, a)
template <typename ...T>
void debug_(const char *s, T ...a) {
cerr << "\e[1;32m(" << s << ") = (";
int cnt = sizeof...(T);
(..., (cerr << a << (--cnt ? ", " : ")\e[0m\n")));
}
template <typename I>
void orange_(const char *s, I L, I R) {
cerr << "\e[1;32m[ " << s << " ] = [ ";
for (int f = 0; L != R; ++L)
cerr << (f++ ? ", " : "") << *L;
cerr << " ]\e[0m\n";
}
#else
#define safe ((void)0)
#define debug(...) ((void)0)
#define orange(...) ((void)0)
#endif
namespace {
constexpr int maxn = 50000 + 5;
int who[maxn];
bool din[maxn];
int covc = 1;
vector<int> cov[maxn];
vector<int> g[maxn];
bool instk[maxn], vis[maxn];
vector<int> stk;
void dfs(int u) {
vis[u] = true;
stk.push_back(u);
instk[u] = true;
for (int v : g[u]) {
if (stk.size() >= 2 and v == stk[stk.size() - 2])
continue;
if (instk[v]) {
for (auto it = stk.rbegin(); it != stk.rend(); ++it) {
cov[*it].push_back(covc);
if (*it == v) break;
}
covc += 1;
} else if (not vis[v]) {
dfs(v);
}
}
instk[u] = false;
stk.pop_back();
vis[u] = true;
}
void dfs_who(int u, int f) {
for (int v : g[u]) {
if (v == f) continue;
if (cov[v].size() == 0) {
who[v] = who[u];
dfs_who(v, u);
}
}
}
void solve1(int n, int q) {
for (int i = 1; i <= n; ++i) if (cov[i].size() == 2) {
int cnt = 0;
for (int j : g[i])
cnt += cov[j].size() == 2;
if (cnt == 1) din[i] = true;
}
while (q--) {
int u, v;
cin >> u >> v;
if (who[u] == who[v]) {
cout << 1 << '\n';
} else {
u = who[u], v = who[v];
if (din[u] or din[v]) {
cout << 3 << '\n';
} else if (cov[u] == cov[v]) {
cout << 3 << '\n';
} else {
cout << 4 << '\n';
}
}
}
}
vector<int> pseudo[maxn];
void dfs_pseudo(int u, int f, int x) {
pseudo[u].push_back(x);
for (int v : g[u]) {
if (v == f) continue;
if (cov[v].size() == 0)
dfs_pseudo(v, u, x);
}
}
void solve2(int n, int q) {
for (int i = 1; i <= n; ++i) {
if (cov[i].size() >= 1) {
dfs_pseudo(i, i, i);
}
}
for (int i = 1; i <= n; ++i) {
if (pseudo[i].size() == 2) {
cov[n + 1] = pseudo[i];
who[i] = n + 1;
}
}
while (q--) {
int u, v;
cin >> u >> v;
if (who[u] == who[v]) {
cout << 1 << '\n';
} else {
u = who[u], v = who[v];
if (cov[u].size() == 2 or cov[v].size() == 2) {
cout << 2 << '\n';
} else if (cov[u] == cov[v]) {
cout << 2 << '\n';
} else {
cout << 4 << '\n';
}
}
}
}
} // namespace
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int n, q;
cin >> n >> q;
for (int i = 0; i < n + 1; ++i) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
dfs(1);
int cnt = 0;
for (int i = 1; i <= n; ++i) {
if (cov[i].size() == 2) cnt++;
if (cov[i].size() >= 1) {
who[i] = i;
dfs_who(i, i);
}
}
if (cnt >= 2) solve1(n, q);
else solve2(n, q);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 6908kb
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: 3ms
memory: 7036kb
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: 26ms
memory: 8796kb
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:
4 3 3 4 4 3 1 3 4 1 3 4 3 4 3 3 1 3 3 3 4 4 4 3 3 3 4 3 3 3 1 3 3 3 3 4 4 4 4 3 4 3 3 3 4 3 4 4 4 4 3 4 4 4 4 3 3 3 4 4 4 4 4 4 3 4 3 4 1 4 1 1 4 3 3 4 3 3 1 4 3 3 4 4 3 3 4 4 4 3 4 3 4 4 4 4 4 3 4 3 3 3 1 3 4 4 3 4 3 4 3 3 4 1 4 3 3 3 4 4 4 4 3 3 3 3 3 4 4 4 4 3 3 4 3 4 4 3 3 4 4 4 1 3 3 3 3 4 4 3 ...
result:
wrong answer 20190th lines differ - expected: '3', found: '4'