QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#343111 | #8235. Top Cluster | ucup-team191# | WA | 2375ms | 64044kb | C++23 | 2.8kb | 2024-03-01 22:33:23 | 2024-03-01 22:33:23 |
Judging History
answer
#include <cstdio>
#include <vector>
#include <algorithm>
#define X first
#define Y second
#define PB push_back
using namespace std;
typedef long long ll;
typedef vector < int > vi;
typedef pair < int, int > pii;
const int N = 5e5 + 500;
vector < pii > v[N];
vector < int > comp;
int siz[N], blok[N], col[N], n, q;
ll dis[N];
void colour(int x, int lst, int cur) {
col[x] = cur;
for(auto [y, w] : v[x])
if(y != lst && !blok[y]) colour(y, x, cur);
}
void dfs(int x, int lst) {
siz[x] = 1; comp.PB(x);
for(auto [y, w] : v[x]) {
if(y != lst && !blok[y]) {
dis[y] = dis[x] + w;
dfs(y, x);
siz[x] += siz[y];
}
}
}
bool cmp(int A, int B) {
return dis[A] > dis[B];
}
ll qdis[N];
int ans[N];
int qno[N], val[N];
bool cmp2(int A, int B) {
return qdis[A] - dis[qno[A]] > qdis[B] - dis[qno[B]];
}
void rek(int xx, vector < int > qv) {
dfs(xx, xx);
int x = xx;
for(int t : comp)
if(siz[t] < siz[x] && 2 * siz[t] >= (int)comp.size())
x = t;
comp.clear();
dis[x] = 0; dfs(x, x);
sort(comp.begin(), comp.end(), cmp);
for(int i = 0;i < (int)v[x].size();i++) {
if(!blok[v[x][i].X]) colour(v[x][i].X, x, i);
}
col[x] = -1;
int odg1 = -1, odg2 = -1, j = 0;
vector < vi > sQ((int)v[x].size());
sort(qv.begin(), qv.end(), cmp2);
for(int i = 0;i < (int)comp.size();i++) {
int c = comp[i];
while(j < (int)qv.size() && qdis[qv[j]] - dis[qno[qv[j]]] >= dis[c]) {
if(odg1 != -1 && col[odg1] != col[qno[qv[j]]]) {
ans[qv[j]] = min(ans[qv[j]], val[odg1]);
}
if(odg2 != -1 && col[odg2] != col[qno[qv[j]]]) {
ans[qv[j]] = min(ans[qv[j]], val[odg2]);
}
if(qno[qv[j]] != x) sQ[col[qno[qv[j]]]].PB(qv[j]);
j++;
}
if(odg1 == -1 || val[c] < val[odg1])
swap(c, odg1);
if(odg2 == -1 || val[c] < val[odg2])
swap(c, odg2);
}
while(j < (int)qv.size()) {
if(odg1 != -1 && col[odg1] != col[qno[qv[j]]]) {
ans[qv[j]] = min(ans[qv[j]], val[odg1]);
}
if(odg2 != -1 && col[odg2] != col[qno[qv[j]]]) {
ans[qv[j]] = min(ans[qv[j]], val[odg2]);
}
if(qno[qv[j]] != x) sQ[col[qno[qv[j]]]].PB(qv[j]);
j++;
}
blok[x] = 1;
for(int i = 0;i < (int)v[x].size();i++) {
if(!blok[v[x][i].X]) {
rek(v[x][i].X, sQ[i]);
}
}
}
int main(){
scanf("%d%d", &n, &q);
vi smece;
for(int i = 1;i <= n;i++) {
scanf("%d", val + i); smece.PB(val[i]);
}
sort(smece.begin(), smece.end());
int bar = 0;
while(bar < n && smece[bar] == bar) bar++;
for(int i = 1;i < n;i++) {
int a, b, c; scanf("%d%d%d", &a, &b, &c);
v[a].PB({b, c}); v[b].PB({a, c});
}
smece.clear();
for(int i = 0;i < q;i++) {
smece.PB(i); ans[i] = bar;
scanf("%d%lld", qno + i, qdis + i);
}
sort(smece.begin(), smece.end(), cmp2);
rek(1, smece);
for(int i = 0;i < q;i++)
printf("%d\n", ans[i]);
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 12112kb
input:
5 4 3 9 0 1 2 1 2 10 3 1 4 3 4 3 3 5 2 3 0 1 0 4 6 4 7
output:
1 0 3 4
result:
ok 4 number(s): "1 0 3 4"
Test #2:
score: -100
Wrong Answer
time: 2375ms
memory: 64044kb
input:
500000 500000 350828 420188 171646 209344 4 999941289 289054 79183 999948352 427544 160827 138994 192204 108365 99596 999987124 292578 2949 384841 269390 999920664 315611 163146 51795 265839 34188 999939494 145387 366234 86466 220368 357231 347706 332064 279036 173185 5901 217061 112848 37915 377359...
output:
0 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 2499...
result:
wrong answer 56785th numbers differ - expected: '156', found: '601'