QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#417505 | #8680. Turning Red | Milan | RE | 69ms | 24120kb | C++14 | 3.0kb | 2024-05-22 19:22:09 | 2024-05-22 19:22:09 |
Judging History
answer
#include <bits/stdc++.h>
#define MULTI \
int _; \
cin >> _; \
while (_--)
#define fi first
#define se second
#define pb(a) push_back(a)
#define rep(i, n) for (int i = 0; i < n; i++)
#define reps(i, n, m) for (int i = n; i <= m; i++)
#define repsv(i, n, m) for (int i = n; i >= m; i--)
#define vsz(a) (int)(a.size())
#define mp(a, b) make_pair(a, b)
#define all(a) a.begin(), a.end()
using namespace std;
typedef pair<double, double> pdd;
typedef pair<int, int> pii;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef long long int ll;
typedef long double ld;
typedef vector<ll> vll;
#ifdef LOCAL
#include "debugs.hpp"
#else
#define dbg(...) 0
#endif
vi l;
vector<vi> gl, gb;
vector<bool> traite;
vi vul, vub, pressb;
bool dfs1(int b, int presses, int& acc, int pid);
bool dfs2(int i, int presses, int& acc, int pid, int bPrec);
bool dfs1(int b, int presses, int& acc, int pid){
dbg(b, presses);
acc += presses;
vub[b] = pid;
traite[b] = true;
pressb[b] = presses;
for(int i : gb[b]){
if(vul[i] != pid){
if(!dfs2(i, presses, acc, pid, b))
return false;
}
}
return true;
}
bool dfs2(int i, int presses, int& acc, int pid, int bPrec){
int val = (l[i]+presses)%3;
vul[i] = pid;
dbg(i, val);
if(gl[i].size() == 1){
dbg("size 1");
return val == 0;
}
int nextPresses = (3-val)%3;
dbg(nextPresses);
for(int j : gl[i]){
dbg(j);
if(vub[j] != pid){
if(!dfs1(j, nextPresses, acc, pid))
return false;
}
else if(j != bPrec && pressb[j] != nextPresses)
return false;
}
return true;
}
void solve()
{
int ln, bn;
cin >> ln >> bn;
l = vi(ln);
gl = vector<vi>(ln);
gb = vector<vi>(bn);
rep(i, ln){
char c;
cin >> c;
if(c == 'R') l[i] = 0;
else if(c == 'G') l[i] = 1;
else l[i] = 2;
}
rep(i, bn){
int n;
cin >> n;
rep(j, n){
int in;
cin >> in;
in--;
gl[in].pb(i);
gb[i].pb(in);
}
}
rep(i, ln){
if(gl[i].size() == 0 && l[i] != 0){
cout << "impossible\n";
return;
}
}
traite = vector<bool>(bn);
vul = vi(ln), vub = vi(ln);
pressb = vi(bn);
int pid = 0;
int sum = 0;
rep(bbase, bn){
if(traite[bbase]) continue;
int curbest = INT_MAX;
rep(i, 3){
int acc = 0;
if(dfs1(bbase, i, acc, ++pid)){
curbest = min(curbest, acc);
}
}
if(curbest == INT_MAX){
cout << "impossible\n";
return;
}
sum += curbest;
}
cout << sum << '\n';
}
int main()
{
ios_base::sync_with_stdio(false);
// MULTI
solve();
}
詳細信息
Test #1:
score: 100
Accepted
time: 69ms
memory: 24120kb
input:
200000 171004 RGRGBBBGGBRRRBBRRBRBBGRRRGGGGRRRRGBBBRGGGBGGRGGRGRRBRBRRGGBGGGGBRBRBRRBRBRGRRGRBRRBGRRRRBRBBBRGBBBGRRBRGRRGGRGGBGRBGRBRGBBRRBBBGBGRGBBRBGGGGBBBBRBRRRGBGRRBBGGGRRBRBBRGRBGRBRGGBRBBBBRGGRRRBGRBGRBRRRGRGRRBRGRRGRRGBBBBRGGBBRRBGGBGBGRBRGBGGRRGRBRRGGRGRBBBRRBBRRGGRRRRGBRGGGGGGRGBRGRBBBGRBBG...
output:
impossible
result:
ok single line: 'impossible'
Test #2:
score: 0
Accepted
time: 0ms
memory: 3584kb
input:
10 0 RRRRRRRRRR
output:
0
result:
ok single line: '0'
Test #3:
score: 0
Accepted
time: 0ms
memory: 3640kb
input:
10 0 GGGGGGGGGG
output:
impossible
result:
ok single line: 'impossible'
Test #4:
score: 0
Accepted
time: 0ms
memory: 3640kb
input:
10 0 BBBBBBBBBB
output:
impossible
result:
ok single line: 'impossible'
Test #5:
score: 0
Accepted
time: 0ms
memory: 3588kb
input:
10 0 RGBRGBRGBR
output:
impossible
result:
ok single line: 'impossible'
Test #6:
score: 0
Accepted
time: 0ms
memory: 3536kb
input:
10 0 GBBRRRBRGB
output:
impossible
result:
ok single line: 'impossible'
Test #7:
score: 0
Accepted
time: 0ms
memory: 3576kb
input:
10 2 BBRRBRBRRR 7 7 8 5 3 10 9 4 5 6 10 1 3 2
output:
impossible
result:
ok single line: 'impossible'
Test #8:
score: 0
Accepted
time: 0ms
memory: 3844kb
input:
10 9 RRRRRBGRRG 1 10 5 9 3 8 2 5 2 8 7 1 7 1 10 2 6 1 1 3 1 6 4 1 9 4 2
output:
5
result:
ok single line: '5'
Test #9:
score: 0
Accepted
time: 59ms
memory: 17472kb
input:
200000 10000 RBBBBGRRGGRBRGRGBBRBGGRGRGRBBRRRGRGRGRBGRBBBBGBGBBGGBBGGGGBRGBBBBBBRRGBBGGGRBBBGBRGGGRRGGGGBGBGBGRBBGGRRRGGGRGGRBRRBGRGRBBGBGGRGBBRBBBGGRGBBGBBBBBGRGBGGRGBGGBGRRBGBBGRBGBGGRBBBGBBBGGGBBBRBBGRRRBGBBRBRBBGRGGGBBRGRRRGRRRRGRRBGBRRRRGBBRBBGRRRRBRBRRRGBGRGGBGRBRBGBBRRGBBRBBBGBGGGBBRGRBRRGBBR...
output:
impossible
result:
ok single line: 'impossible'
Test #10:
score: 0
Accepted
time: 62ms
memory: 18680kb
input:
200000 49974 GRBRRGBRRRRRGBRBGRRBGGGRRRRBGGBBBGRRGBBRBGBRBRRBRBBRGRBGGBBGBBRRRBGGBRBRRGBRRRBRRGRGRGRBGRBGBBBGGGRGRBGRGGRBBRBRGGBRBBRRGGGRBGBGBGRBRBRGGBBBRBBRGRRBBGBGRBBRBBBRGGGGRRBGBBRGBGBGGBRBGGRGGGBRRRBBBGRRRRGBBBGBBGRRRBGRGGGRGBRGBGRBBRBRRBGGRGBBRGBBBRGRBBBRRGGRRRGGRRRGRRBGGGBGRRGBRRRRRGRGBGGRRRR...
output:
impossible
result:
ok single line: 'impossible'
Test #11:
score: 0
Accepted
time: 60ms
memory: 20736kb
input:
200000 97963 GRBBRBGGGBGBBGRBBBRGRGBBBGBRBGBGGGBRBBGRGRRBRRGBRRBBGRRGBRRGBBRBGRRRGRGRRRBGBBRGGBRRBBBGRBBGBBRRRGRRGRGRRRBBRGBRRRRRGGGGGBGRRBGRGGBBBBRBBRBBBBBBRRRGBGGRGRGRBBRRBGBRRBBRGBGRBRBRGBRRGBRBGGRGGGGBGGGRBBRGRGGBBGRRGRBGGRBGRBBBRRGGRGGBRRBBRGGRBRBBGRRRGGGBRBRGBRBRBBGGBRGRBBRGGBBBGGBGRRRRRRGGGBR...
output:
impossible
result:
ok single line: 'impossible'
Test #12:
score: -100
Runtime Error
input:
200000 400000 GRGBBBRRGGRGBBRBBGBRRRBBRBRGRGRRBBRBRGRBRGGRRGGBRGRRBRBRRBRRGGBBGRBBGRRRBRBGBBRBGBRBBBBBBGBRGRBBGRGBGBBGRGBGBBBBGRGRRRGGGBGGGGRBRBRBRRBGGBGRRGBGGGRRBBBGRBRBGGBBRBBGRGGBGRGBGRRGGBRGGRGRGGBGBRBBBRGGBRRRRBGRBGRRGBRBRGRRBBGGGRRGRGGBRBBBBRGBBGRGGBGGRBGGRBRBBRBBBGRGGGGGBGGRRBBGRGGGRGBRBGGGGR...