QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#131738#6559. A Tree and Two Edgestriplem5dsWA 2ms6408kbC++233.5kb2023-07-27 22:43:422023-07-27 22:43:43

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-07-27 22:43:43]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:6408kb
  • [2023-07-27 22:43:42]
  • 提交

answer

/// Msaa el 5ra
#pragma GCC optimize("O3")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("avx,avx2,fma")

#include "bits/stdc++.h"

using namespace std;

#define pb push_back
#define F first
#define S second
#define f(i, a, b)  for(int i = a; i < b; i++)
#define all(a)  a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define popCnt(x) (__builtin_popcountll(x))
#define int ll

using ll = long long;
using ii = pair<int, int>;
using ull = unsigned long long;

const int N = 5e4 + 5, LG = 16, MOD = (119 << 23) + 1;
const long double PI = acos(-1);
const long double EPS = 1e-7;
vector<int> adj[N];
bool vis[N];
int pa[N][LG], lvl[N];
vector<ii> bckEdges;

void dfs(int node, int par) {
    f(k, 1, LG) pa[node][k] = pa[pa[node][k - 1]][k - 1];
    vis[node] = true;
    for (auto v: adj[node])
        if (v != par && vis[v]) {
            if (vis[v]) {
                bckEdges.push_back({node, v});
            }
        }
    for (auto v: adj[node])
        if (!vis[v]) {
            lvl[v] = lvl[node] + 1;
            pa[v][0] = node;
            dfs(v, node);
        }
}

int LCA(int u, int v) {
    if (lvl[u] > lvl[v])swap(u, v);
    int k = lvl[v] - lvl[u];
    f(i, 0, LG)if (k >> i & 1)v = pa[v][i];
    if (u == v)return u;
    for (int i = LG - 1; i >= 0; --i)
        if (pa[u][i] != pa[v][i]) {
            u = pa[u][i];
            v = pa[v][i];
        }
    return pa[u][0];
}

int dist(int u, int v) { return lvl[u] + lvl[v] - 2 * lvl[LCA(u, v)]; }

bool onPath(int u, int v, int k) { return dist(u, v) == dist(u, k) + dist(v, k); }

bool checkIntersection(ii a, ii b) {
    int l1 = LCA(a.F, a.S);
    int l2 = LCA(b.F, b.S);
    if (onPath(a.F, a.S, l2))return true;
    if (onPath(b.F, b.S, l1))return true;
    return false;
}

bool calc(vector<ii> path) {
    f(i, 0, path.size())
        f(j, i + 1, path.size())
            if (checkIntersection(path[i], path[j]))
                return false;
    return true;
}

void doWork() {

    int n, q;
    cin >> n >> q;
    f(i, 0, n + 1) {
        int u, v;
        cin >> u >> v;
        adj[u].pb(v);
        adj[v].pb(u);
    }
    dfs(1, 1);
    int a = bckEdges[0].F, b = bckEdges[0].S;
    int c = bckEdges[1].F, d = bckEdges[1].S;
    while (q--) {
        int u, v;
        cin >> u >> v;
        int ans =
                calc(vector<ii>({{u, v}}))
                +
                calc(vector<ii>({{u, c},
                                 {d, v}}))
                + calc(vector<ii>({{u, d},
                                   {c, v}}))
                + calc(vector<ii>({{u, a},
                                   {b, v}}))
                + calc(vector<ii>({{u, b},
                                   {a, v}}))
                + calc(vector<ii>({{u, b},
                                   {a, c},
                                   {d, v}}))
                + calc(vector<ii>({{u, b},
                                   {a, d},
                                   {c, v}}))
                + calc(vector<ii>({{u, a},
                                   {b, c},
                                   {d, v}}))
                + calc(vector<ii>({{u, a},
                                   {b, d},
                                   {c, v}}));
        cout << ans << '\n';
    }
}

int32_t main() {
#ifdef ONLINE_JUDGE
    ios_base::sync_with_stdio(0);
    cin.tie(0);
#endif // ONLINE_JUDGE
    int t = 1;
//    cin >> t;
    while (t--) {
        doWork();
    }
}

详细

Test #1:

score: 0
Wrong Answer
time: 2ms
memory: 6408kb

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
2
3
4

result:

wrong answer 4th lines differ - expected: '3', found: '2'