QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#497895 | #7671. Metal Processing Plant | Max_s_xaM | WA | 45ms | 6144kb | C++14 | 6.1kb | 2024-07-29 20:07:44 | 2024-07-29 20:07:44 |
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 <= 2) 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: 0ms
memory: 3748kb
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: 3740kb
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: 3656kb
input:
1
output:
0
result:
ok single line: '0'
Test #4:
score: 0
Accepted
time: 0ms
memory: 3624kb
input:
2 1
output:
0
result:
ok single line: '0'
Test #5:
score: 0
Accepted
time: 0ms
memory: 3744kb
input:
2 1000000000
output:
0
result:
ok single line: '0'
Test #6:
score: 0
Accepted
time: 0ms
memory: 3620kb
input:
3 1000000000 1000000000 1000000000
output:
1000000000
result:
ok single line: '1000000000'
Test #7:
score: 0
Accepted
time: 0ms
memory: 3672kb
input:
4 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000
output:
1000000000
result:
ok single line: '1000000000'
Test #8:
score: 0
Accepted
time: 0ms
memory: 3744kb
input:
3 78 97 24
output:
24
result:
ok single line: '24'
Test #9:
score: -100
Wrong Answer
time: 45ms
memory: 6144kb
input:
200 202018752 202018795 202018793 100018905 202018758 202018741 202018727 202018766 202018728 100018879 202018781 100018860 202018785 100018841 100018910 100018836 100018883 100018847 202018734 202018775 100018830 100018901 202018773 202018754 202018737 100018843 202018788 202018778 202018777 202018...
output:
201039697
result:
wrong answer 1st lines differ - expected: '201024848', found: '201039697'