QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#515046 | #2269. To be Connected, or not to be, that is the Question | WorldFinalEscaped# | WA | 43ms | 13712kb | C++17 | 2.9kb | 2024-08-11 14:45:38 | 2024-08-11 14:45:39 |
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, tot;
} pre[N], suf[N];
bool check(Info L, Info R) {
int k1 = L.cnt1 + L.cnt2, k2 = R.cnt1 + R.cnt2;
// printf("now L = (cnt1 = %d, cnt2 = %d, tot = %d)\n", L.cnt1, L.cnt2, L.tot);
// printf("now R = (cnt1 = %d, cnt2 = %d, tot = %d)\n", R.cnt1, R.cnt2, R.tot);
if (k2 == 1) swap(L, R), swap(k1, k2);
if (k1 == 1) {
return L.tot >= k2;
} else {
if (!L.cnt2 || !R.cnt2) return false;
if (L.tot < k2) return false;
if (R.tot < k1) return false;
if (L.tot - k1 < R.cnt1) return false;
if (R.tot - k2 < L.cnt1) return false;
return true;
}
}
int main() {
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].tot++, 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] > 1) + (sz[fv] > 1);
sz[fv] += sz[fu], f[fu] = fv;
pre[x].cnt1 += (sz[fv] == 1);
pre[x].cnt2 += (sz[fv] > 1);
}
}
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].tot++, 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] > 1) + (sz[fv] > 1);
sz[fv] += sz[fu], f[fu] = fv;
suf[x].cnt1 += (sz[fv] == 1);
suf[x].cnt2 += (sz[fv] > 1);
}
}
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: 9324kb
input:
2 0 861866350 106689920
output:
106689920
result:
ok single line: '106689920'
Test #2:
score: 0
Accepted
time: 1ms
memory: 7960kb
input:
3 0 582295931 120217528 845887275
output:
-1
result:
ok single line: '-1'
Test #3:
score: 0
Accepted
time: 10ms
memory: 10032kb
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: 17ms
memory: 10460kb
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: 43ms
memory: 13712kb
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:
3105544
result:
wrong answer 1st lines differ - expected: '500886962', found: '3105544'