题目及数据:

https://www.lanzous.com/i1xdr8h

心路历程

预计分数:$100+ 100 +60 = 260$

实际得分:$100 + 100 + 0 = 200$

我这次检查了三遍绝对没算错!!!

上来看T1,咦?我好像做过这题在仙人掌上的版本。。树上更简单吧。。写+拍 1h,期间拍出了暴力的两个bug。。。

看T2,好难啊,不会做啊 qwq。。然后开始强行套算法。恩。把单调队列套进去发现是可行的。。而且好像还可以无视题目的一些性质。。嘿嘿嘿这题有加强版了

T3更神仙。。想了一个并查集优化暴力的$n^2$算法,然后写挂了。。。自己造的输出跑的贼快,一上评测机就死循环。。

因为昨天晚上打cf特别困,而且自我感觉$260$应该不低了,就睡了一个半小时。。。。。。。。。

T1

听大佬们说是原题啊Orz

我居然不知道 我好像是做过然后忘的一干二净。。

很显然,把路径拆开,判断lca之间的关系即可

#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<vector>
#include<cstring>
#define getchar() ((p1 == p2) && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
using namespace std;
const int MAXN = 1e5 + 10;
char buf[(1 << 21)], *p1 = buf, *p2 = buf;
inline int read() {
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}
int T, N, Q, fa[MAXN], dep[MAXN], top[MAXN], son[MAXN], siz[MAXN];
vector<int> v[MAXN];
void init() {
    for(int i = 1; i <= N; i++) v[i].clear();
    memset(top, 0, sizeof(top));
    memset(son, 0, sizeof(son));
}
void dfs1(int x, int _fa) {
    siz[x] = 1; fa[x] = _fa;
    for(int i = 0; i < v[x].size(); i++) {
        int to = v[x][i];
        if(to == _fa) continue;
        dep[to] = dep[x] + 1;
        dfs1(to, x);
        siz[x] += siz[to];
        if(siz[to] > siz[son[x]]) son[x] = to;
    }
}
void dfs2(int x, int topf) {
    top[x] = topf;
    if(!son[x]) return ;
    dfs2(son[x], topf);
    for(int i = 0; i < v[x].size(); i++) {
        int to = v[x][i];
        if(top[to]) continue;
        dfs2(to, to);
    }
}
int LCA(int x, int y) {
    while(top[x] != top[y]) {
        if(dep[top[x]] < dep[top[y]]) swap(x, y);
        x = fa[top[x]];
    }
    if(dep[x] > dep[y]) swap(x, y);
    return x;
}
bool check(int a, int x, int y) {
    if(dep[x] < dep[y]) swap(x, y);
    if(LCA(a, x) == a && LCA(a, y) == y) return 1;
    else return 0;
}
int main() {
    freopen("railway.in", "r", stdin);
    freopen("railway.out", "w", stdout);
    T = read(); 
    while(T--) {
        N = read(); Q = read();
        init();
        for(int i = 1; i <= N - 1; i++) {
            int x = read(), y = read();
            v[x].push_back(y); v[y].push_back(x);
        }
        dep[1] = 1; dfs1(1, 0);
        dfs2(1, 1);
        while(Q--) {
            int xx1 = read(), yy1 = read(), xx2 = read(), yy2 = read();
            int lca1 = LCA(xx1, yy1), lca2 = LCA(xx2, yy2);
            if(check(lca1, xx2, lca2) || check(lca1, yy2, lca2) || check(lca2, xx1, lca1) || check(lca2, yy1, lca1)) puts("YES");
            else puts("NO");
        }
    }
    return 0;
}
T1

相关文章: