在UOJ上怎么改时限都是97分,挂在Hack的第9个点和Hack的第12个点上。
# include <bits/stdc++.h>
using namespace std;
namespace IO {
struct __INPUT {
#define SIZE 1000000
inline char gc() { static streambuf *inbuf = cin.rdbuf(); return (p1 == p2 && (p2 = (p1 = buf) + inbuf->sgetn(buf, SIZE), p1 == p2) ? EOF : *p1++); }
inline bool isdigit(char ch) { return ch >= '0' && ch <= '9'; } char buf[SIZE], *p1, *p2;
template <typename T> inline bool read(T &x) { static streambuf *inbuf = cin.rdbuf(); x = 0; int f = 0, flag = false; char ch = gc(); while (!isdigit(ch)) { if (ch == '-') f = 1; ch = gc(); } if (isdigit(ch)) x = x * 10 + ch - '0', ch = gc(), flag = true; while (isdigit(ch)) x = (x << 3) + (x << 1) + (ch ^ 48), ch = gc(); x = f ? -x : x; return flag; }
template <typename T, typename... Args> inline bool read(T &a, Args &...args) { return read(a) && read(args...); }
FILE *in; __INPUT(FILE *in) : in(in) {}
template <typename T> __INPUT &operator>>(T &x) { read(x); return *this; }
__INPUT &operator>>(char &x) { x = ' '; for (; x <= ' ';) x = gc(); return *this; }
__INPUT &operator>>(char *x) { int i = 0; char c = ' '; for (; c <= ' ';) c = gc(); for (; c > ' ';) x[i++] = c, c = gc(); x[i] = 0; return *this; }
__INPUT &operator>>(string &x) { x.clear(); char c = ' '; for (; c <= ' ';) c = gc(); for (; c > ' ';) x += c, c = gc(); return *this; }
} ein(stdin);
struct __OUTPUT {
#define endl '\n'
template <typename T> inline void write(T x) { static streambuf *outbuf = cout.rdbuf(); static char stack[21]; static int top = 0; if (x < 0) outbuf->sputc('-'), x = -x; if (!x) { outbuf->sputc('0'); return; } while (x) stack[++top] = x % 10 ^ 48, x /= 10; while (top) outbuf->sputc(stack[top]), --top;}
inline void putc(const char ch) { static streambuf *outbuf = cout.rdbuf(); outbuf->sputc(ch); }
template <typename T> inline void write(const char ch, T x) { static streambuf *outbuf = cout.rdbuf(); static char stack[21]; static int top = 0; if (x < 0) outbuf->sputc('-'), x = -x; if (!x) { outbuf->sputc('0'), outbuf->sputc(ch); return; } while (x) stack[++top] = x % 10 ^ 48, x /= 10; while (top) outbuf->sputc(stack[top]), --top; outbuf->sputc(ch); }
template <typename T, typename... Args> inline void write(T a, Args... args) { write(a), write(args...); }
template <typename T, typename... Args> inline void write(const char ch, T a, Args... args) { write(ch, a), write(ch, args...); }
FILE *out; __OUTPUT(FILE *out) : out(out) {} template <typename T> __OUTPUT &operator<<(T x) { write(x); return *this;}
__OUTPUT &operator<<(const char x) { static streambuf *outbuf = cout.rdbuf(); outbuf->sputc(x); return *this;}
__OUTPUT &operator<<(const char *x) { static streambuf *outbuf = cout.rdbuf(); for (int i = 0; x[i]; ++i) outbuf->sputc(x[i]); return *this; }
__OUTPUT &operator<<(const string x) { static streambuf *outbuf = cout.rdbuf(); for (char c : x) outbuf->sputc(c); return *this; }
} eout(stdout);
} // namespace IO
using namespace IO;
mt19937 mrand(random_device{}());
# define ll long long
# define N 100005
int n, m, d, dfn[N], low[N], ins[N], bel[N], cnt = 0, Tm = 0;
stack<int> St;
vector<int> G[N];
string str, ans;
vector<array<int, 4> > Con;
inline void Tarjan (int u) {
dfn[u] = low[u] = ++Tm;
ins[u] = 1;
St.push(u);
for (int v : G[u]) {
if (!dfn[v]) {
Tarjan(v);
low[u] = min (low[u], low[v]);
} else if (ins[v]) {
low[u] = min (low[u], dfn[v]);
}
}
if (low[u] == dfn[u]) {
++cnt;
while (1) {
int v = St.top();
St.pop();
bel[v] = cnt;
ins[v] = 0;
if (v == u) {
break;
}
}
}
}
inline bool check () {
for (int i = 0; i < (n << 1); ++i) {
G[i].clear ();
dfn[i] = 0;
}
Tm = cnt = 0;
auto idx = [&](int x, int y) {
if (str[x] == ('a' + y % 3)) return -1;
else if (str[x] == ('a' + (y + 1) % 3)) return 2 * x;
else if (str[x] == ('a' + (y + 2) % 3)) return 2 * x + 1;
assert (0);
return -1;
};
for (const auto& [i, hi, j, hj] : Con) {
int f1 = idx(i, hi);
int f2 = idx(j, hj);
if ((~f1) && (~f2)) {
G[f1].push_back(f2);
G[f2 ^ 1].push_back(f1 ^ 1);
} else if ((~f1) && (!(~f2))) {
G[f1].push_back(f1 ^ 1);
}
}
for (int i = 0; i < (n << 1); ++i) {
if (!dfn[i]) {
Tarjan(i);
}
}
for (int i = 0; i < n; ++i) {
if (bel[i << 1] == bel[i << 1 | 1]) {
return 0;
}
}
for (int i = 0; i < n; ++i) {
ans += ((str[i] - 'a' + 1) + (int)(bel[i << 1] < bel[i << 1 | 1])) % 3 + 'A';
}
eout << ans << endl;
return 1;
}
signed main() {
ios_base::sync_with_stdio(0), cin.tie(nullptr), cout.tie(nullptr);
ein >> n >> d >> str;
ein >> m;
vector<int> X;
for (int i = 1, u, v; i <= m; ++i) {
char c1, c2;
ein >> u >> c1 >> v >> c2;
--u, --v;
Con.push_back(array<int, 4> {u, c1 - 'A', v, c2 - 'A'});
}
for (int i = 0; i < n; ++i) {
if (str[i] == 'x') {
X.push_back (i);
}
}
if (d <= 7 || ((1 << d) * (n + m) <= (int)5e7)) {
for (int S = 0; S < (1 << d); ++S) {
for (int i = 0; i < d; ++i) {
str[X[i]] = ((S >> i) & 1) + 'a';
}
if (check()) {
return 0;
}
}
eout << "-1\n";
return 0;
} else {
while (clock() <= 0.90 * CLOCKS_PER_SEC) {
for (int i = 0; i < d; ++i)
str[X[i]] = "abc"[mrand() % 3];
if (check()) {
return 0;
}
}
eout << "-1\n";
return 0;
}
}
我调成了0.9s也会挂,不知道为什么。。。
大佬求卡!!!!
代码可能会有亿点点丑陋。。。