QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#293909 | #2679. 校园旅行 | yhk1001 | 100 ✓ | 648ms | 112356kb | C++14 | 2.3kb | 2023-12-29 22:49:10 | 2023-12-29 22:49:12 |
Judging History
answer
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
using namespace std;
// #define Debug
const int N = 5000, M = 5e5;
const int E = M << 1;
int n, m, q;
int col[N + 5];
int head[N + 5], to[E + 5], nxt[E + 5], tot = 1;
void add_edge(int u, int v)
{
tot++;
to[tot] = v;
nxt[tot] = head[u];
head[u] = tot;
return ;
}
void add(int u, int v)
{
add_edge(u, v);
add_edge(v, u);
return ;
}
vector<int> e[N + 5];
int ans[N + 5][N + 5];
int vis[N + 5];
bool dfs1(int u, int fa)
{
if (fa)
e[u].push_back(fa), e[fa].push_back(u);
bool res = true;
for (int i = head[u]; i; i = nxt[i])
{
int v = to[i];
if (col[u] != col[v])
continue;
if (vis[v])
res &= (vis[v] != vis[u]);
else
{
vis[v] = 3 - vis[u];
res &= dfs1(v, u);
}
}
return res;
}
void dfs2(int u, int fa)
{
if (fa)
e[u].push_back(fa), e[fa].push_back(u);
for (int i = head[u]; i; i = nxt[i])
{
int v = to[i];
if (col[u] == col[v] || !vis[v])
continue;
vis[v] = 0;
dfs2(v, u);
}
return ;
}
typedef pair<int, int> pir;
queue<pir> que;
void build()
{
for (int u = 1; u <= n; u++)
{
ans[u][u] = 1;
que.emplace(u, u);
for (auto v : e[u])
{
if (col[u] != col[v] || ans[u][v])//if their's a self-circle
continue;
ans[u][v] = 1;
que.emplace(u, v);
}
}
while (!que.empty())
{
auto t = que.front();
que.pop();
int u = t.first, v = t.second;
for (auto uu : e[u])
for (auto vv : e[v])
{
if (col[uu] != col[vv] || ans[uu][vv])
continue;
ans[uu][vv] = 1;
que.emplace(uu, vv);
}
}
return ;
}
int main()
{
// freopen("tour.in", "r", stdin);
// freopen("tour.out", "w", stdout);
scanf("%d%d%d", &n, &m, &q);
for (int i = 1; i <= n; i++)
scanf("%1d", col + i);
for (int i = 1, u, v; i <= m; i++)
{
scanf("%d%d", &u, &v);
add(u, v);
}
for (int u = 1; u <= n; u++)
{
if (vis[u])
continue;
vis[u] = 1;
bool res = dfs1(u, 0);
if (!res)
e[u].push_back(u);
}
for (int u = 1; u <= n; u++)
{
if (!vis[u])
continue;
vis[u] = 0;
dfs2(u, 0);
}
build();
for (int i = 1, u, v; i <= q; i++)
{
scanf("%d%d", &u, &v);
if (ans[u][v])
puts("YES");
else
puts("NO");
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 10
Accepted
time: 424ms
memory: 81180kb
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: 186ms
memory: 67616kb
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: 184ms
memory: 63660kb
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: 108ms
memory: 59736kb
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: 143ms
memory: 67052kb
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: 174ms
memory: 68444kb
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: 203ms
memory: 67972kb
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: 563ms
memory: 110980kb
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: 621ms
memory: 112116kb
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: 648ms
memory: 112356kb
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