QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#497893 | #7671. Metal Processing Plant | Max_s_xaM | WA | 1ms | 5788kb | C++14 | 6.1kb | 2024-07-29 20:07:10 | 2024-07-29 20:07:12 |
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 = 210;
int n, d[MAXN][MAXN], ans;
struct Edge
{
int u, v, w;
bool operator < (const Edge u) const { return w > u.w; }
};
vector <Edge> e;
int fa[MAXN], col[MAXN]; bool vis[MAXN];
int Find(int x) { return x == fa[x] ? x : fa[x] = Find(fa[x]); }
inline void Union(int u, int v) { u = Find(u), v = Find(v); if (u != v) fa[u] = v; }
int st[MAXN], top;
vector <int> g[MAXN << 1];
int dfn[MAXN << 1], low[MAXN << 1], bel[MAXN << 1], scc, clk;
int stk[MAXN << 1], tot; bool inq[MAXN << 1];
inline void Tarjan(int u)
{
dfn[u] = low[u] = ++clk, stk[++tot] = u, inq[u] = 1;
for (auto v : g[u])
if (!dfn[v]) Tarjan(v), low[u] = min(low[u], low[v]);
else if (inq[v]) low[u] = min(low[u], dfn[v]);
if (low[u] == dfn[u])
{
scc++;
while (stk[tot] != u) bel[stk[tot]] = scc, inq[stk[tot]] = 0, tot--;
bel[u] = scc, inq[u] = 0, tot--;
}
}
inline bool Check()
{
for (int i = 1; i <= 2 * n; i++)
{
dfn[i] = low[i] = bel[i] = inq[i] = 0;
// cout << i << ": ";
// for (auto v : g[i]) cout << v << ' '; cout << '\n';
}
scc = clk = tot = 0;
for (int i = 1; i <= 2 * n; i++)
if (!dfn[i]) Tarjan(i);
for (int i = 1; i <= n; i++)
if (bel[i] == bel[i + n]) return 0;
return 1;
}
inline void Solve(int bg)
{
// cout << "Solve " << bg << "\n";
// for (int i = 0; i < bg; i++) cout << e[i].u << ' ' << e[i].v << " " << e[i].w << '\n';
// for (int i = 1; i <= n; i++) cout << col[i] << ' '; cout << '\n';
top = 0;
for (int i = 1; i <= n; i++) g[i].clear(), g[i + n].clear();
for (int i = 1; i <= n; i++)
if (vis[i] && fa[i] == i) st[++top] = i;
int l = bg, r = e.size() - 1, mid;
while (l <= r)
{
mid = l + r >> 1;
for (int i = 1; i <= n; i++) g[i].clear(), g[i + n].clear();
int x = Find(e[bg - 1].u), y = col[e[bg - 1].u];
if (vis[x]) g[x + (y ? 0 : n)].push_back(x + (y ? n : 0));
x = Find(e[bg - 1].v), y = col[e[bg - 1].v];
if (vis[x]) g[x + (y ? 0 : n)].push_back(x + (y ? n : 0));
for (int i = bg; i <= mid; i++)
{
int u = e[i].u, v = e[i].v, w = e[i].w;
if (Find(u) == Find(v))
{
// cout << u << ' ' << v << " ##\n";
if (col[u] != col[v]) continue;
u = Find(u);
g[u + (col[u] ? 0 : n)].push_back(u + (col[u] ? n : 0));
continue;
}
if (!vis[u] || !vis[v]) continue;
x = col[u], y = col[v], u = Find(u), v = Find(v);
g[u + (x ? n : 0)].push_back(v + (y ? 0 : n));
g[v + (y ? n : 0)].push_back(u + (x ? 0 : n));
}
// cout << "check " << mid << "\n";
if (Check()) l = mid + 1;
else r = mid - 1;
}
// cout << l << "\n";
if (l == e.size()) ans = min(ans, e[bg - 1].w);
else ans = min(ans, e[bg - 1].w + e[l].w);
}
int main()
{
// freopen("A.in", "r", stdin);
// freopen("A.out", "w", stdout);
#if DEBUG
#else
ios::sync_with_stdio(0), cin.tie(0);
#endif
Read(n);
for (int i = 1; i <= n; i++)
for (int j = i + 1; j <= n; j++)
Read(d[i][j]), d[j][i] = d[i][j], e.push_back(Edge{i, j, d[i][j]});
if (n == 1) return cout << "0\n", 0;
sort(e.begin(), e.end());
// for (int i = 0; i < e.size(); i++) cout << e[i].u << ' ' << e[i].v << " " << e[i].w << '\n';
ans = e[0].w;
for (int i = 1; i <= n; i++) fa[i] = i;
for (int _ = 0; _ < e.size(); _++)
{
int u = e[_].u, v = e[_].v;
if (Find(u) == Find(v))
{
if (col[u] == col[v]) { Solve(_ + 1); break; }
continue;
}
vis[u] = vis[v] = 1;
Solve(_ + 1);
if (col[u] == col[v])
{
for (int i = 1; i <= n; i++)
if (Find(i) == Find(v)) col[i] ^= 1;
}
Union(u, v);
// cout << "add " << u << ' ' << v << '\n';
// for (int i = 1; i <= n; i++) cout << col[i] << ' '; cout << '\n';
}
cout << ans << '\n';
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 5704kb
input:
5 4 5 0 2 1 3 7 2 0 4
output:
4
result:
ok single line: '4'
Test #2:
score: 0
Accepted
time: 0ms
memory: 3728kb
input:
7 1 10 5 5 5 5 5 10 5 5 5 100 100 5 5 10 5 5 98 99 3
output:
15
result:
ok single line: '15'
Test #3:
score: 0
Accepted
time: 0ms
memory: 3652kb
input:
1
output:
0
result:
ok single line: '0'
Test #4:
score: -100
Wrong Answer
time: 1ms
memory: 5788kb
input:
2 1
output:
1
result:
wrong answer 1st lines differ - expected: '0', found: '1'