QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#676831 | #7898. I Just Want... One More... | TokaiZaopen | WA | 2ms | 9772kb | C++20 | 5.1kb | 2024-10-26 01:08:37 | 2024-10-26 01:08:37 |
Judging History
answer
#include <bits/stdc++.h>
#pragma - Ofast
// #define int ll
// using ll = long long;
const int inf = 1e9;
using namespace std;
namespace FAST
{
char buf[1 << 20], *p1, *p2;
char gc() { return p1 == p2 ? p2 = buf + fread(p1 = buf, 1, 1 << 20, stdin), (p1 == p2) ? EOF : *p1++ : *p1++; }
inline int read(int f = 1, char c = gc(), int x = 0)
{
while (c < '0' || c > '9')
f = (c == '-') ? -1 : 1, c = gc();
while (c >= '0' && c <= '9')
x = x * 10 + c - '0', c = gc();
return f * x;
}
}
using FAST::read;
struct MF
{
struct edge
{
int v, nxt, cap, flow;
};
edge e[1000010];
// vector<int> fir, dep, cur;
// vector<int> vis;
int fir[200010], dep[200010], cur[200010], vis[200010], gap[200010];
int cnt;
int n;
MF(int node)
{
// fir.resize(node + 5);
// dep.resize(node + 5);
// cur.resize(node + 5);
// vis.resize(node + 5);
n = node;
cnt = 0;
// for (int i = 0; i <= n; i++)
// {
// fir[i] = ~fir[i];
// }
}
void addedge(int from, int to, int w)
{
e[cnt] = {to, fir[from], w, 0};
fir[from] = cnt++;
e[cnt] = {from, fir[to], 0, 0};
fir[to] = cnt++;
}
bool bfs(int st, int ed)
{
queue<int> q;
for (int i = 0; i <= n; i++)
{
dep[i] = 0;
vis[i] = 0;
}
dep[st] = 1;
gap[1] = 1;
vis[st] = 1;
q.push(st);
while (q.size())
{
int u = q.front();
vis[u] = 0;
q.pop();
for (int i = fir[u]; ~i; i = e[i].nxt)
{
// cerr << "///\n";
int v = e[i].v;
if (!dep[v])
{
dep[v] = dep[u] + 1;
gap[dep[v]]++;
if (vis[v])
continue;
q.push(v);
vis[v] = 1;
}
}
}
return dep[ed];
}
int dfs(int st, int u, int flow, int ed)
{
if ((u == ed) || (!flow))
return flow;
int ret = 0;
for (int &i = cur[u]; ~i; i = e[i].nxt)
{
int v = e[i].v, d;
if ((dep[v] == dep[u] + 1) && e[i].cap > e[i].flow)
{
d = dfs(st, v, min(flow - ret, e[i].cap - e[i].flow), ed);
ret += d;
e[i].flow += d;
e[i ^ 1].flow -= d;
if (ret == flow)
return ret;
}
}
gap[dep[u]]--;
if (gap[dep[u]] == 0)
dep[st] = n + 1;
dep[u]++;
gap[dep[u]]++;
return ret;
}
int maxflow(int st, int ed)
{
int res = 0;
bfs(st, ed);
while (dep[st] <= n)
{
for (int i = 0; i <= n; i++)
cur[i] = fir[i];
dfs(st, st, inf, ed);
}
return res;
}
};
int n, m;
bool vis[200010];
// int l[100010];
// int r[100010];
MF graph(200010);
int dfs(int x, MF &g, int mode);
void solve();
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int __ = 1;
__ = read();
while (__--)
solve();
return 0;
}
void solve()
{
n = read();
m = read();
int st, ed;
// for (int i = 1; i <= n; i++)
// {
// l[i] = i;
// r[i] = i + n;
// }
st = n * 2 + 1;
ed = n * 2 + 2;
graph.n = ed;
// graph.e.clear();
for (int i = 0; i <= ed; i++)
{
graph.fir[i] = (~0);
graph.cur[i] = 0;
graph.gap[i] = 0;
// graph.dep[i] = 0;
graph.cnt = 0;
}
for (int i = 1; i <= m; i++)
{
int u, v;
u = read();
v = read();
graph.addedge(u, v + n, 1);
}
for (int i = 1; i <= n; i++)
{
graph.addedge(st, i, 1);
graph.addedge(i + n, ed, 1);
}
graph.maxflow(st, ed);
for (int i = 1; i <= ed; i++)
vis[i] = 0;
vis[st] = 1;
int cntl = dfs(st, graph, 1);
// for (int i = 1; i <= ed; i++)
// vis[i] = 0;
vis[ed] = 1;
int cntr = dfs(ed, graph, 2);
cout << (long long)cntl * (long long)cntr << '\n';
}
int dfs(int x, MF &g, int mode)
{
int res = 0;
if (mode == 1 && 1 <= x && x <= n)
res++;
if (mode == 2 && n + 1 <= x && x <= 2 * n)
res++;
for (int i = g.fir[x]; ~i; i = g.e[i].nxt)
{
int u = g.e[i].v;
if (vis[u])
continue;
if (mode == 1)
{
if (g.e[i].cap - g.e[i].flow < 1 || g.e[i].v == 2 * n + 2)
continue;
}
else
{
if (g.e[i].cap - g.e[i].flow > 0 || g.e[i].v == 2 * n + 1)
continue;
}
vis[u] = 1;
res += dfs(u, g, mode);
}
return res;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 7696kb
input:
3 4 3 1 2 3 2 4 3 3 3 1 3 2 2 3 1 3 2 1 2 1 2
output:
6 0 4
result:
ok 3 number(s): "6 0 4"
Test #2:
score: -100
Wrong Answer
time: 2ms
memory: 9772kb
input:
10000 5 4 2 2 3 1 5 3 1 1 1 1 1 1 1 1 1 1 2 2 2 2 1 2 1 1 1 1 1 1 1 1 1 1 1 1 2 3 2 1 1 2 1 1 5 5 1 4 2 2 3 1 1 3 1 2 2 4 2 2 2 1 1 2 1 1 5 1 5 3 3 1 2 2 1 1 1 1 3 2 3 1 2 1 5 2 1 2 2 3 5 3 1 5 4 2 1 2 4 1 1 1 2 3 1 1 2 2 2 1 4 1 1 4 3 1 1 1 1 1 1 1 2 1 2 2 3 3 1 3 2 3 2 2 3 3 1 3 3 3 1 2 3 3 2 2 1 ...
output:
6 0 0 2 0 0 0 0 6 0 16 4 0 6 9 9 9 0 9 4 0 1 1 1 0 4 16 12 3 2 16 0 2 2 20 1 0 0 0 0 16 4 4 16 4 9 0 9 0 2 3 0 9 4 9 16 20 0 0 1 12 0 1 2 0 0 1 0 0 2 2 4 0 12 1 0 0 2 1 2 2 3 0 4 1 6 0 0 0 0 9 16 2 0 1 2 0 12 2 4 0 12 3 1 9 4 6 9 9 12 3 16 15 16 9 4 9 0 1 16 9 9 1 9 16 9 12 4 9 2 0 4 0 6 0 3 0 0 0 0...
result:
wrong answer 103rd numbers differ - expected: '1', found: '3'