QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#111379 | #6526. Canvas | hos_lyric | WA | 1ms | 3676kb | C++14 | 5.3kb | 2023-06-06 22:05:55 | 2023-06-06 22:05:59 |
Judging History
answer
#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using Int = long long;
template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
struct Scc {
int n;
vector<int> as, bs;
vector<int> ptr, zu, us;
int l;
vector<int> ids;
int operator[](int u) const { return ids[u]; }
explicit Scc(int n_) : n(n_), as(), bs(), l(-1) {}
void ae(int u, int v) {
assert(0 <= u); assert(u < n);
assert(0 <= v); assert(v < n);
as.push_back(u);
bs.push_back(v);
}
void dfs0(int u) {
if (!ids[u]) {
ids[u] = -1;
for (int i = ptr[u]; i < ptr[u + 1]; ++i) dfs0(zu[i]);
us.push_back(u);
}
}
void dfs1(int u) {
if (!~ids[u]) {
ids[u] = l;
for (int i = ptr[u]; i < ptr[u + 1]; ++i) dfs1(zu[i]);
}
}
void run() {
const int m = as.size();
ptr.resize(n + 2);
zu.resize(m);
for (int u = 0; u < n + 2; ++u) ptr[u] = 0;
for (int i = 0; i < m; ++i) ++ptr[as[i] + 2];
for (int u = 2; u < n + 1; ++u) ptr[u + 1] += ptr[u];
for (int i = 0; i < m; ++i) zu[ptr[as[i] + 1]++] = bs[i];
ids.assign(n, 0);
us.clear();
for (int u = 0; u < n; ++u) dfs0(u);
for (int u = 0; u < n + 2; ++u) ptr[u] = 0;
for (int i = 0; i < m; ++i) ++ptr[bs[i] + 2];
for (int u = 2; u < n + 1; ++u) ptr[u + 1] += ptr[u];
for (int i = 0; i < m; ++i) zu[ptr[bs[i] + 1]++] = as[i];
l = 0;
for (int j = n; --j >= 0; ) if (!~ids[us[j]]) { dfs1(us[j]); ++l; }
}
vector<vector<int>> group() const {
assert(~l);
vector<vector<int>> uss(l);
for (int u = 0; u < n; ++u) uss[ids[u]].push_back(u);
return uss;
}
};
int N, M;
vector<int> A, B, X, Y;
vector<vector<int>> G;
vector<int> vis, is;
void dfs(int u) {
vis[u] = 1;
for (const int i : G[u]) {
const int v = B[i];
if (!vis[v]) {
dfs(v);
is.push_back(i);
}
}
}
int main() {
for (int numCases; ~scanf("%d", &numCases); ) { for (int caseId = 1; caseId <= numCases; ++caseId) {
scanf("%d%d", &N, &M);
A.resize(M);
B.resize(M);
X.resize(M);
Y.resize(M);
for (int i = 0; i < M; ++i) {
scanf("%d%d%d%d", &A[i], &X[i], &B[i], &Y[i]);
--A[i];
--B[i];
if (X[i] > Y[i]) {
swap(A[i], B[i]);
swap(X[i], Y[i]);
}
}
vector<int> mxs(N, 0);
for (int i = 0; i < M; ++i) {
chmax(mxs[A[i]], X[i]);
chmax(mxs[B[i]], Y[i]);
}
Scc scc(N);
for (int i = 0; i < M; ++i) if (X[i] == 1 && Y[i] == 2) {
scc.ae(A[i], B[i]);
}
scc.run();
const auto uss = scc.group();
vector<int> indeg(scc.l, 0);
for (int i = 0; i < M; ++i) if (X[i] == 1 && Y[i] == 2) {
const int ra = scc[A[i]];
const int rb = scc[B[i]];
if (ra != rb) {
++indeg[rb];
}
}
vector<int> srcs;
for (int r = 0; r < scc.l; ++r) if (!indeg[r]) {
int um = uss[r][0];
for (const int u : uss[r]) {
if (mxs[u] == 2) {
um = u;
break;
}
}
srcs.push_back(um);
}
// cerr<<"uss = "<<uss<<", indeg = "<<indeg<<", srcs = "<<srcs<<endl;
G.assign(N, {});
for (int i = 0; i < M; ++i) if (X[i] == 1 && Y[i] == 2) {
G[A[i]].push_back(i);
}
vis.assign(N, 0);
is.clear();
for (const int src : srcs) {
dfs(src);
}
// cerr<<"is = "<<is<<endl;
vector<int> used(M, 0);
for (const int i : is) {
used[i] = 1;
}
vector<int> ans;
for (int i = 0; i < M; ++i) if (X[i] == 1 && Y[i] == 1) ans.push_back(i);
for (int i = 0; i < M; ++i) if (X[i] == 1 && Y[i] == 2 && !used[i]) ans.push_back(i);
for (const int i : is) ans.push_back(i);
for (int i = 0; i < M; ++i) if (X[i] == 2 && Y[i] == 2) ans.push_back(i);
assert((int)ans.size() == M);
vector<int> vals(N, 0);
for (const int i : ans) {
vals[A[i]] = X[i];
vals[B[i]] = Y[i];
}
int score = 0;
for (int u = 0; u < N; ++u) {
score += vals[u];
}
printf("%d\n", score);
for (int j = 0; j < M; ++j) {
if (j) printf(" ");
printf("%d", ans[j] + 1);
}
puts("");
}
#ifndef LOCAL
break;
#endif
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3592kb
input:
2 4 4 1 1 2 2 3 2 4 1 1 2 3 2 2 1 4 1 4 2 3 2 4 1 1 2 3 1
output:
7 4 2 1 3 5 2 1
result:
ok Correct. (2 test cases)
Test #2:
score: -100
Wrong Answer
time: 0ms
memory: 3676kb
input:
1 10 13 1 1 2 2 2 1 3 2 1 2 3 1 3 1 4 2 4 1 5 2 5 1 6 2 4 2 6 1 7 1 8 2 8 1 9 2 7 2 9 1 5 2 9 1 8 2 10 2 1 1 10 1
output:
18 13 3 4 5 10 7 6 11 9 8 2 1 12
result:
wrong answer Jury has better answer. Participant 18, jury 19 (test case 1)