QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#251678 | #2269. To be Connected, or not to be, that is the Question | lemonilemon# | WA | 34ms | 18112kb | C++17 | 2.9kb | 2023-11-14 23:12:16 | 2023-11-14 23:12:17 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using uint = unsigned int;
const double EPS = 1e-8;
const int INF = 0x3F3F3F3F;
const ll LINF = 46 * ll(1e17);
const int MOD = 1e9 + 7;
const int maxn = 1e5 + 25;
struct dsu_undo {
vector<int> e;
int cnt_cc, cnt_size_one;
vector<pair<int, int>> st;
vector<int> st2, st3;
dsu_undo(int n): e(n, -1), cnt_cc(n), cnt_size_one(n) { }
int size(int x) { return -e[find(x)]; }
int find(int x) { return e[x] < 0 ? x : find(e[x]); }
int time() { return st.size(); }
void rollback(int t) {
for (int i = time(); i-- > t;) {
e[st[i].first] = st[i].second;
cnt_size_one = st2[i];
cnt_cc = st3[i];
}
st.resize(t);
st2.resize(t);
st3.resize(t);
}
bool join(int a, int b) {
a = find(a), b = find(b);
if (a == b)
return false;
if (e[a] > e[b]) swap(a, b);
st.push_back({a, e[a]});
st.push_back({b, e[b]});
st2.push_back(cnt_size_one);
st2.push_back(cnt_size_one);
st3.push_back(cnt_cc);
st3.push_back(cnt_cc);
cnt_size_one -= (size(a) == 1) + (size(b) == 1);
cnt_cc--;
e[a] += e[b]; e[b] = a;
return true;
}
};
// cnt_cc, cnt_size_one, size()
int l[maxn];
int ord[maxn];
int mod[maxn];
vector<int> lgraph[maxn], rgraph[maxn];
signed main() { ios::sync_with_stdio(0); cin.tie(0);
int n, m;
cin >> n >> m;
dsu_undo ldsu(n), rdsu(n);
for(int i = 0; i < n; ++i) {
cin >> l[i];
ord[i] = i;
}
sort(ord, ord + n, [](int a, int b) {
return l[a] < l[b];
});
for(int i = 0; i < m; ++i) {
int u, v;
cin >> u >> v;
--u, --v;
if(l[u] < l[v]) swap(u, v);
lgraph[u].push_back(v);
rgraph[v].push_back(u);
}
for(int i = n - 1; i >= 0; --i) {
int u = ord[i];
for(auto v : rgraph[u]) {
mod[i] += rdsu.join(u, v);
}
}
for(int i = 0; i < n - 1; ++i) {
int u = ord[i];
for(auto v : lgraph[u]) {
ldsu.join(u, v);
}
auto t = rdsu.time();
rdsu.rollback(t - 2 * mod[i]);
if(l[ord[i + 1]] == l[ord[i]]) continue;
int lcntcc = ldsu.cnt_cc - (n - i - 1);
int rcntcc = rdsu.cnt_cc - (i + 1);
int lcntone = ldsu.cnt_size_one - (n - i - 1);
int rcntone = rdsu.cnt_size_one - (i + 1);
if(i + 1 < rcntcc || (n - i - 1) < lcntcc) continue;
int edge = lcntcc - lcntone + rcntcc - rcntone - 1;
int lleft = (i + 1) - lcntone - edge;
int rleft = (n - i - 1) - rcntone - edge;
if(((lleft >= rcntone) || (rleft >= lcntone)) && (lleft >= 0 && rleft >= 0)) {
cout << l[u] << '\n';
return 0;
}
}
cout << "-1\n";
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 8568kb
input:
2 0 861866350 106689920
output:
106689920
result:
ok single line: '106689920'
Test #2:
score: 0
Accepted
time: 2ms
memory: 8124kb
input:
3 0 582295931 120217528 845887275
output:
-1
result:
ok single line: '-1'
Test #3:
score: 0
Accepted
time: 6ms
memory: 9004kb
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: 9380kb
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: 34ms
memory: 18112kb
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:
382729706
result:
wrong answer 1st lines differ - expected: '500886962', found: '382729706'