QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#131606 | #853. Flat Organization | _LAP_ | WA | 18ms | 5632kb | C++14 | 1.9kb | 2023-07-27 19:03:35 | 2023-07-27 19:03:39 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int N = 2005;
int n, d[N][N];
int pre[N], low[N], dfs_clock;
int scc_siz, sccno[N]; stack<int> stk;
void Tarjan(int u) {
pre[u] = low[u] = ++dfs_clock; stk.push(u);
for(int i = 0; i < n; i ++) if(d[u][i]) {
if(!pre[i]) {
Tarjan(i); low[u] = min(low[u], low[i]);
} else if(!sccno[i]) low[u] = min(low[u], pre[i]);
}
if(low[u] >= pre[u]) {
++scc_siz; int y;
do {
y = stk.top(); stk.pop(); sccno[y] = scc_siz;
} while(y != u);
}
}
int p[N];
int find(int x) {return x == p[x] ? x : p[x] = find(p[x]); }
int main() {
ios::sync_with_stdio(false), cin.tie(0);
int T; cin >> T;
while(T --) { dfs_clock = scc_siz = 0;
cin >> n;
for(int i = 0; i < n; i ++)
for(int j = 0; j < n; j ++)
cin >> d[i][j];
memset(pre, 0, sizeof(int) * n);
memset(low, 0, sizeof(int) * n);
memset(sccno, 0, sizeof(int) * n);
for(int i = 0; i < n; i ++)
if(!pre[i]) Tarjan(i);
for(int i = 0; i < n; i ++) p[sccno[i]] = sccno[i];
typedef pair<int, pair<int, int>> node;
vector<node> vec;
for(int i = 0; i < n; i ++)
for(int j = 0; j < n; j ++)
if(sccno[i] != sccno[j] && d[i][j])
vec.push_back({d[i][j], {i, j}});
sort(vec.begin(), vec.end());
long long ans = 0;
vector<pair<int, int>> res;
for(auto &nd : vec) {
int x = sccno[nd.second.first], y = sccno[nd.second.second];
if(find(x) == find(y)) continue;
p[find(x)] = find(y);
ans += nd.first, res.push_back(nd.second);
}
cout << "YES\n";
cout << res.size() << ' ' << ans << '\n';
for(auto &pr : res) cout << pr.first + 1 << ' ' << pr.second + 1 << '\n';
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3568kb
input:
1 5 0 1 0 6 11 0 0 1 6 12 2 0 0 7 12 0 0 0 0 4 0 0 0 0 0
output:
YES 2 10 4 5 1 4
result:
ok OK!
Test #2:
score: -100
Wrong Answer
time: 18ms
memory: 5632kb
input:
100 5 0 1 0 6 11 0 0 1 6 12 2 0 0 7 12 0 0 0 0 4 0 0 0 0 0 1 0 2 0 0 7 0 2 0 1000000000 0 0 3 0 0 5 6 0 0 0 7 0 3 0 4 6 0 0 0 0 1 0 3 0 4 0 0 0 0 6 3 0 3 0 0 0 10 0 6 3 0 0 3 0 1999999998 1999999999 0 0 1999999998 0 0 0 50 0 105800 801 1800 64800 0 259200 288801 72201 12801 125000 20001 28801 33800 ...
output:
YES 2 10 4 5 1 4 YES 0 0 YES 1 7 2 1 YES 1 1000000000 1 2 YES 0 0 YES 2 5 3 2 1 2 YES 2 7 3 2 1 2 YES 2 9 3 1 2 3 YES 2 3999999996 1 2 2 3 YES 3 602 34 39 4 33 11 47 YES 3 649 32 45 17 29 27 12 YES 5 1209 14 35 4 25 1 18 25 12 35 4 YES 3 844 14 21 1 41 3 8 YES 3 866 46 26 17 35 35 44 YES 4 1066 37 8...
result:
wrong answer Test 3: Contestant claims to have a found a solution on a NO input