题目传送门
判断混合图中是否存在欧拉回路
#include
#define N 1010
using namespace std;
template <typename node> void chkmax(node &x, node y) {x = max(x, y);}
template <typename node> void chkmin(node &x, node y) {x = min(x, y);}
template <typename node> void read(node &x) {
x = 0; int f = 1; char c = getchar();
while (!isdigit(c)) {if (c == '-') f = -1; c = getchar();}
while (isdigit(c)) x = x * 10 + c - '0', c = getchar(); x *= f;
}
struct Node {
int x, y, v;
} a[N * 4];
struct Edge {
int next, num, c;
} e[N * N];
int n, m, s, t, cnt, l[N], cur[N], sum[N], vis[N * 4];
void add(int x, int y, int c) {
e[++cnt] = (Edge) {e[x].next, y, c};
e[x].next = cnt;
}
void Add(int x, int y, int c) {
add(x, y, c), add(y, x, 0);
}
bool bfs(int s) {
for (int i = 1; i <= t; i++) l[i] = -1;
queue <int> q; q.push(s);
while (!q.empty()) {
int x = q.front(); q.pop();
for (int p = e[x].next; p; p = e[p].next) {
int k = e[p].num, c = e[p].c;
if (c && l[k] == -1)
q.push(k), l[k] = l[x] + 1;
}
}
return l[t] != -1;
}
int dfs(int x, int lim) {
if (x == t) return lim;
int used = 0;
for (int p = cur[x]; p; p = e[p].next) {
int k = e[p].num, c = e[p].c;
if (c && l[k] == l[x] + 1) {
int w = dfs(k, min(c, lim - used));
e[p].c -= w, e[p ^ 1].c += w, used += w;
if (e[p].c) cur[x] = p;
if (used == lim) return lim;
}
}
if (!used) l[x] = -1; return used;
}
int dinic() {
int ret = 0;
while (bfs(s)) {
for (int i = 0; i <= t; i++) cur[i] = e[i].next;
ret += dfs(s, INT_MAX);
}
return ret;
}
bool check(int mid) {
memset(vis, 0, sizeof(vis));
memset(sum, 0, sizeof(sum));
s = 0, t = cnt = n + 1;
if (cnt % 2 == 0) cnt++;
for (int i = 0; i <= t; i++) e[i].next = 0;
for (int i = 2; i <= m; i++) {
if (a[i].v > mid) continue;
if (i % 2 == 0) sum[a[i].x]++, sum[a[i].y]--, vis[i ^ 1] = 1;
else if (vis[i]) Add(a[i].x, a[i].y, 1);
else sum[a[i].x]++, sum[a[i].y]--;
}
int tmp = 0;
for (int i = 1; i <= n; i++) {
if (abs(sum[i]) % 2 == 1) return false;
int x = abs(sum[i]) / 2;
if (sum[i] > 0) tmp += x, Add(i, t, x);
else Add(s, i, x);
}
return dinic() == tmp;
}
int main() {
read(n), read(m);
int tot = 1, l = INT_MAX, r = -l, ans = r;
for (int i = 1; i <= m; i++) {
int x, y, tx, ty;
read(x), read(y), read(tx), read(ty);
a[++tot] = (Node) {x, y, tx};
a[++tot] = (Node) {y, x, ty};
chkmin(l, min(tx, ty));
chkmax(r, max(tx, ty));
}
m = tot;
while (l <= r) {
int mid = (l + r) >> 1;
if (check(mid)) r = mid - 1, ans = mid;
else l = mid + 1;
}
if (ans == -INT_MAX) cout << "NIE\n";
else cout << ans << "\n";
return 0;
}