QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#398706 | #6414. Classical Maximization Problem | Max_s_xaM | WA | 86ms | 18392kb | C++17 | 4.4kb | 2024-04-25 16:45:22 | 2024-04-25 16:45:23 |
Judging History
answer
#include <iostream>
#include <algorithm>
#include <vector>
typedef long long ll;
typedef double lf;
// #define DEBUG 1
struct IO
{
#define MAXSIZE (1 << 20)
#define isdigit(x) (x >= '0' && x <= '9')
char buf[MAXSIZE], *p1, *p2;
char pbuf[MAXSIZE], *pp;
#if DEBUG
#else
IO() : p1(buf), p2(buf), pp(pbuf) {}
~IO() {fwrite(pbuf, 1, pp - pbuf, stdout);}
#endif
#define gc() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, MAXSIZE, stdin), p1 == p2) ? ' ' : *p1++)
#define blank(x) (x == ' ' || x == '\n' || x == '\r' || x == '\t')
template <typename T>
void Read(T &x)
{
#if DEBUG
std::cin >> x;
#else
bool sign = 0; char ch = gc(); x = 0;
for (; !isdigit(ch); ch = gc())
if (ch == '-') sign = 1;
for (; isdigit(ch); ch = gc()) x = x * 10 + (ch ^ 48);
if (sign) x = -x;
#endif
}
void Read(char *s)
{
#if DEBUG
std::cin >> s;
#else
char ch = gc();
for (; blank(ch); ch = gc());
for (; !blank(ch); ch = gc()) *s++ = ch;
*s = 0;
#endif
}
void Read(char &c) {for (c = gc(); blank(c); c = gc());}
void Push(const char &c)
{
#if DEBUG
putchar(c);
#else
if (pp - pbuf == MAXSIZE) fwrite(pbuf, 1, MAXSIZE, stdout), pp = pbuf;
*pp++ = c;
#endif
}
template <typename T>
void Write(T x)
{
if (x < 0) x = -x, Push('-');
static T sta[35];
int top = 0;
do sta[top++] = x % 10, x /= 10; while (x);
while (top) Push(sta[--top] ^ 48);
}
template <typename T>
void Write(T x, char lst) {Write(x), Push(lst);}
} IO;
#define Read(x) IO.Read(x)
#define Write(x, y) IO.Write(x, y)
#define Put(x) IO.Push(x)
using namespace std;
const int MAXN = 2e5 + 10;
int n, ans, fr[MAXN], to[MAXN];
int m1, m2, tmp[MAXN];
vector <pair <int, int> > e[MAXN], res;
vector <int> pr[MAXN], cr, rem;
bool vis[MAXN], dir[MAXN], ind[MAXN];
bool b[MAXN];
int st[MAXN], top;
void DFS1(int u, int fa)
{
st[++top] = u;
vis[u] = 1;
for (auto [v, w] : e[u])
if (!vis[v]) ind[v] ^= 1, DFS1(v, u);
else if (!dir[w] && v != fa) ind[v] ^= 1, pr[v].push_back(w), dir[w] = 1;
}
void DFS2(int u)
{
for (auto [v, w] : e[u])
if (!dir[w])
{
dir[w] = 1, DFS2(v), b[u] ^= b[v];
if (b[v]) pr[u].push_back(w);
else pr[v].push_back(w);
}
}
inline void Solve(int u)
{
top = 0, DFS1(u, 0);
cr.clear();
for (int i = 1; i <= top; i++)
if (ind[st[i]]) cr.push_back(st[i]);
for (int i = 0; i + 1 < cr.size(); i += 2) b[cr[i]] ^= 1, b[cr[i + 1]] ^= 1;
DFS2(u);
}
int main()
{
#if DEBUG
#else
ios::sync_with_stdio(0), cin.tie(0);
#endif
int T;
Read(T);
while (T--)
{
Read(n);
for (int i = 1; i <= 2 * n; i++)
{
e[i].clear(), pr[i].clear();
vis[i] = dir[i] = ind[i] = b[i] = 0;
}
for (int i = 1; i <= n * 2; i++) Read(fr[i]), Read(to[i]), tmp[i] = fr[i];
sort(tmp + 1, tmp + n * 2 + 1), m1 = unique(tmp + 1, tmp + n * 2 + 1) - tmp - 1;
for (int i = 1; i <= n * 2; i++) fr[i] = lower_bound(tmp + 1, tmp + m1 + 1, fr[i]) - tmp;
for (int i = 1; i <= n * 2; i++) tmp[i] = to[i];
sort(tmp + 1, tmp + n * 2 + 1), m2 = unique(tmp + 1, tmp + n * 2 + 1) - tmp - 1;
for (int i = 1; i <= n * 2; i++) to[i] = lower_bound(tmp + 1, tmp + m2 + 1, to[i]) - tmp;
for (int i = 1, u, v; i <= n * 2; i++)
{
u = fr[i], v = to[i];
e[u].emplace_back(v + m1, i), e[v + m1].emplace_back(u, i);
}
for (int i = 1; i <= m1 + m2; i++)
if (!vis[i]) Solve(i);
rem.clear(), ans = 0, res.clear();
for (int i = 1; i <= m1 + m2; i++)
{
ans += pr[i].size() / 2;
for (int j = 0; j + 1 < pr[i].size(); j += 2)
res.emplace_back(pr[i][j], pr[i][j + 1]);
if (pr[i].size() & 1) rem.push_back(pr[i].back());
}
for (int i = 0; i + 1 < rem.size(); i += 2)
res.emplace_back(rem[i], rem[i + 1]);
cout << ans << '\n';
for (auto [u, v] : res) cout << u << ' ' << v << '\n';
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 18000kb
input:
3 2 0 0 0 1 1 0 1 1 2 0 0 0 1 0 2 0 3 2 0 0 1 1 2 2 3 3
output:
2 2 1 4 3 2 1 2 3 4 0 1 2 3 4
result:
ok ok (3 test cases)
Test #2:
score: -100
Wrong Answer
time: 86ms
memory: 18392kb
input:
10000 2 -107276936 -310501829 419434212 585811870 -65754386 -491212232 381152038 897148193 3 -474045168 493506332 299114415 540203303 165808153 983551 -506936261 -694189769 766718170 -725540031 975267148 -593051087 1 -818952276 -762387923 584023914 -612401389 6 -77701228 -266484128 659434465 6322062...
output:
0 3 1 2 4 2 2 5 4 4 6 3 1 2 0 1 2 0 6 10 12 1 3 4 5 7 8 9 2 11 10 12 7 1 6 3 1 4 14 5 3 7 12 8 10 9 13 2 11 11 2 5 8 4 9 0 1 2 0 25 22 39 10 31 18 33 54 51 41 66 12 11 29 60 48 3 62 19 2 57 43 46 4 37 61 9 15 36 56 59 24 64 1 34 32 26 63 21 5 6 53 47 16 52 30 38 58 27 28 13 23 14 55 40 65 35 20 17 4...
result:
wrong answer pair 2 is 4=4 (test case 2)