Submission #62553531


Source Code Expand

Copy
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
#define N 200005
struct DSU{
vector<int> fa,si;
int size;
DSU(int x=0){
init(x);
}
void init(int n){
fa.resize(n+1);
for(int i=1;i<=n;i++){
fa[i]=i;
}
si.assign(n+1,1);
size=n;
}
 
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
#define N 200005

struct DSU{
    vector<int> fa,si;
    int size;

    DSU(int x=0){
        init(x);
    }

    void init(int n){
        fa.resize(n+1);
        for(int i=1;i<=n;i++){
            fa[i]=i;
        }
        si.assign(n+1,1);
        size=n;
    }

    int find(int x){
        return x==fa[x]?x:fa[x]=find(fa[x]);
    }

    bool merge(int x,int y){
        x=find(x);y=find(y);
        // if(si[x]<si[y])swap(x,y);
        if(x==y)return false;
        fa[y]=x;
        si[x]+=si[y];
        return true;
    }
};

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    
    int n,m;
    cin>>n>>m;

    DSU dsu(n);
    vector<int> l(m+1),r(m+1);
    vector<int> les;
    for(int i=1;i<=m;i++){
        cin>>l[i]>>r[i];
        if(!dsu.merge(l[i],r[i]))les.push_back(i);
    }
    set<int> bl;
    for(int i=1;i<=n;i++){
        bl.insert(dsu.find(i));
    }
    int pt=0;
    vector<array<int,3>> op;
    while(bl.size()>=2){
        int f=dsu.find(l[les[pt]]);
        if(f==*(bl.begin())){
            int v=*(bl.rbegin());
            op.push_back({les[pt],l[les[pt]],v});
            dsu.merge(f,v);
            bl.erase(v);
            pt++;  
        }else{
            int v=*(bl.begin());
            op.push_back({les[pt],l[les[pt]],v});
            dsu.merge(f,v);
            bl.erase(v);
            pt++;
        }
    }
    cout<<op.size()<<"\n";
    sort(op.begin(),op.end());
    for(auto c:op){
        cout<<c[0]<<" "<<c[1]<<" "<<c[2]<<"\n";
    }
    return 0;
}

Submission Info

Submission Time
Task E - Cables and Servers
User Mint_Cat
Language C++ 17 (gcc 12.2)
Score 450
Code Size 1667 Byte
Status AC
Exec Time 97 ms
Memory 17684 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 450 / 450
Status
AC × 3
AC × 22
Set Name Test Cases
Sample sample_01.txt, sample_02.txt, sample_03.txt
All random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, sample_01.txt, sample_02.txt, sample_03.txt
Case Name Status Exec Time Memory
random_01.txt AC 20 ms 5040 KB
random_02.txt AC 13 ms 5248 KB
random_03.txt AC 3 ms 3716 KB
random_04.txt AC 1 ms 3460 KB
random_05.txt AC 22 ms 5812 KB
random_06.txt AC 14 ms 5640 KB
random_07.txt AC 4 ms 3936 KB
random_08.txt AC 1 ms 3444 KB
random_09.txt AC 24 ms 5536 KB
random_10.txt AC 8 ms 4436 KB
random_11.txt AC 41 ms 7428 KB
random_12.txt AC 1 ms 3504 KB
random_13.txt AC 34 ms 6184 KB
random_14.txt AC 32 ms 6216 KB
random_15.txt AC 35 ms 6188 KB
random_16.txt AC 32 ms 6100 KB
random_17.txt AC 93 ms 13268 KB
random_18.txt AC 97 ms 17684 KB
random_19.txt AC 2 ms 3548 KB
sample_01.txt AC 1 ms 3460 KB
sample_02.txt AC 1 ms 3428 KB
sample_03.txt AC 1 ms 3464 KB


2025-03-24 (Mon)
09:07:26 +00:00