QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#515214 | #2269. To be Connected, or not to be, that is the Question | WorldFinalEscaped# | WA | 37ms | 14348kb | C++17 | 3.5kb | 2024-08-11 16:06:29 | 2024-08-11 16:06:29 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
vector<int> adj[N];
int n, m, a[N], id[N];
int f[N], sz[N];
int find(int x) {
return f[x] == x ? x : f[x] = find(f[x]);
}
// size = 1: cnt1
// size > 1: cnt2
// total dot: tot
// total comp: cnt1 + cnt2
struct Info {
int cnt1, cnt2, cnt3, tot3;
} pre[N], suf[N];
bool check(Info L, Info R) {
int solveL = min(L.tot3 - L.cnt3 * 2, R.cnt1);
L.tot3 -= solveL, R.cnt1 -= solveL;
int solveR = min(R.tot3 - R.cnt3 * 2, L.cnt1);
R.tot3 -= solveR, L.cnt1 -= solveR;
if (L.cnt1 + R.cnt1 > 2) {
return false;
}
int degL = L.cnt1 + 2 * L.cnt2 + L.tot3, cntL = L.cnt1 + L.cnt2 + L.cnt3;
int degR = R.cnt1 + 2 * R.cnt2 + R.tot3, cntR = R.cnt1 + R.cnt2 + R.cnt3;
return degL >= cntR && degR >= cntL;
}
int main() {
// printf("check = %d\n", check({0, 1, 1, 3}, {4, 0, 1, 10000}));
// return 0;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
id[i] = i;
}
sort(id + 1, id + n + 1, [&](int i, int j) { return a[i] < a[j]; });
static int rk[N];
for (int i = 1; i <= n; i++)
rk[id[i]] = i;
for (int i = 1; i <= m; i++) {
int u, v;
scanf("%d%d", &u, &v);
adj[u].push_back(v);
adj[v].push_back(u);
}
for (int i = 1; i <= n; i++) {
f[i] = i, sz[i] = 1;
}
for (int x = 1; x <= n; x++) {
int u = id[x];
pre[x] = pre[x - 1];
pre[x].cnt1++;
for (auto v : adj[u]) {
if (rk[v] >= rk[u])
continue;
if (find(u) == find(v))
continue;
int fu = find(u), fv = find(v);
pre[x].cnt1 -= (sz[fu] == 1) + (sz[fv] == 1);
pre[x].cnt2 -= (sz[fu] == 2) + (sz[fv] == 2);
pre[x].cnt3 -= (sz[fu] >= 3) + (sz[fv] >= 3);
pre[x].tot3 -=
(sz[fu] >= 3 ? sz[fu] : 0) + (sz[fv] >= 3 ? sz[fv] : 0);
sz[fv] += sz[fu], f[fu] = fv;
pre[x].cnt1 += (sz[fv] == 1);
pre[x].cnt2 += (sz[fv] == 2);
pre[x].cnt3 += (sz[fv] >= 3);
pre[x].tot3 += (sz[fv] >= 3 ? sz[fv] : 0);
}
}
for (int i = 1; i <= n; i++) {
f[i] = i, sz[i] = 1;
}
for (int x = n; x >= 1; x--) {
int u = id[x];
suf[x] = suf[x + 1];
suf[x].cnt1++;
for (auto v : adj[u]) {
if (rk[v] <= rk[u])
continue;
if (find(u) == find(v))
continue;
int fu = find(u), fv = find(v);
suf[x].cnt1 -= (sz[fu] == 1) + (sz[fv] == 1);
suf[x].cnt2 -= (sz[fu] == 2) + (sz[fv] == 2);
suf[x].cnt3 -= (sz[fu] >= 3) + (sz[fv] >= 3);
suf[x].tot3 -=
(sz[fu] >= 3 ? sz[fu] : 0) + (sz[fv] >= 3 ? sz[fv] : 0);
sz[fv] += sz[fu], f[fu] = fv;
suf[x].cnt1 += (sz[fv] == 1);
suf[x].cnt2 += (sz[fv] == 2);
suf[x].cnt3 += (sz[fv] >= 3);
suf[x].tot3 += (sz[fv] >= 3 ? sz[fv] : 0);
}
}
for (int x = 1; x < n; x++) {
if (a[id[x]] != a[id[x + 1]]) {
// printf("current debug %d\n", a[id[x]]);
if (check(pre[x], suf[x + 1])) {
printf("%d\n", a[id[x]]);
return 0;
}
}
}
puts("-1");
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 8564kb
input:
2 0 861866350 106689920
output:
106689920
result:
ok single line: '106689920'
Test #2:
score: 0
Accepted
time: 1ms
memory: 8456kb
input:
3 0 582295931 120217528 845887275
output:
-1
result:
ok single line: '-1'
Test #3:
score: 0
Accepted
time: 9ms
memory: 10328kb
input:
52033 0 432755200 936478974 298744051 31368207 847742874 81290408 425992405 651328821 903238557 526933479 356290128 722885083 779029575 480262946 443316470 542413465 170562283 440427743 438763956 784112617 255213130 899556824 323259505 857165776 533714690 565510843 859610987 686006833 211894364 9600...
output:
-1
result:
ok single line: '-1'
Test #4:
score: 0
Accepted
time: 18ms
memory: 11164kb
input:
100000 0 514837648 759500586 899265033 24085608 545610751 221779667 568755229 169602804 284396186 169538713 571993850 645890208 375601406 769765103 805781464 228017324 648075651 874669052 771742115 269678248 190757592 220852391 275602630 816966668 111244645 208546040 715493307 277760893 770626133 25...
output:
-1
result:
ok single line: '-1'
Test #5:
score: -100
Wrong Answer
time: 37ms
memory: 14348kb
input:
100000 99999 299608910 294889223 380597480 583050141 733930013 271705935 623956575 293208851 168678637 517320846 970153874 376864085 620559158 384944405 959726556 311522848 233740144 852169507 874336822 670072232 182817184 163689537 962302870 278762094 916902553 742474244 377317908 941252256 1153825...
output:
495952802
result:
wrong answer 1st lines differ - expected: '500886962', found: '495952802'