QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#333361#2679. 校园旅行james1BadCreeper100 ✓524ms109488kbC++142.0kb2024-02-19 20:55:282024-02-19 20:55:29

Judging History

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

  • [2024-02-19 20:55:29]
  • 评测
  • 测评结果:100
  • 用时:524ms
  • 内存:109488kb
  • [2024-02-19 20:55:28]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std; 
const int N = 5e3 + 5; 
const int M = 5e5 + 5; 

int n, m, q, u[M], v[M], tag[N]; 
int f[N][N], col[N], is[N], fa[2][N]; 
char a[N]; 
vector<int> E[N], G[N]; 

int find(int x, int i) {
    if (fa[i][x] == x) return x; 
    return fa[i][x] = find(fa[i][x], i); 
}

void dfs(int x, int c) {
    col[x] = c; 
    for (int y : E[x]) {
        if (!col[y]) dfs(y, -c); 
        else if (col[x] == col[y]) is[abs(c)] = 1; 
    }
}

int main(void) {
    ios::sync_with_stdio(0); cin.tie(0); 
    cin >> n >> m >> q >> a + 1; 
    queue<pair<int, int>> Q; 
    for (int i = 1; i <= n; ++i) a[i] -= '0', f[i][i] = 1, Q.emplace(i, i), fa[0][i] = fa[1][i] = i; 
    for (int i = 1; i <= m; ++i) {
        cin >> u[i] >> v[i]; 
        if (a[u[i]] == a[v[i]]) E[u[i]].emplace_back(v[i]), E[v[i]].emplace_back(u[i]); 
    }

    for (int i = 1; i <= n; ++i) if (!col[i]) dfs(i, i); 
    for (int i = 1; i <= m; ++i) {
        if (a[u[i]] != a[v[i]]) {
            int x = find(u[i], 0), y = find(v[i], 0); 
            if (x == y) continue; 
            G[u[i]].emplace_back(v[i]); G[v[i]].emplace_back(u[i]); 
            fa[0][x] = y; 
        } else {
            int x = find(u[i], 1), y = find(v[i], 1); 
            if (x == y) continue; 
            G[u[i]].emplace_back(v[i]); G[v[i]].emplace_back(u[i]); 
            fa[1][x] = y; 
            f[u[i]][v[i]] = f[v[i]][u[i]] = 1; 
            Q.emplace(u[i], v[i]); 
        }
    }

    for (int i = 1; i <= n; ++i) if (is[abs(col[i])]) G[i].emplace_back(i); 
    
    while (!Q.empty()) {
        auto [i, j] = Q.front(); Q.pop(); 
        for (int x : G[i]) for (int y : G[j]) if (a[x] == a[y] && !f[x][y]) {
            f[x][y] = f[y][x] = 1; 
            Q.emplace(x, y); 
        }
    }

    while (q--) {
        int x, y; cin >> x >> y; 
        cout << (f[x][y] ? "YES\n" : "NO\n"); 
    }
    return 0; 
}

/*
5 5 1
01000
1 2
2 3
3 4
4 5
3 3

1 5
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 10
Accepted
time: 288ms
memory: 76840kb

input:

3000 10000 100000
110000100010110011101010100100001001000101110111011011101111111001100001011010101011001010100001011111001010100011101100101111110001011100000011000010101101010110110111001011110100100011000110101011000110110000011010101100000101111011010001110010011100010111110100010000011001001101...

output:

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

result:

ok 100000 lines

Test #2:

score: 10
Accepted
time: 140ms
memory: 65176kb

input:

3000 9265 100000
0111101100111000110010101001111100011111111011000111101101010000011110010100011011000011100001111011010001010110100011001111011010101111011101101101111010001110101010000000111110011000101110111011001101000010111101011011000110101010001110011111101100111011010001101110011000001010001...

output:

NO
YES
YES
YES
NO
YES
YES
YES
YES
YES
YES
NO
YES
YES
YES
YES
YES
YES
YES
YES
NO
NO
YES
YES
YES
YES
YES
YES
NO
YES
NO
YES
YES
YES
YES
YES
YES
YES
NO
NO
YES
YES
YES
YES
YES
NO
YES
YES
NO
YES
YES
YES
NO
YES
YES
YES
YES
YES
NO
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
NO
YES
NO
YES
YES
YES
NO
YES
YES
NO
Y...

result:

ok 100000 lines

Test #3:

score: 10
Accepted
time: 116ms
memory: 68228kb

input:

3000 10000 100000
111101111111010000110001010110001111111011000110100101100000111110000110011010010001100110110011100010011100011001000111110100101011010001100010011010100001011101001000110000110111000100111010010001111111011100111000011110110111011111100111100101011110011111111001100011100100111111...

output:

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

result:

ok 100000 lines

Test #4:

score: 10
Accepted
time: 63ms
memory: 68440kb

input:

3000 50000 100000
010100100001111000100101000001011111100010100010101010100110100010110010110000011010100001001110000101100000001101100010001001011001110010101011011011011000101111111100011010000001110100001001010111011111011001100011111000011000011101001100010111000111010001100010100111110010001011...

output:

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

result:

ok 100000 lines

Test #5:

score: 10
Accepted
time: 121ms
memory: 68568kb

input:

3000 50000 100000
001000000001001110011111011010101001000101110110000110011100101010010011101000110010111010001010110001011000111100111101100000111000101111011010000101101111110010000111011100100001111000000001111110000110100011100010110111100101000110111011000001111100000111101000001101001110101010...

output:

YES
YES
NO
NO
NO
YES
YES
YES
YES
NO
NO
NO
YES
NO
NO
NO
NO
NO
NO
YES
YES
YES
YES
NO
NO
YES
NO
NO
YES
NO
NO
NO
YES
YES
NO
YES
YES
YES
NO
NO
NO
YES
YES
NO
YES
NO
NO
NO
YES
YES
NO
YES
YES
YES
NO
YES
YES
YES
NO
YES
YES
YES
YES
YES
NO
NO
YES
NO
YES
YES
YES
YES
NO
YES
NO
YES
YES
YES
YES
YES
NO
NO
YES
YES
N...

result:

ok 100000 lines

Test #6:

score: 10
Accepted
time: 123ms
memory: 67876kb

input:

3000 50000 100000
110011011010100101101101100111100110011110101011111001101011110110101010101110000111111001000001100011100011001100010011011101101011010000100101000110110111001111111001111011001110101110001110110100010000111100000101101111000001111010011101100111111110010001011011101001011110100111...

output:

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

result:

ok 100000 lines

Test #7:

score: 10
Accepted
time: 143ms
memory: 65400kb

input:

3000 50000 100000
111011101011110010111011011110110011101010100000101111100100011001000010110001010000101111010110110000110111100011110101000100001100100100011011100010010010101111100010000010011101001101000001111011001011101001101111100001100001101100100110011010001101111101100111001010111100100011...

output:

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

result:

ok 100000 lines

Test #8:

score: 10
Accepted
time: 507ms
memory: 107844kb

input:

5000 361135 100000
00000011101011100011110101101001100110010001101001100101101001111010101110111010111100110000010110011100100100011100101010011011000011111010110111011010100001010011001110001111010001110010000000100110101111000110111001111011110111100000110011101101000110001111001101000111010011111...

output:

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

result:

ok 100000 lines

Test #9:

score: 10
Accepted
time: 510ms
memory: 109488kb

input:

5000 500000 100000
10101100011011001010110100000010100001110111001010100100100100000010010001001011001110001010110000000010110011010101010101001011110111001001100001001011011001001010110101111001101111110010001101010010101001101100110100001010000110001100011001001110110110100100100101111101000001111...

output:

NO
NO
NO
YES
NO
YES
NO
YES
YES
YES
YES
NO
YES
YES
YES
YES
NO
NO
YES
YES
YES
YES
YES
NO
YES
NO
YES
YES
NO
YES
YES
NO
YES
NO
YES
YES
YES
NO
NO
YES
YES
NO
YES
NO
NO
YES
YES
YES
YES
NO
NO
NO
NO
NO
YES
NO
YES
NO
NO
NO
YES
YES
YES
YES
YES
YES
YES
NO
NO
YES
YES
NO
YES
YES
NO
NO
YES
NO
YES
NO
NO
YES
NO
NO
Y...

result:

ok 100000 lines

Test #10:

score: 10
Accepted
time: 524ms
memory: 109204kb

input:

5000 500000 100000
11011101110101000011000010101011101000000011001111101011111001010111000101110000011100100101011001011011011001011111011101000111010101100000000110001010011000010101010100000101101001100101010001111011001100111001001001011000001000100111011110101011011100010011100001101000001010110...

output:

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

result:

ok 100000 lines