QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#863628#4809. Maximum RangezltWA 50ms24368kbC++144.8kb2025-01-19 20:23:092025-01-19 20:23:10

Judging History

你现在查看的是最新测评结果

  • [2025-01-19 20:23:10]
  • 评测
  • 测评结果:WA
  • 用时:50ms
  • 内存:24368kb
  • [2025-01-19 20:23:09]
  • 提交

answer

#include <bits/stdc++.h>
#define pb emplace_back
#define fst first
#define scd second
#define mkp make_pair
#define uint unsigned
#define mems(a, x) memset((a), (x), sizeof(a))

using namespace std;
typedef long long ll;
typedef double db;
typedef unsigned long long ull;
typedef long double ldb;
typedef pair<int, int> pii;

const int maxn = 100100;
const int maxm = 1000100;
const int inf = 0x3f3f3f3f;

int n, m, dfn[maxn], low[maxn], tim, scc[maxn], cnt, stk[maxn], top;
pii f[maxn], g[maxn];

struct node {
	int u, v, d;
} a[maxn];

vector<int> G[maxn];

void dfs(int u, int fa) {
	dfn[u] = low[u] = ++tim;
	stk[++top] = u;
	for (int i : G[u]) {
		int v = u ^ a[i].u ^ a[i].v;
		if (i == fa) {
			continue;
		}
		if (!dfn[v]) {
			dfs(v, i);
			low[u] = min(low[u], low[v]);
		} else {
			low[u] = min(low[u], dfn[v]);
		}
	}
	if (dfn[u] == low[u]) {
		++cnt;
		while (1) {
			int x = stk[top--];
			scc[x] = cnt;
			if (x == u) {
				break;
			}
		}
	}
}

int nt, S, T, hd[maxm], len = 1;
struct edge {
	int to, next, cap, flow;
} edges[maxm];

inline void add_edge(int u, int v, int c, int f) {
	edges[++len].to = v;
	edges[len].next = hd[u];
	edges[len].cap = c;
	edges[len].flow = f;
	hd[u] = len;
}

struct Dinic {
	int d[maxn], cur[maxn];
	bool vis[maxn];
	
	inline void add(int u, int v, int c) {
		add_edge(u, v, c, 0);
		add_edge(v, u, 0, 0);
	}
	
	inline bool bfs() {
		queue<int> q;
		for (int i = 1; i <= nt; ++i) {
			vis[i] = 0;
			d[i] = -1;
		}
		q.push(S);
		vis[S] = 1;
		d[S] = 0;
		while (q.size()) {
			int u = q.front();
			q.pop();
			for (int i = hd[u]; i; i = edges[i].next) {
				edge &e = edges[i];
				if (!vis[e.to] && e.cap > e.flow) {
					vis[e.to] = 1;
					d[e.to] = d[u] + 1;
					q.push(e.to);
				}
			}
		}
		return vis[T];
	}
	
	int dfs(int u, int a) {
		if (u == T || !a) {
			return a;
		}
		int flow = 0, f;
		for (int &i = cur[u]; i; i = edges[i].next) {
			edge &e = edges[i];
			if (d[e.to] == d[u] + 1 && (f = dfs(e.to, min(a, e.cap - e.flow))) > 0) {
				e.flow += f;
				edges[i ^ 1].flow -= f;
				flow += f;
				a -= f;
				if (!a) {
					break;
				}
			}
		}
		return flow;
	}
	
	inline int solve() {
		int flow = 0;
		while (bfs()) {
			for (int i = 1; i <= nt; ++i) {
				cur[i] = hd[i];
			}
			flow += dfs(S, inf);
		}
		return flow;
	}
} solver;

int b[maxn], c[maxn], t1, t2, d[maxn];

inline void bfs() {
	queue<int> q;
	q.push(S);
	mems(d, -1);
	d[S] = 0;
	while (q.size()) {
		int u = q.front();
		q.pop();
		for (int i = hd[u]; i; i = edges[i].next) {
			edge &e = edges[i];
			if (e.cap == 1 && e.flow == 1 && d[e.to] == -1) {
				d[e.to] = d[u] + 1;
				q.push(e.to);
			}
		}
	}
	assert(d[T] != -1);
}

void dfs2(int u, int fa) {
	if (u <= n) {
		b[++t1] = u;
	}
	for (int i = hd[u]; i; i = edges[i].next) {
		int v = edges[i].to;
		if (v == fa || !edges[i].flow || d[v] != d[u] + 1) {
			continue;
		}
		edges[i].flow = 0;
		dfs2(v, u);
		break;
	}
}

void dfs3(int u, int fa) {
	if (u <= n) {
		c[++t2] = u;
	}
	for (int i = hd[u]; i; i = edges[i].next) {
		int v = edges[i].to;
		if (v == fa || !edges[i].flow || d[v] != d[u] + 1) {
			continue;
		}
		edges[i].flow = 0;
		dfs3(v, u);
		break;
	}
}

void solve() {
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= m; ++i) {
		scanf("%d%d%d", &a[i].u, &a[i].v, &a[i].d);
		G[a[i].u].pb(i);
		G[a[i].v].pb(i);
	}
	dfs(1, -1);
	for (int i = 1; i <= cnt; ++i) {
		f[i] = mkp(-inf, 0);
		g[i] = mkp(inf, 0);
	}
	for (int u = 1; u <= n; ++u) {
		for (int i : G[u]) {
			int v = u ^ a[i].u ^ a[i].v, d = a[i].d;
			if (scc[u] == scc[v]) {
				f[scc[u]] = max(f[scc[u]], mkp(d, i));
				g[scc[u]] = min(g[scc[u]], mkp(d, i));
			}
		}
	}
	int mx = -inf, p = -1;
	for (int i = 1; i <= cnt; ++i) {
		int v = f[i].fst - g[i].fst;
		if (v > mx) {
			mx = v;
			p = i;
		}
	}
	printf("%d\n", mx);
	S = n + 1;
	T = nt = n + 2;
	int x = f[p].scd, y = g[p].scd;
	solver.add(S, a[x].u, 1);
	solver.add(S, a[x].v, 1);
	solver.add(a[y].u, T, 1);
	solver.add(a[y].v, T, 1);
	for (int u = 1; u <= n; ++u) {
		for (int i : G[u]) {
			if (i == x || i == y) {
				continue;
			}
			int v = u ^ a[i].u ^ a[i].v;
			if (u < v) {
				solver.add(u, v, 1);
				solver.add(v, u, 1);
			}
		}
	}
	solver.solve();
	bfs();
	dfs2(S, -1);
	bfs();
	dfs3(S, -1);
	// assert(b[t1] == a[y].u || b[t1] == a[y].v);
	// assert(c[t2] == a[y].u || c[t2] == a[y].v);
	printf("%d\n", t1 + t2);
	reverse(c + 1, c + t2 + 1);
	for (int i = 1; i <= t1; ++i) {
		printf("%d ", b[i]);
	}
	for (int i = 1; i <= t2; ++i) {
		printf("%d%c", c[i], " \n"[i == t2]);
	}
}

int main() {
	int T = 1;
	// scanf("%d", &T);
	while (T--) {
		solve();
	}
	return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 10472kb

input:

5 7
1 2 1
1 3 -2
2 3 1
3 4 3
4 5 1
1 5 -1
2 5 2

output:

5
4
4 5 1 3

result:

ok ok

Test #2:

score: 0
Accepted
time: 44ms
memory: 24328kb

input:

99997 100000
12238 99016 352755196
99016 25485 -412473602
25485 2440 991507552
2440 31171 -181894654
36970 2440 -800167579
2440 41865 -148191946
96629 31171 847888506
36970 95740 395546542
27992 2440 647886610
99016 29557 369124914
80795 27992 -673871966
36970 3509 573208857
57672 29557 874406776
41...

output:

1959330954
37
96048 4697 44442 68883 69259 57672 29557 99016 25485 2440 31171 95092 34883 46301 96778 37694 88289 30288 68523 54073 84997 89628 67966 84407 3463 72825 51491 87712 96230 22074 72089 76022 86665 92617 74677 86274 94991

result:

ok ok

Test #3:

score: 0
Accepted
time: 40ms
memory: 24368kb

input:

99997 100000
41884 21178 -431811360
41884 42699 -450057006
36523 21178 582079730
21178 96679 615552614
63637 21178 498974417
96679 5108 235820276
75058 41884 220112636
35148 42699 589595309
36523 18002 -637739861
65854 5108 -312755792
45137 41884 -511118771
5108 31311 554050951
25335 35148 -28341059...

output:

1968439328
40
23692 87673 42699 41884 45137 85192 38202 83711 83919 55330 71151 98733 99716 70298 33264 26071 90144 25926 52252 51434 69337 7577 5108 50088 6204 28694 41126 87303 83047 26981 54901 59612 14678 35287 78274 18331 89860 71024 99686 98098

result:

ok ok

Test #4:

score: 0
Accepted
time: 50ms
memory: 24336kb

input:

99984 99999
33974 29867 335681778
33974 87468 348956829
83048 87468 320849805
29867 69456 -424530698
72457 69456 -950650074
53838 83048 755969166
85914 69456 569454441
51728 87468 -202158773
15970 29867 -865071002
15970 94894 697607001
94894 74694 616318126
33974 11496 -89287579
53838 34365 -6577379...

output:

1985932414
36
50114 35807 13570 79841 32897 75496 85914 55808 57640 58540 79605 55857 61993 46598 303 11395 92249 27866 23026 83298 1652 76013 29864 53838 83048 87468 51728 75902 5449 70238 97838 83656 91542 28078 78849 80694

result:

ok ok

Test #5:

score: -100
Wrong Answer
time: 46ms
memory: 22748kb

input:

99988 99992
8584 11873 -811540160
68064 11873 -930246087
11873 60056 916668870
68064 82193 -859062523
60056 75072 790866030
27767 75072 357619485
75072 78221 411650300
39636 82193 264106928
6675 60056 933851261
71747 78221 -508471038
11873 92771 -665232168
34402 27767 -906494982
11873 42714 63734230...

output:

1932268861
25
60056 75072 91957 3186 29873 5185 88236 50628 2518 83283 23798 75593 83884 20678 16385 62720 25891 75176 34179 64174 38274 8778 99809 28745 6675

result:

wrong answer Edge 23798-75593 does not appear in original graph