QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#820875#2893. 独立no_zzz_is_wwbndAC ✓87ms52136kbC++143.4kb2024-12-19 09:04:192024-12-19 09:04:20

Judging History

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

  • [2024-12-19 09:04:20]
  • 评测
  • 测评结果:AC
  • 用时:87ms
  • 内存:52136kb
  • [2024-12-19 09:04:19]
  • 提交

answer

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <iostream>
#include <vector>
#include <map>
#include <queue>
#include <cassert>

using namespace std;

#define int long long
#define PII pair<int,int>
#define x first
#define y second
#define For(i, l, r) for(int i = l; i <= r; i ++)
#define Rep(i, l, r) for(int i = l; i >= r; i --)

bool START;

void in(int &x)
{
	char c = getchar(); int op = 1;
	while(c > '9' || c < '0') op *= (c == '-' ? -1 : 1), c = getchar();
	for(x = 0; c >= '0' && c <= '9'; c = getchar()) x = x * 10 + c - '0'; x *= op;
}

const int N = 2e5 + 50, P = 1e9 + 7, oo = 1e15, Big = 1e11;

int n, m, S, T, q = 101, b = 137, a[N], x[N], y[N], z[N], tot, sz[N];
int U[N], V[N], W[N], fa[N], me[N];
int find(int x)
{
	return fa[x] == x ? x : fa[x] = find(fa[x]);
}

namespace sub0
{
	void solve()
	{
		int ans = 0;
		For(s, 1, (1 << n) - 1)
		{
			int res = 0;
			For(i, 1, tot)
			{
				if(s >> (U[i] - 1) & 1) if(s >> (V[i] - 1) & 1)
					res -= W[i];
			}
			For(i, 1, n) if(s >> ( i - 1) & 1) res += a[i];
			ans = max(ans, res);
		}
		printf("%lld\n", ans);
	}
}

int f[N], g[N], p[N], cnt;
vector<PII> G[N];
vector<int> vec[N];

void dfs_t(int u, int pa)
{
	f[u] = a[u]; g[u] = 0;
	for(PII E : G[u])
	{
		int v = E.x, w = E.y;
		if(v == pa) continue;
		dfs_t(v, u);
		g[u] += max(g[v], f[v]);
		f[u] += max(f[v] - w, g[v]);
	}
}

bool vis[N];
int mxd, dep[N], pos[N], num;
vector<PII> tr[N];

void dfs1(int u, int pa)
{
	vis[u] = 1; dep[u] = dep[pa] + 1;
	for(PII E : G[u])
	{
		int v = E.x, w = E.y;
		if(v == pa) continue;
		if(!vis[v])
		{
			dfs1(v, u), tr[u].push_back({v, w});
		}
		else
		{
			pos[++ num] = u, pos[++ num] = v;
		}
	}
}

void dfs2(int u, int pa)
{
	for(PII E : tr[u])
	{
		int v = E.x, w = E.y;
		dfs2(v, u);
		g[u] += max(g[v], f[v]);
		f[u] += max(f[v] - w, g[v]);
	}
}


map<PII, int> mp;
bool ENDPOS = 0;
signed main()
{
	in(n); in(m); in(x[0]); in(y[0]); in(a[0]); in(z[0]);
	For(i, 1, n) a[i] = (q * a[i - 1] + b) % P;
	tot = 0;
	For(i, 1, m)
	{
		x[i] = (x[i - 1] * q + b) % P;
		y[i] = (y[i - 1] * q + b) % P;
		z[i] = (z[i - 1] * q + b) % P;
		int u = x[i] % n + 1, v = y[i] % n + 1;
		if(u == v || mp.count({u, v}));
		else {
			tot ++;
			U[tot] = u; V[tot] = v; W[tot] = z[i];
			G[u].push_back({v, z[i]}); G[v].push_back({u, z[i]});
			mp[{u, v}] = mp[{v, u}] = z[i];
		}
	}
	// sub0::solve();
	For(i, 1, n) fa[i] = i;
	For(i, 1, tot)
	{
		fa[find(U[i])] = find(V[i]);
	}
	For(i, 1, n) sz[find(i)] ++; For(i, 1, m) me[find(U[i])] ++;
	int mx = 0, ans = 0;
	For(i, 1, n) vec[find(i)].push_back(i);
	For(i, 1, n) if(find(i) == i)
	{
		if(me[i] == sz[i] - 1)
		{
			dfs_t(i, 0); ans += max(f[i], g[i]); continue;
		}
		num = 0;
		dfs1(i, 0);
		sort(pos + 1, pos + num + 1); num = unique(pos + 1, pos + num + 1) - pos - 1;
		int res = 0;
		For(s, 0, (1 << num) - 1)
		{
			For(j, 1, n) f[j] = a[j], g[j] = 0;
			For(j, 1, num) if(s >> (j - 1) & 1) g[pos[j]] = -oo; else f[pos[j]] = -oo;
			int E = 0;
			For(i, 1, num) For(j, 1, i - 1) if(s >> (i - 1) & 1) if(s >> (j - 1) & 1)
			{
				if(mp.count({pos[i], pos[j]})) E += mp[{pos[i], pos[j]}];
			}
			dfs2(i, 0); res = max(res, max(f[i], g[i]) - E);
		}
		ans += res;
	}
	printf("%lld\n", ans);
	cerr << (&ENDPOS - &START) * 1.0 / 1024 / 1024 << endl;
	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 3ms
memory: 40620kb

input:

10 5 1 2 3 4

output:

3909327860

result:

ok single line: '3909327860'

Test #2:

score: 0
Accepted
time: 4ms
memory: 42240kb

input:

10000 5000 323944114 980712849 218464573 902936457

output:

4127316482561

result:

ok single line: '4127316482561'

Test #3:

score: 0
Accepted
time: 82ms
memory: 50892kb

input:

100000 50000 69108640 163256243 729156747 674884157

output:

40985622819672

result:

ok single line: '40985622819672'

Test #4:

score: 0
Accepted
time: 71ms
memory: 49572kb

input:

100000 50000 274087387 315287350 284117614 889718361

output:

40947902019309

result:

ok single line: '40947902019309'

Test #5:

score: 0
Accepted
time: 71ms
memory: 49704kb

input:

100000 50000 626549769 467318457 986562122 104552559

output:

40907099503987

result:

ok single line: '40907099503987'

Test #6:

score: 0
Accepted
time: 72ms
memory: 52004kb

input:

100000 50000 831528516 619349564 689006624 319386763

output:

41017978988200

result:

ok single line: '41017978988200'

Test #7:

score: 0
Accepted
time: 73ms
memory: 51748kb

input:

100000 50000 36507256 771380671 243967491 534220967

output:

41077280523359

result:

ok single line: '41077280523359'

Test #8:

score: 0
Accepted
time: 76ms
memory: 50208kb

input:

100000 50000 388969637 923411777 946411999 749055172

output:

41009748953373

result:

ok single line: '41009748953373'

Test #9:

score: 0
Accepted
time: 66ms
memory: 49576kb

input:

100000 50000 593948384 75442877 648856501 963889376

output:

40991206034855

result:

ok single line: '40991206034855'

Test #10:

score: 0
Accepted
time: 70ms
memory: 52084kb

input:

100000 50000 946410766 227473984 203817368 31239940

output:

40932441913184

result:

ok single line: '40932441913184'

Test #11:

score: 0
Accepted
time: 78ms
memory: 50596kb

input:

100000 50000 663698625 561638538 539101941 150346545

output:

40924699056938

result:

ok single line: '40924699056938'

Test #12:

score: 0
Accepted
time: 0ms
memory: 38208kb

input:

6 3 90677470 927115294 683528073 970379897

output:

2137516189

result:

ok single line: '2137516189'

Test #13:

score: 0
Accepted
time: 66ms
memory: 51672kb

input:

100000 50000 713669645 241546443 365180750 489471924

output:

40894915891391

result:

ok single line: '40894915891391'

Test #14:

score: 0
Accepted
time: 72ms
memory: 51756kb

input:

100000 50000 221139747 865700752 943990951 580014954

output:

40816659887937

result:

ok single line: '40816659887937'

Test #15:

score: 0
Accepted
time: 68ms
memory: 50400kb

input:

100000 50000 426118494 17731852 498951818 794849159

output:

41067804257367

result:

ok single line: '41067804257367'

Test #16:

score: 0
Accepted
time: 70ms
memory: 52060kb

input:

100000 50000 778580875 169762958 201396320 57380682

output:

41019991719429

result:

ok single line: '41019991719429'

Test #17:

score: 0
Accepted
time: 75ms
memory: 51632kb

input:

100000 50000 983559622 321794065 903840828 77033926

output:

41056783133409

result:

ok single line: '41056783133409'

Test #18:

score: 0
Accepted
time: 75ms
memory: 51792kb

input:

100000 50000 188538363 473825172 458801695 291868131

output:

40983981734650

result:

ok single line: '40983981734650'

Test #19:

score: 0
Accepted
time: 67ms
memory: 50376kb

input:

100000 50000 541000744 625856279 161246197 506702335

output:

41007858745216

result:

ok single line: '41007858745216'

Test #20:

score: 0
Accepted
time: 59ms
memory: 51628kb

input:

100000 50000 745979491 777887386 863690705 721536540

output:

41012305119617

result:

ok single line: '41012305119617'

Test #21:

score: 0
Accepted
time: 79ms
memory: 50016kb

input:

100000 50000 929918492 418651573 936370744 288067408

output:

40948033429862

result:

ok single line: '40948033429862'

Test #22:

score: 0
Accepted
time: 87ms
memory: 50492kb

input:

100000 50000 815729732 264083040 753936146 558134119

output:

41103051884986

result:

ok single line: '41103051884986'

Test #23:

score: 0
Accepted
time: 0ms
memory: 40844kb

input:

6 3 517823605 849894548 121910581 290446313

output:

2830036710

result:

ok single line: '2830036710'

Test #24:

score: 0
Accepted
time: 74ms
memory: 50460kb

input:

100000 50000 20708472 416114146 456380647 122827913

output:

41018143959974

result:

ok single line: '41018143959974'

Test #25:

score: 0
Accepted
time: 63ms
memory: 49768kb

input:

100000 50000 373170854 568145253 11341514 337662118

output:

41028863217493

result:

ok single line: '41028863217493'

Test #26:

score: 0
Accepted
time: 76ms
memory: 50820kb

input:

100000 50000 578149601 720176360 713786023 552496322

output:

40951181028791

result:

ok single line: '40951181028791'

Test #27:

score: 0
Accepted
time: 64ms
memory: 50976kb

input:

100000 50000 930611982 872207467 416230524 767330526

output:

40948576487909

result:

ok single line: '40948576487909'

Test #28:

score: 0
Accepted
time: 71ms
memory: 51664kb

input:

100000 50000 135590722 982164731 788820845 353029936

output:

41020594313888

result:

ok single line: '41020594313888'

Test #29:

score: 0
Accepted
time: 68ms
memory: 51752kb

input:

100000 50000 340569469 28786039 673635900 196998928

output:

41035989067161

result:

ok single line: '41035989067161'

Test #30:

score: 0
Accepted
time: 60ms
memory: 51764kb

input:

100000 50000 693031851 180817146 376080401 411833133

output:

41069115335862

result:

ok single line: '41069115335862'

Test #31:

score: 0
Accepted
time: 62ms
memory: 51980kb

input:

100000 50000 898010598 332848253 626667337 356729603

output:

41076889170221

result:

ok single line: '41076889170221'

Test #32:

score: 0
Accepted
time: 73ms
memory: 51784kb

input:

100000 50000 102989338 484879360 633485777 841501541

output:

41126348825706

result:

ok single line: '41126348825706'

Test #33:

score: 0
Accepted
time: 77ms
memory: 50524kb

input:

100000 50000 967760839 966527548 968770350 813124513

output:

40963659647260

result:

ok single line: '40963659647260'

Test #34:

score: 0
Accepted
time: 3ms
memory: 41360kb

input:

10 5 335727704 87288204 547385244 288081467

output:

3351218817

result:

ok single line: '3351218817'

Test #35:

score: 0
Accepted
time: 63ms
memory: 51700kb

input:

100000 50000 172739579 671214851 27958710 289574275

output:

41023167425732

result:

ok single line: '41023167425732'

Test #36:

score: 0
Accepted
time: 63ms
memory: 50212kb

input:

100000 50000 525201960 123106121 226175719 242792915

output:

40939293482589

result:

ok single line: '40939293482589'

Test #37:

score: 0
Accepted
time: 58ms
memory: 51996kb

input:

100000 50000 730180708 275137227 928620227 457627119

output:

41116772147109

result:

ok single line: '41116772147109'

Test #38:

score: 0
Accepted
time: 66ms
memory: 51664kb

input:

100000 50000 427168334 631064729 672461324 857483040

output:

41025540909037

result:

ok single line: '41025540909037'

Test #39:

score: 0
Accepted
time: 79ms
memory: 52136kb

input:

100000 50000 287621829 579199441 186025596 887295528

output:

41041606108852

result:

ok single line: '41041606108852'

Test #40:

score: 0
Accepted
time: 73ms
memory: 51960kb

input:

100000 50000 492600576 731230548 888470104 520261001

output:

41050863684028

result:

ok single line: '41050863684028'

Test #41:

score: 0
Accepted
time: 71ms
memory: 51900kb

input:

100000 50000 845062957 883261655 590914606 169480296

output:

41064379595552

result:

ok single line: '41064379595552'

Test #42:

score: 0
Accepted
time: 68ms
memory: 51972kb

input:

100000 50000 50041698 35292754 145875473 384314500

output:

40994165715249

result:

ok single line: '40994165715249'

Test #43:

score: 0
Accepted
time: 59ms
memory: 49288kb

input:

100000 50000 255020445 187323861 848319981 599148705

output:

41015913407828

result:

ok single line: '41015913407828'

Test #44:

score: 0
Accepted
time: 3ms
memory: 39752kb

input:

100000 0 1 2 3 4

output:

49891211697278

result:

ok single line: '49891211697278'

Test #45:

score: 0
Accepted
time: 0ms
memory: 41288kb

input:

100 50 499308315 298486660 933836775 591606674

output:

40654051506

result:

ok single line: '40654051506'

Test #46:

score: 0
Accepted
time: 4ms
memory: 40152kb

input:

100000 50000 1 1 2 3

output:

49965403577651

result:

ok single line: '49965403577651'

Test #47:

score: 0
Accepted
time: 3ms
memory: 41196kb

input:

100 50 85174457 766036718 917683570 796585422

output:

38414656729

result:

ok single line: '38414656729'

Test #48:

score: 0
Accepted
time: 0ms
memory: 37668kb

input:

1000 500 187537858 256543968 335220000 244298843

output:

404321266633

result:

ok single line: '404321266633'

Test #49:

score: 0
Accepted
time: 0ms
memory: 38220kb

input:

1000 500 655087915 92907130 687682381 396329949

output:

400275637877

result:

ok single line: '400275637877'

Test #50:

score: 0
Accepted
time: 10ms
memory: 41968kb

input:

10000 5000 340097318 775734102 66433466 200491948

output:

4124854860192

result:

ok single line: '4124854860192'