CF2023C C+K+S 题解

· · 题解

图是强联通图,因此对于两点 u,v 有路径 u→v 和 v → u。

考虑我们连额外边的时候:

如图上,因为 a,d 之间一定有路径,b,c 之间一定有路径所以此时一定会形成一个环。

怎样快速判断?首先想一下如何快速确定两点之间距离。

(下文中的 |u→v| 为 u 到 v 的路径长度 \bmod{k}。)

  1. 距离 \bmod k 才有用。
  2. 考虑固定一个点(比如 1 号点),一对 u,v:

如图上,可以得出 |u→v| \equiv b - a\pmod{k}。

设 w_p 为 |1→p|。

接下来,可以就可以得出对于第一个图,要求为:

(w_a - w_d) + (w_c - w_b) + 2 \equiv 0 \pmod{k}\\

即:

(w_a - w_b) + (w_c - w_d) + 2 \equiv 0 \pmod{k}

那么也就是说,所有的两个图间建的同向边 u→v 有 w_u - w_v 在模 k 意义下相等。

设 w_u - w_v \equiv t\pmod{k},那么 w_v \equiv w_u - t\pmod{k}。

统计数组 cnt,cnt_i 表示有多少个 w_p = i。

将 t 带入刚才的式子,可以同时算出 w_c - w_d \equiv -2 - t\pmod{k}。

然后对其做哈希匹配。

对于一对需要形如 u→v 匹配的 u,v,需要满足:

w_u - w_v \equiv t \pmod{k}

也就是说在 t 固定的时候对于 w_u 的每一个值(设为 r)有图一 w_u = r 的个数等于图二 w_v = r - t 的个数。

所以应该是把 0 右移 t0 次和 1 做匹配。对于两个方向的边都是这样,因为对称,t1 可以直接推出。

Code:

#include<bits/stdc++.h>
#define ull unsigned long long
#define ul unsigned int
using namespace std;
const int N = 200010;
const ul bs = 13331;
int n, k, tp[2][N], w[2][N], cnt[2][2][N];
vector <int> g[2][N];
bool vis[N];
struct node{
    ull a;ul b;
    bool operator == (const node &s)const{
        return (a == s.a) && (b == s.b);
    }
    node operator * (const ul &v)const{
        return {a * v, b * v};
    }
    node operator * (const node &s)const{
        return {a * s.a, b * s.b};
    }
    node operator + (const node &s)const{
        return {a + s.a, b + s.b};
    }
    node operator + (const ul &v)const{
        return {a + v, b + v};
    }
    node operator - (const node &s)const{
        return {a - s.a, b - s.b};
    }
}h[2][2][N], mi[N];
void bfs(bool t){
    queue <int> q;
    for(int i = 1;i <= n;i++){
        vis[i] = 0;
    }
    vis[1] = 1;
    q.push(1);
    while(!q.empty()){
        int p = q.front();
        q.pop();
        for(auto x : g[t][p]){
            if(vis[x])continue;
            w[t][x] = (w[t][p] + 1) % k;
            q.push(x);vis[x] = 1;
        }
    }
    for(int i = 1;i <= n;i++){
        cnt[t][tp[t][i]][w[t][i]]++;
    }
}
node get_v(bool t0, bool t1, int x){
    node v0 = h[t0][t1][k] - h[t0][t1][k - x] * mi[x];
    node v1 = h[t0][t1][k - x];
    return (v0 * mi[k - x] + v1);
}
void solve(){
    scanf("%d%d", &n, &k);
    for(int j = 0;j <= 1;j++){
        for(int i = 1;i <= n;i++){
            scanf("%d", &tp[j][i]);
            w[j][i] = 0, cnt[j][0][i - 1] = 0, cnt[j][1][i - 1] = 0;
            g[j][i].clear();
        }
        int m;
        scanf("%d", &m);
        for(int i = 1;i <= m;i++){
            int x, y;
            scanf("%d%d", &x, &y);
            g[j][x].push_back(y);
        }
        bfs(j);
    }
    int tmp0 = 0, tmp1 = 0;
    for(int i = 1;i <= n;i++){
        tmp0 += (int)tp[0][i];
        tmp1 += (int)tp[1][i];
    }
    if(tmp0 + tmp1 != n){
        printf("NO\n");
        return;
    }
    if(tmp0 == 0 || tmp0 == n){
        printf("YES\n");
        return;
    }
    for(int j0 = 0;j0 <= 1;j0++){
        for(int j1 = 0;j1 <= 1;j1++){
            for(int i = 1;i <= k;i++){
                ul v = cnt[j0][j1][i - 1];
                h[j0][j1][i] = h[j0][j1][i - 1] * bs + v;
            }
        }
    }
    for(int t0 = 0; t0 < k; t0++){
        int t1 = ((-2 - t0) % k + k) % k;
        // 图2 outgoing -> 图1 incoming
        bool ok0 = (h[1][1][k] == get_v(0, 0, t0));
        // 图1 outgoing -> 图2 incoming
        bool ok1 = (h[0][1][k] == get_v(1, 0, t1));
        if(ok0 && ok1){
            printf("YES\n");
            return;
        }
    }
    printf("NO\n");
}
void init(){
    mi[0] = {1, 1};
    for(int i = 1;i <= N - 5;i++){
        mi[i] = mi[i - 1] * bs;
    }
}
int main(){
    int t;init();
    scanf("%d", &t);
    while(t--){
        solve();
    }
    return 0;
}