QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#640499 | #8943. Challenge Matrix Multiplication | 口嗨战神 (Binyang Jiang, Dayu Wang, Hejun Dong) | WA | 0ms | 3736kb | C++20 | 2.0kb | 2024-10-14 13:43:36 | 2024-10-14 13:43:36 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define db double
void solve() {
int n, m;
cin >> n >> m;
vector<vector<int>> out(n + 1);
vector<int> bel(n + 1), pos(n + 1), in(n + 1), vis(n + 1);
vector<vector<int>> chain;
queue<int> q;
for (int i = 1; i <= m; i++) {
int a, b;
cin >> a >> b;
out[a].pb(b);
in[b]++;
}
for (int i = 1; i <= n; i++) {
if (in[i] == 0) {
q.push(i);
}
}
while (q.size()) {
int x = q.front();
q.pop();
if (vis[x]) continue;
vector<int> tmp;
while (1) {
vis[x] = 1;
bel[x] = chain.size();
pos[x] = tmp.size();
tmp.pb(x);
for (int y : out[x]) {
if (--in[y] == 0) q.push(y);
}
int z = -1;
for (int y : out[x]) {
if (!vis[y]) {
z = y;
break;
}
}
if (z == -1) break;
x = z;
}
chain.pb(tmp);
}
constexpr int inf = 1e9;
vector dp(n + 1, vector(chain.size(), inf));
function<void(int)> dfs = [&](int x) -> void {
if (dp[x][0] != inf) return;
dp[x][bel[x]] = pos[x];
for (int y : out[x]) {
if (bel[x] == bel[y]) continue;
dfs(y);
for (int i = 0; i < chain.size(); i++) {
dp[x][i] = min(dp[x][i], dp[y][i]);
}
}
};
for (int i = 1; i <= n; i++) {
dfs(i);
int ans = 0;
for (int j = 0; j < chain.size(); j++) {
ans += max(0, (int)chain[j].size() - dp[i][j]);
}
cout << ans << " \n"[i==n];
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
srand(time(0));
int t = 1;
// cin>>t;
while (t--) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3732kb
input:
4 6 1 3 2 3 2 4 1 2 1 3 1 3
output:
4 3 1 1
result:
ok 4 number(s): "4 3 1 1"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3736kb
input:
5 7 1 4 1 5 1 2 2 4 3 4 2 5 1 4
output:
4 3 2 1 1
result:
ok 5 number(s): "4 3 2 1 1"
Test #3:
score: -100
Wrong Answer
time: 0ms
memory: 3728kb
input:
100 900 89 90 34 35 45 46 97 98 41 42 59 61 19 20 25 29 2 3 28 29 54 55 77 78 69 74 60 61 43 44 13 14 92 93 65 66 68 69 72 73 78 81 54 56 55 60 14 15 9 10 92 93 10 11 18 19 16 17 97 98 76 77 39 40 71 72 7 8 63 64 7 8 16 24 13 24 83 84 90 91 1 4 4 5 96 97 81 82 91 92 80 81 66 67 19 20 3 4 9 10 47 48 ...
output:
95 91 93 73 72 90 89 69 87 85 66 84 80 82 81 79 79 78 77 59 74 74 73 72 71 60 49 68 55 66 65 64 55 62 53 52 51 50 49 48 55 46 45 44 43 49 41 40 51 38 49 48 47 46 38 4 3 39 41 40 32 38 37 30 29 34 33 26 31 30 3 28 27 26 23 22 22 22 21 20 19 18 17 16 15 14 14 13 12 11 10 9 8 7 6 5 4 3 2 1
result:
wrong answer 1st numbers differ - expected: '100', found: '95'