NOIP 模拟赛 T3?
若操作囚犯 移动若干步(没有到达终点),然后囚犯 若干步,囚犯 再次移动若干步,发现这个东西非常难做,猜测:
若存在可行方案,则一定存在一种方案,使每个囚犯都只被操作一次,并沿最短路直接走到终点。
证明
若存在囚犯 被分成了多段移动,考虑第一次发生这种情况 :
- 囚犯 走到某个点 ;
- 接下来不能继续走,于是去操作别的囚犯。
设 是 将要走的下一个点,由于 此时停下来了,说明房间 当前被占据。
因为 是第一次多段移动,所以 只有可能是在他的起点或终点
- 在终点,此时无解。
- 在起点,若 会经过 走过的那一段路,由于在树上,无解。否则可以先操作 再操作 。
考虑什么时候会无解,对于两名囚犯不能相遇,尝试将其转化为若干更好做的限制。
- 若囚犯 的路径经过囚犯 的起点,那么囚犯 必须先于囚犯 移动。
- 若囚犯 的路径经过囚犯 的终点,那么囚犯 必须先于囚犯 移动。
若两路径的终点(起点)没有被包含,根据最开始的结论,两人不会互相影响。
当有环时,无解。
暴力枚举路径上的每个点并建图,跑拓扑排序即可做到 。
在树上枚举路径,考虑树链剖分+线段树优化建图。
此处只讲限制 的建图优化,限制 同理。
- 囚犯 向其经过的路径建边。
- 囚犯 的起点向囚犯 建边。
路径是 个区间,线段树访问区间是 ,总时间复杂度 。
这里讲下自环的处理
对于每个囚犯 ,路径 本身一定包含 与 。
若直接把整条路径都加入建图,会产生 的自环。
实现时将一个区间删去一个点,拆成至多两个区间即可。
赛时只拿了暴力分(悲)。
code
#include <bits/stdc++.h>
#define int long long
using namespace std;
namespace IO {
const int LL=1<<20; char bbuf[LL],*pp1,*pp2,OOUT[LL],*pp3=OOUT;
#define Gc() (pp1==pp2&&(pp2=(pp1=bbuf)+fread(bbuf,1,LL,stdin),pp1==pp2)?EOF:*pp1++)
#define Pc(x) (pp3==OOUT+LL&&(fwrite(OOUT,1,LL,stdout),pp3=OOUT),*pp3++=(x))
struct F{~F(){fwrite(OOUT,1,pp3-OOUT,stdout);}}fff;
template<typename T> inline void rd(T& x){x=0ll; char c=Gc(); bool xans=0; while(!isdigit(c))xans^=c=='-',c=Gc();while(isdigit(c))x=x*10+(c^48),c=Gc(); if(xans)x=-x;}inline void rd(char& c){do c=Gc();while(isspace(c));}inline void rd(string& s){s=""; char c; rd(c); do s+=c; while(!isspace(c=Gc())&&~c);}template<typename T,typename... A> inline void rd(T& x,A&... mat){rd(x),rd(mat...);}template<typename T> inline void wt(T x){if(x<0)Pc('-'),x=-x; static int s[45],t=0; do s[++t]=x%10; while(x/=10);while(t)Pc(s[t--]|48);}inline void wt(char c){Pc(c);}inline void wt(const char* s){while(*s)Pc(*s++);}inline void wt(string s){for(char c:s)Pc(c);}template<typename T,typename... A> inline void wt(T x,A... mat){wt(x),Pc(sizeof...(mat)?' ':'\n'),(wt(mat...),0);}
} using namespace IO;
#define fr(x) freopen(#x".in", "r", stdin); freopen(#x".out", "w", stdout);
#define rep(i, l, r) for(int i = (l); i <= (r); ++i)
#define urep(i, r, l) for(int i = (r); i >= (l); --i)
#define ckin(x, y) (1 <= x && 1 <= y && x <= n && y <= m)
// #define id(x, y, i) ((x-1)*m+y+n*m*i)
#define ary array <int, 2>
constexpr int inf = 0x3f3f3f3f3f3f3f3f, mod = 998244353;
#define _ 3000005
int n, m;
int deg[_];
vector <int> e[_];
// ---------------------------------------- CUT ----------------------------------------
int fa[_], dep[_], sz[_], son[_], top[_], dfn[_], idx[_], cnt;
void init(int u)
{
sz[u]++;
for(int v : e[u])
if(v != fa[u]) fa[v] = u, dep[v] = dep[u] + 1, init(v), sz[u] += sz[v], son[u] = sz[son[u]] > sz[v] ? son[u] : v;
}
void cut(int u, int tp)
{
top[u] = tp, dfn[u] = ++cnt, idx[cnt] = u;
if(son[u]) cut(son[u], tp);
for(int v : e[u])
if(v != fa[u] && v != son[u]) cut(v, v);
}
// ---------------------------------------- SEG ----------------------------------------
vector <int> g[_]; // 新图
// 1-m 囚犯 | m+1-m+4*n 出树 | m+4*n+1-m+8*n 入树
int t[_ << 2][2];
#define mid ((l + r) >> 1)
#define ls (p << 1)
#define rs (ls | 1)
int pos[_];
void build(int p = 1, int l = 1, int r = n)
{
if(l == r) return pos[l] = p, void();
g[m+p].emplace_back(m+ls), deg[m+ls]++;
g[m+p].emplace_back(m+rs), deg[m+rs]++;
g[m+4*n+ls].emplace_back(m+4*n+p), deg[m+4*n+p]++;
g[m+4*n+rs].emplace_back(m+4*n+p), deg[m+4*n+p]++;
build(ls, l, mid), build(rs, mid+1, r);
}
// f 表示 [ql, qr] -> f 或 f -> [ql, qr]
void qry(int id, int ql, int qr, bool f, int p = 1, int l = 1, int r = n)
{
if(ql <= l && r <= qr) return f ? (g[id].emplace_back(m+p), deg[m+p]++) : (g[m+4*n+p].emplace_back(id), deg[id]++), void();
if(ql <= mid) qry(id, ql, qr, f, ls, l, mid);
if(qr > mid) qry(id, ql, qr, f, rs, mid+1, r);
}
void add(int id, int ql, int qr, int x, bool f)
{
if(x < ql || qr < x) return qry(id, ql, qr, f);
if(ql <= x-1) qry(id, ql, x-1, f);
if(x+1 <= qr) qry(id, x+1, qr, f);
}
int u[_], v[_];
signed main()
{
int T;
rd(T);
while(T--)
{
rd(n);
rep(i, 1, n) e[i].clear();
rep(i, 1, n) fa[i] = dep[i] = sz[i] = son[i] = top[i] = dfn[i] = idx[i] = 0;
rep(i, 2, n) rd(u[i], v[i]), e[u[i]].emplace_back(v[i]), e[v[i]].emplace_back(u[i]); dep[1] = 1;
init(1), cnt = 0, cut(1, 1);
rd(m);
rep(i, 1, m+8*n) g[i].clear(), deg[i] = 0;build();
rep(i, 1, m)
{
rd(u[i], v[i]);
int x = u[i], y = v[i];
g[m+pos[dfn[x]]].emplace_back(i), deg[i]++;
g[i].emplace_back(m+4*n+pos[dfn[y]]), deg[m+4*n+pos[dfn[y]]]++;
while (top[x] != top[y])
{
if (dep[top[x]] > dep[top[y]]) add(i, dfn[top[x]], dfn[x], dfn[v[i]], 0), add(i, dfn[top[x]], dfn[x], dfn[u[i]], 1), x = fa[top[x]];
else add(i, dfn[top[y]], dfn[y], dfn[v[i]], 0), add(i, dfn[top[y]], dfn[y], dfn[u[i]], 1), y = fa[top[y]];
}
if(dep[x] > dep[y]) add(i, dfn[y], dfn[x], dfn[v[i]], 0), add(i, dfn[y], dfn[x], dfn[u[i]], 1);
else add(i, dfn[x], dfn[y], dfn[v[i]], 0), add(i, dfn[x], dfn[y], dfn[u[i]], 1);
}
queue <int> q;
rep(i, 1, m+8*n) if(!deg[i]) q.push(i);
while(q.size())
{
for(int i : g[q.front()]) if(!--deg[i]) q.push(i);
q.pop();
}
int f = 1;
rep(i, 1, m) f &= !deg[i];
wt(f ? "Yes" : "No", "\n");
}
return 0;
}