QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#873742#4809. Maximum RangezhangmingzuWA 34ms56576kbC++143.6kb2025-01-26 21:47:362025-01-26 21:47:37

Judging History

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

  • [2025-01-26 21:47:37]
  • 评测
  • 测评结果:WA
  • 用时:34ms
  • 内存:56576kb
  • [2025-01-26 21:47:36]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
int n, m, s, t;
const int maxn = 1e6 + 5;
struct node
{
    int id, nxt, flow;
} edge[maxn << 1];
int hd[maxn], len = 1, dep[maxn], cur[maxn], dfn[maxn], low[maxn], flag[maxn], vis[maxn], w[maxn], mx = -1, mi, x[maxn], y[maxn], cnt, maxx, minn, siz, tmx, tmn;
vector<pair<int, int>> vec[maxn], os;
int mxx, mnn;
void add(int x, int y, int f)
{
    edge[++len].id = y;
    edge[len].nxt = hd[x];
    edge[len].flow = f;
    hd[x] = len;
}
void Add(int x, int y, int f)
{
	add(x, y, f);
	add(y, x, 0);
}
int bfs()
{
    queue<int> q;
    for (int i = 1; i <= cnt; i++) dep[i] = 0;
    dep[s] = 1;
    q.emplace(s);
    while (!q.empty())
    {
        int tmp = q.front();
        q.pop();
        for (int i = hd[tmp]; i; i = edge[i].nxt)
            if (!dep[edge[i].id] && edge[i].flow)
                dep[edge[i].id] = dep[tmp] + 1, q.emplace(edge[i].id);
    }
    return dep[t];
}
int dinic(int now, int flow)
{
    if (!flow) return 0;
    if (now == t) return flow;
    int tmp = 0;
    for (int &i = cur[now]; i; i = edge[i].nxt)
        if (dep[edge[i].id] == dep[now] + 1 && edge[i].flow)
        {
            int tmp2 = dinic(edge[i].id, min(flow, edge[i].flow));
            edge[i].flow -= tmp2, flow -= tmp2, tmp += tmp2, edge[i ^ 1].flow += tmp2;
            if (!flow) return tmp;
        }
    return tmp;
}
void tarjan(int now, int fa)
{
	dfn[now] = low[now] = ++cnt;
	for (auto j : vec[now])
	{
		int i = j.first;
		if (i != fa)
		{
			if (!dfn[i])
			{
				tarjan(i, now);
				low[now] = min(low[now], low[i]);
				if (low[i] == dfn[i])
					flag[j.second] = 1;
			}
			else low[now] = min(low[now], dfn[i]);
		}
	}
}
void dfs(int now)
{
	siz++;
	vis[now] = 1;
	for (auto i : vec[now])
	{
		if (flag[i.second]) continue;
		maxx = max(maxx, w[i.second]), minn = min(minn, w[i.second]);
		if (!vis[i.first])
			dfs(i.first);
	}
}
int id[maxn];
void gt(int now, int fa)
{
	vis[now] = 1;
	for (auto i : vec[now])
	{
		if (i.first == fa || flag[i.second]) continue;
		if (!mxx && w[i.second] == tmx) mxx = i.second;
		else if (!mnn && w[i.second] == tmn) mnn = i.second;
		cnt++;
		add(now, cnt, 1);
		os.emplace_back(len, i.second);
		add(cnt, now, 1);
		os.emplace_back(len, i.second);
		add(cnt, i.first, 1);
		os.emplace_back(len, i.second);
		add(i.first, cnt, 1);
		os.emplace_back(len, i.second);
		id[i.second] = cnt;
		if (!vis[i.first])
			gt(i.first, now);
	}
}
vector<int> so;
void ga(int now)
{
	if (now <= n) so.emplace_back(now);
	for (int i = hd[now]; i; i = edge[i].nxt)
		if (!edge[i].flow)
			return edge[i].flow = 1, ga(edge[i].id);
}
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    cin >> n >> m;
    for (int i = 1; i <= m; i++)
    {
    	int u, v;
    	cin >> u >> v >> w[i];
    	x[i] = u, y[i] = v;
    	vec[u].emplace_back(v, i);
    	vec[v].emplace_back(u, i);
	}
	tarjan(1, 0);
	for (int i = 1; i <= n; i++)
		if (!vis[i])
		{
			minn = 1e9, maxx = -1e9, siz = 0;
			dfs(i);
			if (siz != 1 && maxx - minn > mx) mx = maxx - minn, tmx = maxx, tmn = minn, mi = i;
		}
	cout << mx << '\n';
	cnt = n;
	memset(vis, 0, sizeof(vis));
	gt(mi, 0);
	s = id[mnn], t = id[mxx];
//	if (n >= 100)
//	cout << tmx << ' ' << tmn << ' ' << mi << ' ' << mnn << ' ' << mxx << '\n';
	bfs();
	for (int i = 1; i <= cnt; i++) cur[i] = hd[i];
	dinic(s, 1);
	bfs();
	for (int i = 1; i <= cnt; i++) cur[i] = hd[i];
	dinic(s, 1);
	ga(s);
	reverse(so.begin(), so.end());
	ga(s);
	cout << so.size() << '\n';
	for (int i : so) cout << i << ' ';
    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 4ms
memory: 46516kb

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
3 1 5 4 

result:

ok ok

Test #2:

score: 0
Accepted
time: 32ms
memory: 54080kb

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: 33ms
memory: 56576kb

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

result:

ok ok

Test #4:

score: 0
Accepted
time: 30ms
memory: 54504kb

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: 34ms
memory: 55116kb

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
22
6675 32701 89787 8975 26922 21107 93559 75593 83884 20678 16385 62720 25891 75176 34179 64174 38274 8778 99809 28745 6675 60056 

result:

wrong answer Cycle contains repeated edge 6675-60056