QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#403866#7415. Fast Spanning TreeCall_me_EricWA 0ms20704kbC++143.0kb2024-05-02 19:42:082024-05-02 19:42:14

Judging History

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

  • [2024-05-02 19:42:14]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:20704kb
  • [2024-05-02 19:42:08]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
bool stmemory;
namespace Call_me_Eric{
inline int read(){
    int x = 0, f = 1;char ch = getchar();
    while(ch < '0' || ch > '9'){if(ch == '-') f = -1;ch = getchar();}
    while(ch >= '0' && ch <= '9'){x = (x << 1) + (x << 3) + (ch ^ 48);ch = getchar();}
    return x * f;
}
const int maxn = 3e5 + 10;
int n, m;
struct edge{
    int u, v, s, id;
    edge(int u = 0,int v = 0,int s = 0,int id = 0)
    :u(u),v(v),s(s),id(id){}
}e[maxn];
vector<int> ans;

int fa[maxn], siz[maxn];
bool del[maxn];
struct warn{
    int lim, id;
    warn(int lim = 0,int id = 0):lim(lim),id(id){}
    friend bool operator < (warn a,warn b){return a.lim != b.lim ? a.lim < b.lim : a.id < b.id;}
    friend bool operator > (warn a,warn b){return a.lim != b.lim ? a.lim > b.lim : a.id > b.id;}
};
int getf(int x){return fa[x] == x ? x : fa[x] = getf(fa[x]);}
priority_queue<int,vector<int>,greater<int> > que;
priority_queue<warn,vector<warn>,greater<warn> > q[maxn];

void main(){
    n = read(), m = read();int u, v, s;
    for(int i = 1;i <= n;i++)siz[i] = read(), fa[i] = i;
    for(int i = 1;i <= m;i++){
        u = read(), v = read(), s = read();
        e[i] = edge(u,v,s,i);
        if(siz[u] + siz[v] >= s)que.push(i);
        else{
            q[u].emplace((s + siz[u] - siz[v] + 1) / 2,i);
            q[v].emplace((s + siz[v] - siz[u] + 1) / 2,i);
        }
    }
    auto check = [](int id){
        if(del[id])return;
        int u = getf(e[id].u), v = getf(e[id].v);
        if(u == v)return;
        if(siz[u] + siz[v] >= e[id].s){ans.emplace_back(id);del[id] = 1;return;}
        q[u].emplace((e[id].s + siz[u] - siz[v] + 1) / 2,id);
        q[v].emplace((e[id].s + siz[v] - siz[u] + 1) / 2,id);
        return;
    };
    while(!que.empty()){
        int id = que.top();que.pop();if(del[id])continue;
        u = e[id].u, v = e[id].v, s = e[id].s;
        int fu = getf(u), fv = getf(v), sum = siz[fu] + siz[fv];
        if(fu == fv)continue;
        ans.emplace_back(id);del[id] = 1;
        if(q[u].size() < q[v].size())swap(u, v);
        fa[v] = u;siz[u] += siz[v];siz[u] = min((int)1e9,siz[u]);
        while(!q[v].empty()){
            warn tp = q[v].top();q[v].pop();
            if(siz[u] >= tp.lim)check(tp.id);
            else q[u].emplace(tp);
        }
        while(!q[u].empty()){
            warn tp = q[u].top();
            if(siz[u] >= tp.lim){check(tp.id);q[u].pop();}
            else break;
        }
    }
    printf("%d\n",ans.size());
    for(int i : ans)printf("%d ",i);
    puts("");
    return;
}
};
bool edmemory;
signed main(){
    auto stclock = clock();
    Call_me_Eric::main();
    auto edclock = clock();
    cerr << (&stmemory - &edmemory) / 1024.0 / 1024.0 << " Mib cost.\n";
    cerr << (edclock - stclock) * 1.0 / CLOCKS_PER_SEC << " Sec cost.\n";
    return 0;
}
/*
5 5
1 4 3 4 0
4 5 5
3 1 1
2 5 2
4 3 1
4 1 4

3 5
3 2 2
1 2 6
1 2 6
1 2 3
1 2 6
2 3 6
*/

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 20532kb

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: 0ms
memory: 20700kb

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: -100
Wrong Answer
time: 0ms
memory: 20704kb

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:

12
1 13 2 3 4 5 6 7 8 12 16 20 

result:

wrong answer 1st numbers differ - expected: '8', found: '12'