QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#309612 | #8131. Filesystem | ucup-team191# | WA | 1ms | 4076kb | C++14 | 2.2kb | 2024-01-20 19:01:07 | 2024-01-20 19:01:07 |
Judging History
answer
#include <cstdio>
#include <vector>
#include <algorithm>
#define PB push_back
#define sz(x) (int)(x).size()
using namespace std;
typedef vector < int > vi;
const int N = 1050;
const int INF = 0x3f3f3f3f;
int n, k, bold[N], p[N];
struct Dinic{
struct Edge {
int to, rev, c, oc;
int flow() { return max(oc - c, 0); }
};
vi lvl, ptr, q;
vector<vector<Edge>> adj;
Dinic(int n) : lvl(n), ptr(n), q(n), adj(n) {}
void add_edge(int a, int b, int c, int rcap) {
adj[a].PB({b, sz(adj[b]), c, c});
adj[b].PB({a, sz(adj[a]) - 1, rcap, rcap});
}
int dfs(int v, int t, int f) {
if(v == t || !f) return f;
for(int &i = ptr[v];i < sz(adj[v]);i++) {
Edge &e = adj[v][i];
if (lvl[e.to] == lvl[v] + 1)
if(int p = dfs(e.to, t, min(f, e.c))) {
e.c -= p, adj[e.to][e.rev].c += p;
return p;
}
}
return 0;
}
int calc(int s, int t) {
int flow = 0; q[0] = s;
for(int L = 0;L < 31;L++)
do {
lvl = ptr = vi(sz(q));
int qi = 0, qe = lvl[s] = 1;
while(qi < qe && !lvl[t]) {
int v = q[qi++];
for(Edge e : adj[v])
if(!lvl[e.to] && e.c >> (30 - L))
q[qe++] = e.to, lvl[e.to] = lvl[v] + 1;
}
while (int p = dfs(s, t, INF)) flow += p;
} while(lvl[t]);
return flow;
}
};
void solve(){
scanf("%d%d", &n, &k);
Dinic F(n + 2);
for(int i = 1;i <= n;i++) bold[i] = 0;
for(int i = 0;i < k;i++) {
int x; scanf("%d", &x);
bold[x] = 1;
}
for(int i = 1;i <= n;i++) {
scanf("%d", p + i);
}
if(bold[1]) F.add_edge(0, 1, 1, 1);
if(bold[n]) F.add_edge(0, n, 1, 1);
for(int i = 1;i <= n;i++) {
if(bold[i] && bold[i + 1]) F.add_edge(i, i + 1, 1, 1);
else if(bold[i]) F.add_edge(i, 0, 1, 1);
else if(bold[i + 1]) F.add_edge(i + 1, 0, 1, 1);
}
if(bold[p[1]]) F.add_edge(n + 1, p[1], 1, 1);
if(bold[p[n]]) F.add_edge(n + 1, p[n], 1, 1);
for(int i = 1;i <= n;i++) {
if(bold[p[i]] && bold[p[i + 1]]) F.add_edge(p[i], p[i + 1], 1, 1);
else if(bold[p[i]]) F.add_edge(p[i], n + 1, 1, 1);
else if(bold[p[i + 1]]) F.add_edge(p[i + 1], n + 1, 1, 1);
}
printf("%d\n", F.calc(0, n + 1) / 2);
}
int main(){
int T; scanf("%d", &T);
for(;T--;) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3820kb
input:
2 12 8 2 5 8 3 4 10 12 1 10 5 8 6 4 7 2 3 11 12 1 9 8 4 1 3 5 7 1 4 5 8 7 6 3 2
output:
3 4
result:
ok 2 number(s): "3 4"
Test #2:
score: 0
Accepted
time: 1ms
memory: 4076kb
input:
100 10 3 10 5 2 2 4 9 8 7 3 1 5 10 6 10 3 8 1 10 8 4 3 7 1 2 9 5 6 10 10 3 6 5 2 8 7 6 4 2 10 1 3 5 9 10 3 5 8 4 10 4 5 7 8 9 1 2 3 6 10 3 8 4 10 10 6 9 2 8 7 1 4 3 5 10 3 9 8 1 8 5 6 10 2 4 1 7 9 3 10 3 5 4 1 7 5 8 4 3 6 9 10 2 1 10 3 2 4 3 6 7 3 9 1 2 5 8 4 10 10 3 9 5 3 6 10 7 4 9 3 1 8 5 2 10 3 ...
output:
2 3 2 2 3 2 2 1 2 2 3 2 2 2 2 2 2 2 3 2 2 2 2 2 2 2 2 3 1 3 1 2 2 2 2 2 2 2 3 2 2 3 3 3 2 2 2 3 2 2 2 2 2 2 3 2 2 2 2 2 3 2 2 1 2 1 2 2 2 1 3 2 3 1 2 2 3 2 2 3 3 2 3 2 2 2 2 3 2 2 2 2 3 1 3 3 2 2 2 2
result:
ok 100 numbers
Test #3:
score: -100
Wrong Answer
time: 1ms
memory: 3816kb
input:
100 10 5 2 6 1 9 3 10 8 2 6 9 7 5 1 3 4 10 5 7 10 1 3 6 5 7 3 9 4 1 8 2 10 6 10 5 8 3 6 2 9 4 2 9 3 8 1 6 10 7 5 10 5 5 6 7 4 3 3 7 6 2 1 5 9 8 10 4 10 5 1 6 8 10 5 8 6 3 10 1 4 7 9 5 2 10 5 6 5 1 3 9 8 10 5 3 6 1 9 2 7 4 10 5 8 6 2 7 1 3 6 5 10 9 1 2 7 4 8 10 5 6 10 8 4 2 3 9 7 5 4 1 10 6 2 8 10 5 ...
output:
2 3 2 1 3 1 2 2 2 3 3 2 2 2 2 4 3 2 3 2 2 3 3 2 3 3 2 1 1 2 2 2 3 3 2 3 3 3 3 1 4 3 3 3 2 2 2 3 4 3 2 2 2 2 2 3 2 2 3 2 2 1 2 2 3 2 2 2 3 3 3 3 3 4 2 3 2 3 2 3 2 2 2 2 2 2 2 2 2 2 3 3 3 2 2 2 2 3 3 2
result:
wrong answer 59th numbers differ - expected: '2', found: '3'