QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#676732 | #7898. I Just Want... One More... | TokaiZaopen | WA | 95ms | 10980kb | C++20 | 5.0kb | 2024-10-25 23:46:57 | 2024-10-25 23:46:58 |
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[200010];
// vector<int> fir, dep, cur;
// vector<int> vis;
int fir[200010], dep[200010], cur[200010], vis[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;
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]) && (e[i].cap > e[i].flow))
{
dep[v] = dep[u] + 1;
if (vis[v])
continue;
q.push(v);
vis[v] = 1;
}
}
}
return dep[ed];
}
int dfs(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) &&
(d = dfs(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)
break;
}
}
if (ret == 0)
dep[u] = -1;
return ret;
}
int maxflow(int st, int ed)
{
int res = 0;
while (bfs(st, ed))
{
for (int i = 0; i <= n; i++)
cur[i] = fir[i];
res += dfs(st, inf, ed);
// cerr << res << '\n';
}
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.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)
{
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;
}
int u = g.e[i].v;
if (vis[u])
continue;
vis[u] = 1;
res += dfs(u, g, mode);
}
return res;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 7976kb
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: 0
Accepted
time: 5ms
memory: 7760kb
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 1 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:
ok 10000 numbers
Test #3:
score: 0
Accepted
time: 9ms
memory: 7896kb
input:
10000 8 1 8 7 8 6 4 5 8 8 6 6 5 3 6 3 8 6 2 3 1 2 2 2 2 1 4 5 1 3 1 2 2 3 1 1 3 3 8 8 4 3 7 3 1 6 5 4 6 3 2 2 6 7 2 5 1 1 1 1 10 2 4 7 9 7 8 10 5 5 5 1 5 2 4 1 3 5 2 4 5 4 6 3 6 4 2 8 10 5 6 10 6 1 7 2 6 6 8 10 8 3 8 4 5 8 2 5 7 1 5 7 5 8 4 2 2 2 5 3 3 4 3 3 4 4 2 5 1 2 1 1 1 1 5 8 4 3 2 4 5 3 2 3 2...
output:
49 16 0 9 16 0 90 18 56 25 36 4 0 6 56 24 9 24 1 9 0 4 90 6 1 30 30 4 0 0 49 15 30 35 20 9 9 36 16 6 35 4 49 24 81 3 0 12 72 36 30 12 9 36 10 2 30 0 0 0 35 0 1 8 0 0 15 6 0 25 49 0 0 3 1 0 8 16 20 0 36 81 0 3 9 30 8 36 0 25 0 49 16 1 0 16 0 0 0 25 8 0 64 64 0 64 9 6 0 81 45 36 16 0 1 25 49 8 4 2 20 ...
result:
ok 10000 numbers
Test #4:
score: 0
Accepted
time: 16ms
memory: 10024kb
input:
10000 11 9 7 5 4 4 4 11 9 7 1 3 9 5 4 9 8 3 7 6 9 7 3 3 4 5 6 2 6 9 2 7 5 9 6 7 8 6 4 7 2 5 6 5 5 8 7 5 6 3 18 1 14 8 17 12 2 11 14 4 8 16 16 1 16 3 3 9 3 2 14 8 6 7 6 16 9 10 9 16 13 1 2 8 9 5 6 2 6 4 5 2 9 7 1 7 7 6 1 7 3 5 1 3 6 4 5 1 5 7 16 7 7 6 6 13 15 7 16 6 8 6 9 1 15 3 18 5 7 17 10 16 16 10...
output:
80 16 20 289 130 144 42 15 169 210 2 36 99 28 144 12 56 169 0 42 36 289 100 70 0 225 81 12 42 4 225 0 12 0 12 168 100 72 289 144 210 100 18 80 110 180 210 0 35 0 64 0 0 130 144 0 40 144 20 0 20 2 121 108 120 132 12 120 42 81 182 9 196 0 0 0 9 99 0 110 132 21 96 0 0 72 8 108 132 25 196 42 221 49 1 72...
result:
ok 10000 numbers
Test #5:
score: 0
Accepted
time: 23ms
memory: 7816kb
input:
10000 17 13 2 9 7 12 1 10 9 15 16 9 10 11 10 4 6 4 10 15 16 13 15 11 11 11 1 12 9 19 9 1 7 2 4 2 4 8 3 9 1 2 3 2 2 8 4 4 3 7 1 7 3 1 9 2 3 4 8 3 2 5 2 3 9 7 8 5 23 25 15 14 14 9 20 17 7 15 20 5 9 22 19 4 1 3 5 18 22 23 17 7 19 5 4 11 22 20 11 4 8 21 7 17 2 12 6 6 11 3 3 14 11 14 3 20 15 5 6 5 27 15 ...
output:
130 12 49 272 4 0 342 306 462 8 42 16 143 9 40 0 156 16 234 30 9 126 238 36 0 36 110 441 165 18 30 306 91 121 0 3 225 110 0 48 399 169 0 154 56 8 4 0 18 16 324 117 0 1 9 104 0 120 63 180 288 55 484 81 4 49 288 288 0 169 24 285 1 48 4 54 210 525 0 8 18 78 625 441 18 48 30 90 1 576 225 16 624 304 0 0 ...
result:
ok 10000 numbers
Test #6:
score: 0
Accepted
time: 31ms
memory: 8932kb
input:
10000 16 17 13 12 13 9 8 10 13 15 10 2 13 7 7 14 2 11 6 9 7 11 1 3 8 7 9 1 6 7 1 11 6 4 14 13 10 3 6 2 9 6 2 3 19 3 1 18 14 4 2 2 16 1 1 2 20 24 15 7 1 16 7 5 15 9 5 8 11 6 18 11 18 17 4 15 2 17 11 5 15 14 20 7 3 13 10 10 7 14 20 3 16 16 17 17 19 17 4 16 17 7 16 14 7 1 31 32 26 6 6 3 7 31 12 18 11 9...
output:
70 49 256 225 72 420 625 0 48 132 0 0 70 112 132 0 24 0 182 8 238 2 342 0 32 0 72 357 0 60 156 399 126 784 72 7 266 25 783 28 208 529 20 104 441 30 255 0 1 725 0 16 324 16 0 0 70 36 2 210 40 224 28 289 484 54 380 255 0 20 1024 221 0 418 81 2 625 420 36 0 0 900 16 378 324 380 182 756 784 378 156 56 0...
result:
ok 10000 numbers
Test #7:
score: 0
Accepted
time: 33ms
memory: 8944kb
input:
8195 31 31 10 29 11 22 28 13 18 14 6 17 7 22 1 20 9 14 28 4 17 29 25 10 8 12 21 29 30 16 26 27 28 5 20 5 4 13 1 19 8 2 28 12 3 10 30 27 6 11 8 7 27 23 8 5 4 15 29 13 1 26 11 18 42 19 17 13 5 9 41 30 30 10 4 28 31 14 35 14 13 37 7 23 37 28 2 11 42 19 15 32 31 15 37 24 7 41 35 8 2 38 9 26 22 2 6 20 17...
output:
380 896 400 528 169 506 6 156 77 117 0 196 42 9 676 42 64 42 196 529 1024 132 729 784 44 400 0 30 96 2116 12 108 0 9 650 110 104 56 650 182 9 736 132 840 360 0 756 0 552 176 783 342 575 56 260 900 27 420 624 182 600 120 24 294 756 176 182 195 4 992 180 420 1722 14 400 16 306 6 96 154 440 638 120 104...
result:
ok 8195 numbers
Test #8:
score: 0
Accepted
time: 22ms
memory: 10980kb
input:
36 200 34923 19 6 111 30 122 58 130 123 79 127 51 17 77 115 180 115 135 39 59 17 6 92 164 83 191 61 135 194 21 53 118 131 99 32 115 128 136 73 149 164 80 61 143 172 183 67 178 34 141 11 63 158 198 99 136 181 199 154 51 124 181 143 196 73 129 36 103 26 20 132 113 184 70 21 54 95 151 64 20 85 9 118 31...
output:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 39601 39601 39601 39601 0 39601 39601 39601 39601 39601 39601 39601 39601 39601 39601 39601 39601 39601
result:
ok 36 numbers
Test #9:
score: 0
Accepted
time: 31ms
memory: 10780kb
input:
47 300 73618 107 45 10 195 243 73 94 169 32 105 204 216 195 153 120 31 175 135 145 78 234 209 118 28 60 136 231 58 187 136 151 217 176 160 91 269 9 6 62 262 45 37 258 86 54 3 9 71 74 291 180 162 97 238 12 124 205 106 26 48 95 188 25 231 190 208 164 86 202 258 177 102 99 210 259 159 293 269 241 22 9 ...
output:
0 0 0 873 0 0 0 0 89401 0 0 89401 89401 89401 89401 89401 89401 89401 0 89401 89401 89401 89401 89401 89401 89401 89401 89401 1425 89401 89401 89401 89401 89401 89401 89401 89401 89401 89401 89401 89401 89401 2630 89401 89401 89401 46440
result:
ok 47 numbers
Test #10:
score: 0
Accepted
time: 95ms
memory: 10640kb
input:
11 5000 82272 1471 562 571 4062 2376 3538 1758 879 4355 4876 1765 2860 1225 3209 498 1448 589 4453 2795 2625 4702 2777 273 2409 3097 2177 673 1783 734 3503 4990 4311 30 3022 533 2902 4075 431 2471 1295 3584 3647 2082 4048 1501 4783 4753 2661 3844 931 1647 2404 2359 2724 3576 1480 4808 1718 33 4135 7...
output:
0 0 0 0 0 49690 24990001 19470126 24990001 9578925 24990001
result:
ok 11 numbers
Test #11:
score: -100
Wrong Answer
time: 45ms
memory: 10224kb
input:
3 5000 100000 4850 2272 3933 2025 680 4017 1731 2777 4531 3649 604 3731 1982 3943 2572 2993 2689 3809 109 770 375 1878 915 1872 583 1674 801 1824 1372 4181 411 4222 997 2596 1434 4470 2509 4087 659 2740 2748 4217 2424 4669 2604 4588 3965 3636 309 3474 3324 1398 3977 653 4482 2406 1688 2064 3575 634 ...
output:
17641179 17746510 17679880
result:
wrong answer 1st numbers differ - expected: '0', found: '17641179'