QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#429545#7415. Fast Spanning TreeBronyaWA 2ms11976kbC++141.9kb2024-06-02 16:48:202024-06-02 16:48:21

Judging History

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

  • [2024-06-02 16:48:21]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:11976kb
  • [2024-06-02 16:48:20]
  • 提交

answer

#include<bits/stdc++.h>

using namespace std;
int n,m;
int a[300005];
int u[300005],v[300005],w[300005];
int fa[300005];
priority_queue<int>q;
struct Heap{
    int lson,rson;
    int len;
    int val,id;
}t[20000005];
int cnt,root[300005];
void merge(int &rt,int x,int y){
    if(!x||!y){
        rt=x+y;
        return;
    }
    if(t[x].val>t[y].val)swap(x,y);
    rt=x;
    merge(t[rt].rson,t[x].rson,y);
    if(t[t[rt].rson].len>t[t[rt].lson].len)swap(t[rt].lson,t[rt].rson);
    t[rt].len=t[t[rt].rson].len+1;
}
bool pd[300005];
int siz[300005];
int Find(int u){
    return (fa[u]!=u?(fa[u]=Find(fa[u])):u);
}
void Insert(int x){
    if(pd[x])return;
    int U=Find(u[x]),V=Find(v[x]);
    if(U==V)return;
    // cerr<<"!!"<<x<<endl;
    if(siz[U]+siz[V]>=w[x]){
        pd[x]=true;
        q.push(-x);
        return;
    }
    int rev=(w[x]-siz[U]-siz[V]+1)/2;
    ++cnt;t[cnt].val=rev;t[cnt].id=x;
    merge(root[U],root[U],cnt);
    ++cnt;t[cnt].val=rev;t[cnt].id=x;
    merge(root[V],root[V],cnt);
}
queue<int>Q;
void check(int x){
    while(root[x]&&t[root[x]].val<=siz[x]){
        int y=root[x];
        Insert(t[y].id);
        merge(root[x],t[y].lson,t[y].rson);
    }
}
int lans[300005],ans;
int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)scanf("%d",&a[i]),fa[i]=i,siz[i]=a[i];
    for(int i=1;i<=m;i++){
        scanf("%d%d%d",&u[i],&v[i],&w[i]);
        pd[i]=false;
        Insert(i);
    }
    while(!q.empty()){
        int x=-q.top();q.pop();
        int U=Find(u[x]),V=Find(v[x]);
        if(U==V)continue;
        lans[++ans]=x;
        fa[V]=U;siz[U]+=siz[V];
        merge(root[U],root[U],root[V]);
        Q.push(U);
        while(!Q.empty()){
            int y=Q.front();Q.pop();
            check(y);
        }
    }
    printf("%d\n",ans);
    for(int i=1;i<=ans;i++)printf("%d ",lans[i]);
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 10172kb

input:

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

output:

4
2 3 1 4 

result:

ok 5 number(s): "4 2 3 1 4"

Test #2:

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

input:

3 5
3 2 2
1 2 6
1 2 6
1 2 3
1 2 6
2 3 6

output:

2
3 5 

result:

ok 3 number(s): "2 3 5"

Test #3:

score: 0
Accepted
time: 0ms
memory: 11948kb

input:

10 20
4 6 6 1 7 9 1 8 7 9
5 3 2
1 10 10
5 6 7
9 6 9
3 1 1
6 8 1
5 7 0
9 5 4
10 3 4
8 6 8
3 10 6
5 3 8
3 7 9
1 9 10
10 1 0
5 7 6
6 10 1
6 5 9
5 8 2
9 2 4

output:

8
1 2 3 4 5 6 7 20 

result:

ok 9 numbers

Test #4:

score: 0
Accepted
time: 0ms
memory: 9868kb

input:

10 20
0 10 4 6 2 0 2 0 2 8
5 10 8
7 1 6
10 5 0
9 8 0
5 8 4
5 1 8
10 3 6
5 4 3
9 2 6
10 3 6
4 3 1
3 10 3
6 1 3
3 2 5
6 9 2
4 2 5
10 6 5
8 6 3
2 1 0
2 3 6

output:

9
1 4 5 6 2 7 8 9 13 

result:

ok 10 numbers

Test #5:

score: -100
Wrong Answer
time: 1ms
memory: 10176kb

input:

100 200
49 21 70 93 66 51 36 59 56 62 10 4 46 73 22 48 89 17 72 60 29 64 19 56 32 54 55 0 77 86 34 35 56 17 55 2 98 80 73 71 64 37 61 22 89 96 99 28 0 35 56 45 74 81 30 3 66 96 28 16 29 43 60 61 60 95 83 5 73 1 28 47 73 44 8 4 91 34 100 23 4 93 44 87 72 5 13 88 100 52 56 61 100 80 14 30 59 97 51 43
...

output:

96
1 2 4 5 6 7 8 10 11 12 13 14 15 16 17 18 19 9 20 21 22 23 25 26 3 27 28 29 30 31 32 33 35 36 37 38 39 40 42 44 45 46 47 48 49 50 51 52 53 34 54 55 56 58 59 60 61 62 63 64 41 66 67 68 69 70 71 72 74 76 78 80 81 82 87 89 91 94 95 96 100 101 24 84 103 107 109 110 117 119 129 142 144 169 170 195 

result:

wrong answer 40th numbers differ - expected: '41', found: '42'