QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#62189#1869. Power Station of ArthinataAC ✓520ms80584kbC++145.8kb2022-11-17 16:51:282022-11-17 16:56:34

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-11-17 16:56:34]
  • 评测
  • 测评结果:AC
  • 用时:520ms
  • 内存:80584kb
  • [2022-11-17 16:51:28]
  • 提交

answer

#include <cstring>
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <ctime>
#include <cstdlib>
#include <set> 
#include <cassert>
#include <string>
#include <map>
#include <unordered_map>
#include <random>
#include <string>
#include <vector>
using namespace std;

#define ll long long
#define llu unsigned long long
#define mem(a, b) memset(a, b, sizeof(a))
#define el printf("\n")
#define pii pair<int, int>
#define af assert(0)
#define _(x) cout<<#x<<" = "<<x<<"\n"
#define __(x, y) cout<<#x<<" = "<<x<<", "<<#y<<" = "<<y<<"\n"
#define rep(i, l, r) for(int i = (l); i <= (r); ++i)
#define dep(i, l, r) for(int i = (l); i <= (r); ++i)

const int N = 1e6 + 10, M = 2e6 + 10;
const char Ans[20][20] = {"NO", "YES"};
int ver[M], nex[M], head[N], dcc_col[N], e_col[N], b[M * 2], col[2][N], val[2][N];
int book[2][4][2][M], sta[M], sta2[M];
char in[M];
vector<int> dcc[N];
int n, m, pos, cnt, ok, top, top2;

void init(){
    pos = cnt = ok = top = top2 = 0;
    for(int i = 0; i <= n; ++i){
        head[i] = dcc_col[i] = e_col[i] = 0;
        dcc[i].clear();
    }
}

void add(int x, int y){
    ver[++pos] = y, nex[pos] = head[x], head[x] = pos;
}

int find(int x){
    int l = 1, r = cnt;
    while(l < r){
        int mid = l + r >> 1;
        if(b[mid] >= x) r = mid;
        else l = mid + 1;
    }
    return l;
}

void dfs(int x, int c){
    dcc[c].push_back(x);
    dcc_col[x] = c;
    for(int i = head[x]; i; i = nex[i]){
        int y = ver[i];
        if(!dcc_col[y]){
            dfs(y, c);
        }
    }
}

void e_dfs(int x, int limit, int c){
    if(!ok) return;
    if(dcc_col[x] != limit) return;
    e_col[x] = c;
    for(int i = head[x]; i; i = nex[i]){
        int y = ver[i];
        if(e_col[y] == c){
            ok = 0;
            return;
        }
        else if(!e_col[y]){
            e_dfs(y, limit, 3 - c);
        }
    }
}

int check(int x){
    ok = 1;
    e_dfs(dcc[x][0], x, 1);
    if(!ok){ // 不是二分图
        top = top2 = 0;
        int A[5] = {0}, B[5] = {0};
        for(int i : dcc[x]){
            sta[++top] = val[0][i];
            // ++A[col[0][i]], 
            ++B[col[0][i]];
        }
        for(int i : dcc[x]){
            sta2[++top2] = val[1][i];
            ++A[col[1][i]];
        }
        sort(sta + 1, sta + 1 + top), sort(sta2 + 1, sta2 + 1 + top2);
        if(top != top2){
            return 0;
        }
        for(int i = 1; i <= top; ++i){
            if(sta[i] != sta2[i]){
                return 0;
            }
        }
        if(((1 & A[1]) ^ (1 & B[1])) == 0 && ((1 & A[0]) ^ (1 & B[0])) == 0){
            return 1;
        }
        return 0;
    }
    else{
        // printf("kfhd");
        top = 0;
        for(int k = 0; k < 2; ++k){
            for(int i : dcc[x]){
                sta[++top] = val[k][i];
                ++book[k][e_col[i]][col[k][i]][val[k][i]];
            }
        }
        // printf("sta : ");
        // for(int i = 1; i <= top; ++i){
        //     printf("%d ", sta[i]);
        // }
        // printf("\n");
        int flag = 1;
        for(int i = 1; i <= top; ++i){
            int now = sta[i];
            if(book[0][1][1][now] + book[0][2][0][now] != book[1][1][1][now] + book[1][2][0][now] ||
            book[0][1][0][now] + book[0][2][1][now] != book[1][1][0][now] + book[1][2][1][now]){
                flag = 0;
                break;
                // printf("129-1");
                // printf("%d %d %d %d\n", book[0][1][1][now], book[0][2][0][now], book[1][1][1][now], book[1][2][0][now]);
            }
            if(book[0][1][0][now] + book[0][1][1][now] + book[0][2][0][now] + book[0][2][1][now] != 
            book[1][1][0][now] + book[1][1][1][now] + book[1][2][0][now] + book[1][2][1][now]){
                flag = 0;
                break;
                // printf("133-1");
            }
        }

        for(int k = 0; k < 2; ++k){
            for(int i : dcc[x]){
                --book[k][e_col[i]][col[k][i]][val[k][i]];
            }
        }
        return flag;
    }
}

int main(){
    // freopen("1.in", "r", stdin);
    int t; scanf("%d", &t);
    for(int id = 1; id <= t; ++id){
        scanf("%d %d", &n, &m);
        init();
        for(int i = 1; i <= m; ++i){
            int x, y; scanf("%d %d", &x, &y);
            add(x, y), add(y, x);
        }
        for(int k = 0; k < 2; ++k){
            for(int i = 1; i <= n; ++i){
                scanf("%d", &val[k][i]);
                b[++cnt] = val[k][i];
            }
            scanf("%s", in + 1);
            for(int i = 1; i <= n; ++i){
                col[k][i] = (in[i] == 'B');
            }
        }

        sort(b + 1, b + 1 + cnt);
        int t = cnt; cnt = 0;
        for(int i = 1; i <= t; ++i){
            if(!cnt || b[i] != b[cnt]) b[++cnt] = b[i];
        }
        for(int k = 0; k < 2; ++k){
            for(int i = 1; i <= n; ++i){
                val[k][i] = find(val[k][i]);
            }
        }
        #ifdef DEBUG
            for(int i = 1; i <= n; ++i){
                printf("%d %d\n", val[0][i], col[0][i]);
            }
            el;
            for(int i = 1; i <= n; ++i){
                printf("%d %d\n", val[1][i], col[1][i]);
            }
            el;
        #endif
        cnt = 0;    
        for(int i = 1; i <= n; ++i){
            if(!dcc_col[i]){
                dfs(i, ++cnt);
            }
        }
        // for(int i = 1; i <= n; ++i){
        //     printf("%d ", dcc_col[i]);
        // }
        int ans = 1;
        for(int i = 1; i <= cnt; ++i){
            if(!check(i)){
                ans = 0;
                break;
            }
        }
        printf("%s\n", Ans[ans]);
        // break;
    }

    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 6ms
memory: 61144kb

input:

3
2 1
1 2
3 4
RR
4 3
BB
3 2
1 2
2 3
1 1 1
RBR
1 1 1
BBB
3 3
1 2
2 3
3 1
1 1 1
RBR
1 1 1
BBB

output:

YES
NO
YES

result:

ok 3 lines

Test #2:

score: 0
Accepted
time: 520ms
memory: 80584kb

input:

25054
5 10
4 5
4 2
4 3
4 1
5 2
5 3
5 1
2 3
2 1
3 1
12 2 9 10 7
RRRBB
10 2 9 7 12
RRBRB
1 0
4
R
4
R
8 4
4 3
1 5
8 5
6 5
7 2 11 10 9 6 3 5
RBRBBRBR
7 2 10 11 6 5 3 9
RBRBBRBR
7 4
7 2
5 1
5 6
5 4
12 5 9 14 5 12 12
RRBRBRR
14 12 9 5 12 12 5
RBBRBRB
10 1
4 6
5 3 2 9 7 3 11 11 6 8
RRRBRRBBBR
5 3 2 3 7 9 1...

output:

YES
YES
YES
YES
YES
YES
YES
YES
YES
NO
YES
NO
YES
NO
NO
YES
YES
YES
NO
YES
NO
YES
YES
NO
YES
YES
NO
YES
NO
YES
YES
YES
YES
NO
YES
YES
NO
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
NO
NO
YES
NO
YES
YES
YES
YES
NO
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
NO
YES
YES
YES
NO
YES
NO
NO...

result:

ok 25054 lines