QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#404650 | #4805. Grammy Sorting | zhaohaikun | WA | 0ms | 3776kb | C++20 | 2.7kb | 2024-05-04 12:27:36 | 2024-05-04 12:27:37 |
Judging History
answer
// MagicDark
#include <bits/stdc++.h>
#define debug cerr << "\033[32m[" << __LINE__ << "]\033[0m "
#define SZ(x) ((int) x.size() - 1)
#define all(x) x.begin(), x.end()
#define ms(x, y) memset(x, y, sizeof x)
#define F(i, x, y) for (int i = (x); i <= (y); i++)
#define DF(i, x, y) for (int i = (x); i >= (y); i--)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
template <typename T> T& chkmax(T& x, T y) {return x = max(x, y);}
template <typename T> T& chkmin(T& x, T y) {return x = min(x, y);}
template <typename T> T& read(T &x) {
x = 0; int f = 1; char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
return x *= f;
}
const int N = 1010;
int n, m, s, t, p[N], low[N], dfn[N], dd[N], dfscnt, fa[N], rk[N];
bool vis[N];
vector <int> v[N], h[N], line, g1[N], g2[N];
bool tarjan(int x) {
bool flag = (x == t);
dd[low[x] = dfn[x] = ++dfscnt] = x;
for (int i: v[x])
if (!dfn[i]) {
fa[i] = x;
flag |= tarjan(i);
chkmin(low[x], low[i]);
} else chkmin(low[x], dfn[i]);
if (flag) line.push_back(x);
else {
if (low[x] == dfn[fa[x]]) {
cout << -1;
exit(0);
}
h[fa[x]].push_back(x);
h[dd[low[x]]].push_back(x);
}
return flag;
}
vector <int> ans;
void dfs(int x) {
if (vis[x]) return;
vis[x] = true;
ans.push_back(x);
for (int i: h[x]) dfs(i);
}
int qq(int x) {
// if (x == n) return n + 1;
// int mx = n;
pair <int, int> mx = make_pair(n + 1, 0);
for (int i: g1[x]) chkmin(mx, make_pair(p[i], i));
return mx.second;
}
int y[N];
signed main() {
read(n), read(m), read(s), read(t);
F(i, 1, n) read(p[i]);
F(i, 1, m) {
int x, y; read(x), read(y);
v[x].push_back(y);
v[y].push_back(x);
}
tarjan(s);
reverse(all(line));
for (int i: line) dfs(i);
assert(ans.size() == n);
F(i, 1, n) rk[ans[i - 1]] = i;
F(i, 1, n) cout << ans[i - 1] << " "; cout << endl;
F(i, 1, n)
for (int j: v[i])
if (rk[j] > rk[i]) {
g1[i].push_back(j);
g2[j].push_back(i);
}
// DF(i, SZ)
// p[n + 1] = n + 1;
p[0] = n + 1;
cout << n - 1 << '\n';
DF(i, n - 1, 1) {
int x = ans[i - 1], t = x;
vector <int> line;
while (t != s) line.push_back(t = g2[t][0]);
reverse(all(line));
while (true) {
line.push_back(x);
int w = qq(x);
if (p[s] < p[w]) break;
// debug << s << " " << p[s] << " " << x << " " << w << endl;
x = w;
}
cout << line.size() << ' ';
for (int j: line) cout << j << ' ';
F(j, 0, SZ(line)) y[j] = p[line[j]];
F(j, 0, SZ(line)) p[line[j]] = y[(j + 1) % line.size()];
cout << '\n';
}
// F(i, 1, n) cout << p[i] << " "; cout << endl;
return 0;
}
/* why?
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 3776kb
input:
5 6 1 2 1 2 3 4 5 1 3 2 3 1 4 2 4 1 5 3 5
output:
1 4 5 3 2 4 2 1 3 4 1 5 3 2 3 1 4 2 3 1 5 3
result:
wrong answer a_1 != A at operation #1