QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#105649#6402. MEXimum Spanning Treewoyouxiangbaile#TL 2ms3520kbC++142.6kb2023-05-14 17:05:442023-05-14 17:05:48

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-05-14 17:05:48]
  • 评测
  • 测评结果:TL
  • 用时:2ms
  • 内存:3520kb
  • [2023-05-14 17:05:44]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define rep(i, d, u) for(int i = d; i <= u; ++i)
#define dep(i, u, d) for(int i = u; i >= d; --i)
#define cep(n) while(n--)
#define gep(i, a) for(int i = firs[a]; i; i = neig[i])
int ured() {
	int re = 0;
	char ch;
	do {
		ch = getchar();
	} while('9' < ch || ch < '0');
	do {
		re = re * 10 + (ch ^ '0');
	} while('0' <= (ch = getchar()) && ch <= '9');
	return re;
}
void uwit(int da) {
	int ch[21], cn = 0;
	do {
		ch[++cn] = da - da / 10 * 10;
	} while(da /= 10);
	do {
		putchar('0' ^ ch[cn]);
	} while(--cn);
}
const int _maxn = 1011;
int n, m, u[_maxn], v[_maxn], w[_maxn], firs[_maxn], neig[_maxn << 1], arri[_maxn << 1], fath[_maxn], dfat[_maxn], fatp[_maxn];
int queu[_maxn], head, tail, frda[_maxn], frpo[_maxn], dans, dept[_maxn], atpo[_maxn];
bool visi[_maxn];
vector<int> edge[_maxn];
int find(int at) {
	return fath[at] == at ? at : fath[at] = find(fath[at]);
}
void dfs1(int at, int fa, int rt) {
	dfat[at] = fa, dept[at] = dept[fa] + 1, atpo[at] = rt;
	gep(i, at) {
		if(arri[i] != fa) {
			fatp[arri[i]] = i >> 1, dfs1(arri[i], at, rt);
		}
	}
}
void updb(int da, int at, int fr) {
	for(int i : edge[da]) {
		if(!visi[i]) {
			queu[++tail] = i, frda[i] = at, frpo[i] = fr;
		}
	}
}
void upda(int le, int ri, int fr) {
	while(le != ri) {
		if(dept[find(le)] > dept[find(ri)]) {
			updb(w[fatp[find(le)]], fatp[find(le)], fr), fath[find(le)] = find(dfat[find(le)]);
		} else {
			updb(w[fatp[find(ri)]], fatp[find(ri)], fr), fath[find(ri)] = find(dfat[find(ri)]);
		}
	}
}
int main() {
	n = ured(), m = ured();
	rep(i, 1, m) {
		u[i] = ured(), v[i] = ured(), w[i] = ured(), edge[w[i]] . push_back(i);
	}
	rep(i, 0, n - 1) {
		rep(j, 1, n) {
			firs[j] = dfat[j] = 0, fath[j] = j;
		}
		rep(j, 1, m) {
			if(visi[j]) {
				neig[j << 1] = firs[u[j]], firs[u[j]] = j << 1, arri[j << 1] = v[j];
				neig[j << 1 | 1] = firs[v[j]], firs[v[j]] = j << 1 | 1, arri[j << 1 | 1] = u[j];
			}
		}
		rep(j, 1, n) {
			if(!dfat[j]) {
				dfs1(j, 0, j);
			}
		}
		head = tail = dans = 0;
		for(int j : edge[i]) {
			queu[++tail] = j, frpo[j] = frda[j] = 0;
		}
		while(head < tail) {
			++head;
			if(atpo[u[queu[head]]] == atpo[v[queu[head]]]) {
				upda(u[queu[head]], v[queu[head]], queu[head]);
			} else {
				dans = queu[head];
				break;
			}
		}
		if(dans) {
			for(; ; dans = frpo[dans]) {
				visi[dans] = 1;
				if(frda[dans]) {
					visi[frda[dans]] = 0;
				} else {
					break;
				}
			}
		} else {
			uwit(i), putchar('\n');
			return 0;
		}
	}
	uwit(n), putchar('\n');
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 3520kb

input:

4 4
1 2 0
2 3 1
1 3 1
3 4 2

output:

3

result:

ok 1 number(s): "3"

Test #2:

score: 0
Accepted
time: 2ms
memory: 3360kb

input:

1000 1000
647 790 6
91 461 435
90 72 74
403 81 240
893 925 395
817 345 136
88 71 821
831 962 53
164 270 298
14 550 317
99 580 81
26 477 488
977 474 861
413 483 167
872 675 17
819 327 449
594 242 68
381 983 319
867 582 358
869 225 669
274 352 392
40 388 998
246 477 44
508 979 286
483 776 71
580 438 6...

output:

502

result:

ok 1 number(s): "502"

Test #3:

score: -100
Time Limit Exceeded

input:

900 1000
232 890 107
425 399 19
5 74 753
105 333 163
779 42 582
359 647 524
767 409 48
239 780 443
484 489 546
97 634 562
627 866 714
500 357 590
60 728 591
407 686 210
547 32 370
76 772 500
407 584 772
73 699 69
332 847 516
829 754 727
562 756 678
819 303 128
781 667 263
535 672 767
89 762 216
878 ...

output:


result: