Cod sursa(job #2711296)

Utilizator stefan.popescuPopescu Stefan stefan.popescu Data 23 februarie 2021 21:31:37
Problema Radiatie Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.73 kb
#include <bits/stdc++.h>
using namespace std;

#define SERVER 0
const string nameFile="radiatie.in";
ifstream in (nameFile+".in");
ofstream out(nameFile+".out");
typedef pair<int, int> Pii;
typedef pair<int, long long> Pil;
typedef pair<short, short> Pss;
typedef pair<double, Pii> Pdii;
typedef pair<int, Pii> Piii;
#if (SERVER)
    #define in cin
    #define out cout
#endif
const int nmax=15e3;
int n, m, q, x, y, z;
int fat[nmax+2];
inline void preFat(){
    for(int i=1; i<=n; i++)
        fat[i]=i;
}
int getFat(int nod){
    if(fat[nod]==nod) return nod;
    return fat[nod]=getFat(fat[nod]);
}
void DsuUnion(int nod1, int nod2){
    nod1=getFat(nod1), nod2=getFat(nod2);
    fat[nod2]=nod1;
}
inline bool areInSameTree(int nod1, int nod2){
    return getFat(nod1)==getFat(nod2);
}
vector <Piii> muchii;
int sol[2*nmax+2];
Pii qrs[2*nmax+2];
vector <int> pos;

int main()
{
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    in>>n>>m>>q;
    for(int i=1; i<=m; i++){
        in>>x>>y>>z;
        muchii.push_back({z, {x, y}});
    }
    sort(muchii.begin(), muchii.end());
    for(int i=1; i<=q; i++){
        in>>qrs[i].first>>qrs[i].second;
        pos.push_back(i);
    }
    for(int put=29; put>=0; put--){
        int vf=0;
        preFat();
        for(int i=0; i<q; i++){
            while(vf<m&&sol[pos[i]]+(1<<put)>=muchii[vf].first)
                DsuUnion(muchii[vf].second.first, muchii[vf].second.second), vf++;
            if(areInSameTree(qrs[pos[i]].first, qrs[pos[i]].second)==false)
                sol[pos[i]]+=(1<<put);
        }
        sort(pos.begin(), pos.end(), [&](const int i, const int j){
            return sol[i]<sol[j];
        });
    }
    for(int i=1; i<=q; i++)
        out<<sol[i]+1<<"\n";
    return 0;
}