#include <bits/stdc++.h>
using namespace std;
#define rep(i,l,r) for(int i=(l);i<=(r);++i)
#define per(i,r,l) for(int i=(r);i>=(l);--i)
const int NR=2e6+5,inf=1e9+7;
int t,n,m,a[NR],b[NR],r[NR],dfn[NR],dfc=0,cnt[2][NR],col[NR];
int du[NR];
int pre[NR<<1],to[NR<<1],id[NR<<1],h[NR],ecnt=0;
vector<int>E[NR];
void newedge(int x,int y,int i) {
	to[++ecnt]=y;
	id[ecnt]=i;
	E[x].push_back(i);
	++du[x];
	pre[ecnt]=h[x];
	h[x]=ecnt;
}
void Dfs1(int u,vector<int>&D,int fa=0) {
	D.push_back(u);
	dfn[u]=dfc;
	for(int i=h[u];i;i=pre[i]) {
		int v=to[i];
		if(dfn[v]==-1)continue;
		if(!dfn[v]) Dfs1(v,D,u);
	}
}
bool tg[NR],y[NR];
void Caozuo(int t) {
	tg[t]=1;
	--cnt[col[t]][r[t]];
	--cnt[col[t+n]][r[t+n]];
	col[t]^=1;
	col[t+n]^=1;
	++cnt[col[t]][r[t]];
	++cnt[col[t+n]][r[t+n]];
	y[t]^=1;
}
void tools(int e) {
	dfn[e]=-1;
	if(cnt[0][e]&&cnt[1][e])return;
	if(!tg[E[e][0]]) Caozuo(E[e][0]);
	else Caozuo(E[e][1]);
}
bool inlegal(int x) {
	return (!cnt[0][x])||(!cnt[1][x]);
}
int lp[NR],lq[NR];
void Dfs2(int u) { //有自环 
	lp[u]=1;
	for(int i=h[u];i;i=pre[i]) {
		int v=to[i],q=id[i];
		if(dfn[v]==-1)continue;
		if(!lp[v]){
			Dfs2(v);
			if(inlegal(v)) 
				Caozuo(q);
		}
	}
}
int ins[NR];
void Dfs3(int u,int &root,int &Vx,int fa=0){
	lp[u]=1;ins[u]=1;
	for(int i=h[u];i;i=pre[i]) {
		int v=to[i],q=id[i];
		if(v==u)continue;
		if(dfn[v]==-1)continue;
		if(!lp[v]) {
			Dfs3(v,root,Vx,u);
		} else if(ins[v]){
			root=u;Vx=q;
		}
	}ins[u]=0;
}
void Dfs4(int u,int root,int Vx) { //调整 V 子树 
lq[u]=1;
	for(int i=h[u];i;i=pre[i]) {
		int v=to[i],q=id[i];
		if(dfn[v]==-1)continue;
		if(u==root&&q==Vx) continue;
		if(v==u)continue;
		if(lq[v])continue;
		Dfs4(v,root,Vx);
		if(inlegal(v))Caozuo(q);
	}
	if(u==root&&inlegal(u)) {
		Caozuo(Vx);
	}
}
int bjd=0,dth=inf,Fa[NR>>1],Fab[NR>>1];
void Dfs_Tree(int u,int dep=0) {
	lq[u]=1;
	for(int i=h[u];i;i=pre[i]) {
		int v=to[i],q=id[i];
		if(dfn[v]==-1)continue;
		if(u==v)continue;
		if(lq[v])continue;
		Fa[v]=u;Fab[v]=q;
		Dfs_Tree(v,1+dep);
		if(inlegal(v))Caozuo(q);
		else {
			Caozuo(q);
			if(!inlegal(v)) {
				if(dep<dth)bjd=v,dth=dep;
			}
			Caozuo(q);
		}
	}
}
void work() {
	cin>>n>>m;
	rep(i,1,m)
		h[i]=0,E[i].clear();
	rep(i,1,m)
		Fa[i]=Fab[i]=dfn[i]=0;
	rep(i,1,m)
		du[i]=0,cnt[0][i]=cnt[1][i]=0;
	ecnt=0;dfc=0;
	rep(i,1,2*n)
		cin>>a[i],col[i]=a[i],dfn[i]=tg[i]=0;
	rep(i,1,n)
		cin>>b[i];
	rep(i,1,2*n)
		cin>>r[i];
	rep(i,1,n) 
		y[i]=0;
	rep(i,1,2*n) {
		++cnt[a[i]][r[i]];
	}
	rep(i,1,n)
		if(b[i])newedge(r[i],r[i+n],i),newedge(r[i+n],r[i],i);/*
	rep(i,1,m) {
		if(du[i]==2) {
			tools(i);
		}
	}*/
	rep(i,1,m) {
		lp[i]=lq[i]=0;
	}
	
	rep(i,1,m) {
		vector<int>C;
		if(!dfn[i]){
			bool flag=1;
			for(int j=h[i];j;j=pre[j]) {
				if(dfn[to[j]]!=-1){
					flag=0;break;
				}
			}
			if(flag){
				if(inlegal(i)&&du[i]){
					cout<<"No\n";return;
				}
				continue;
			}
			dfc=i; Dfs1(i,C);
			int GG=0,Banh=0;
			for(int I:C) {
				for(int l=h[I];l;l=pre[l]) {
					int K=to[l];
					if(I==K) {

						GG=K;Banh=id[l];break;
					}
				}
				if(GG)break;
			}
			if(GG){
				Dfs2(GG);
				if(inlegal(GG)) {
					Caozuo(Banh);
				}
			} else {
				int root=0,Vx=0;
				Dfs3(i,root,Vx);
				if(root) { //有环 
					Dfs4(root,root,Vx);
				} else { //一棵树 
				bjd=0,dth=inf;
				root=i;
					Dfs_Tree(root);
					if(inlegal(root)&&!bjd) {
						cout<<"No"<<'\n';return;
					} else if(inlegal(root)){
						int h=bjd;
						while(h^root) {
							Caozuo(Fab[h]);
							h=Fa[h];
						}
					}
				}
			}
		}
	}
	rep(i,1,m){
		if(inlegal(i)) {
			cout<<"No\n";return;
		}
	}
	cout<<"Yes\n";
	rep(i,1,n)cout<<y[i]<<' ';
	cout<<'\n';
}
int main() {	
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	
	freopen("light.in","r",stdin);
	freopen("light.out","w",stdout);
	cin>>t;
	while(t--) 
		work();
	return 0;
}



1 条评论

  • @ 2026-10-4 17:01:37
    #include <bits/stdc++.h>
    using namespace std;
    #define rep(i,l,r) for(int i=(l);i<=(r);++i)
    #define per(i,r,l) for(int i=(r);i>=(l);--i)
    
    const int NR = 2e6 + 5, inf = 1e9 + 7;
    int t, n, m, a[NR], b[NR], r[NR], dfn[NR], dfc = 0, cnt[2][NR], col[NR];
    int du[NR];
    int pre[NR << 1], to[NR << 1], id[NR << 1], h[NR], ecnt = 0;
    vector<int> E[NR];
    
    void newedge(int x, int y, int i) {
        to[++ecnt] = y;
        id[ecnt] = i;
        E[x].push_back(i);
        ++du[x];
        pre[ecnt] = h[x];
        h[x] = ecnt;
    }
    
    void Dfs1(int u, vector<int>& D, int fa = 0) {
        D.push_back(u);
        dfn[u] = dfc;
        for (int i = h[u]; i; i = pre[i]) {
            int v = to[i];
            if (dfn[v] == -1) continue;
            if (!dfn[v]) Dfs1(v, D, u);
        }
    }
    
    bool tg[NR], y[NR];
    
    void Caozuo(int t) {
        tg[t] = 1;
        --cnt[col[t]][r[t]];
        --cnt[col[t + n]][r[t + n]];
        col[t] ^= 1;
        col[t + n] ^= 1;
        ++cnt[col[t]][r[t]];
        ++cnt[col[t + n]][r[t + n]];
        y[t] ^= 1;
    }
    
    bool inlegal(int x) {
        return (!cnt[0][x]) || (!cnt[1][x]);
    }
    
    int lp[NR], lq[NR];
    
    void Dfs2(int u) { // 有自环
        lp[u] = 1;
        for (int i = h[u]; i; i = pre[i]) {
            int v = to[i], q = id[i];
            if (dfn[v] == -1) continue;
            if (!lp[v]) {
                Dfs2(v);
                if (inlegal(v)) 
                    Caozuo(q);
            }
        }
    }
    
    int ins[NR];
    
    void Dfs3(int u, int &root, int &Vx, int fa = 0) {
        lp[u] = 1; ins[u] = 1;
        for (int i = h[u]; i; i = pre[i]) {
            int v = to[i], q = id[i];
            if (v == u) continue;
            if (dfn[v] == -1) continue;
            if (!lp[v]) {
                Dfs3(v, root, Vx, u);
            } else if (ins[v]) {
                // 修改:以祖先 v 为根,这样整个环都在生成树中
                root = v; Vx = q;
            }
        }
        ins[u] = 0;
    }
    
    void Dfs4(int u, int root, int Vx) { // 调整环子树
        lq[u] = 1;
        for (int i = h[u]; i; i = pre[i]) {
            int v = to[i], q = id[i];
            if (dfn[v] == -1) continue;
            if (u == root && q == Vx) continue;
            if (v == u) continue;
            if (lq[v]) continue;
            Dfs4(v, root, Vx);
            if (inlegal(v)) Caozuo(q);
        }
        if (u == root && inlegal(u)) {
            Caozuo(Vx);
        }
    }
    
    int bjd = 0, dth = inf, Fa[NR >> 1], Fab[NR >> 1];
    
    void Dfs_Tree(int u, int dep = 0) {
        lq[u] = 1;
        for (int i = h[u]; i; i = pre[i]) {
            int v = to[i], q = id[i];
            if (dfn[v] == -1) continue;
            if (u == v) continue;
            if (lq[v]) continue;
            Fa[v] = u; Fab[v] = q;
            Dfs_Tree(v, dep + 1);
            if (inlegal(v)) Caozuo(q);
            else {
                Caozuo(q);
                if (!inlegal(v)) {
                    // 修改:比较子节点 v 的深度 dep+1
                    if (dep + 1 < dth) bjd = v, dth = dep + 1;
                }
                Caozuo(q);
            }
        }
    }
    
    void work() {
        cin >> n >> m;
        rep(i, 1, m) h[i] = 0, E[i].clear();
        rep(i, 1, m) Fa[i] = Fab[i] = dfn[i] = 0;
        rep(i, 1, m) du[i] = 0, cnt[0][i] = cnt[1][i] = 0;
        ecnt = 0; dfc = 0;
        rep(i, 1, 2 * n) cin >> a[i], col[i] = a[i], dfn[i] = tg[i] = 0;
        rep(i, 1, n) cin >> b[i];
        rep(i, 1, 2 * n) cin >> r[i];
        rep(i, 1, n) y[i] = 0;
        rep(i, 1, 2 * n) ++cnt[a[i]][r[i]];
    
        rep(i, 1, n)
            if (b[i]) newedge(r[i], r[i + n], i), newedge(r[i + n], r[i], i);
    
        rep(i, 1, m) lp[i] = lq[i] = 0;
    
        rep(i, 1, m) {
            vector<int> C;
            if (!dfn[i]) {
                bool flag = 1;
                for (int j = h[i]; j; j = pre[j]) {
                    if (dfn[to[j]] != -1) {
                        flag = 0; break;
                    }
                }
                if (flag) {
                    if (inlegal(i) && du[i]) {
                        cout << "No\n"; return;
                    }
                    continue;
                }
                dfc = i; Dfs1(i, C);
                int GG = 0, Banh = 0;
                for (int I : C) {
                    for (int l = h[I]; l; l = pre[l]) {
                        int K = to[l];
                        if (I == K) {
                            GG = K; Banh = id[l]; break;
                        }
                    }
                    if (GG) break;
                }
                if (GG) {
                    Dfs2(GG);
                    if (inlegal(GG)) {
                        Caozuo(Banh);
                        // 修改:操作自环后再次检查
                        if (inlegal(GG)) {
                            cout << "No\n"; return;
                        }
                    }
                } else {
                    int root = 0, Vx = 0;
                    Dfs3(i, root, Vx);
                    if (root) { // 有环
                        Dfs4(root, root, Vx);
                        // 修改:检查环边另一端点是否合法
                        int other = (r[Vx] == root) ? r[Vx + n] : r[Vx];
                        if (inlegal(other)) {
                            cout << "No\n"; return;
                        }
                    } else { // 一棵树
                        bjd = 0, dth = inf;
                        root = i;
                        Dfs_Tree(root);
                        if (inlegal(root) && !bjd) {
                            cout << "No\n"; return;
                        } else if (inlegal(root)) {
                            int h = bjd;
                            while (h ^ root) {
                                Caozuo(Fab[h]);
                                h = Fa[h];
                            }
                        }
                    }
                }
            }
        }
        rep(i, 1, m) {
            if (inlegal(i)) {
                cout << "No\n"; return;
            }
        }
        cout << "Yes\n";
        rep(i, 1, n) cout << y[i] << ' ';
        cout << '\n';
    }
    
    int main() {
        ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
        freopen("light.in", "r", stdin);
        freopen("light.out", "w", stdout);
        cin >> t;
        while (t--) work();
        return 0;
    }
    
  • 1

信息

ID
63
时间
1000ms
内存
256MiB
难度
9
标签
递交数
215
已通过
9
上传者