Cod sursa(job #3323405)

Utilizator yellowGreenFatu Mihai yellowGreen Data 18 noiembrie 2025 11:55:55
Problema Radiatie Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.28 kb
#include <fstream>
#include <vector>
#include <algorithm>
#include <stack>

using namespace std;
ifstream cin("radiatie.in");
ofstream cout("radiatie.out");
struct idk
{
    int y,v;
};
struct idkk
{
    int x,y,v;
    const bool operator<(const idkk &oth) const
    {
        return v<oth.v;
    }
};
struct ceva
{
    int x,tata,h,v;
};
const int NMAX = 15'005;
int n,m,q;
int T[NMAX];
idkk M[NMAX*2];
vector<idk> G[NMAX];
int niv[NMAX];
int anc[15][NMAX],dp[15][NMAX];
int rad(int x)
{
    if(x==T[x])
        return x;
    return T[x]=rad(T[x]);
}
void dfs(int x,int tata,int h,int v)
{
    stack<ceva> st;
    st.emplace(x,tata,h,v);
    while(!st.empty())
    {
        auto [x,tata,h,v]=st.top();
        st.pop();
        //dp[0][x]=v;
        //anc[0][x]=tata;
        niv[x]=h;
        for(auto [y,v]:G[x])
        {
            if(y==tata)
                continue;
            st.emplace(y,x,h+1,v);
        }
    }
}
int max_lant(int x,int y)
{
    if(x==y)
        return 0;
    int ans=0;
    if(niv[x]<niv[y])
        swap(x,y);
    int dif=niv[x]-niv[y];
    for(int i=0;i<=14;i++)
        if(dif&(1<<i))
        {
            ans=max(ans,dp[i][x]);
            x=anc[i][x];
        }
    if(x==y) return ans;
    for(int i=14;i>=0;i--)
        if(anc[i][x]!=anc[i][y])
    {
        ans=max(ans,dp[i][x]);
        ans=max(ans,dp[i][y]);
        x=anc[i][x];
        y=anc[i][y];
    }
    if(x!=y)
    {
        ans=max(ans,dp[0][x]);
        ans=max(ans,dp[0][y]);
    }
    return ans;
}
int main()
{
    cin>>n>>m>>q;
    for(int i=1;i<=m;i++)
        cin>>M[i].x>>M[i].y>>M[i].v;
    sort(M+1,M+m+1);
    for(int i=1;i<=n;i++)
        T[i]=i;
    for(int i=1;i<=m;i++)
    {
        int x=M[i].x;
        int y=M[i].y;
        int v=M[i].v;
        if(rad(x)==rad(y))
            continue;
        T[y]=x;
        G[x].emplace_back(y,v);
        G[y].emplace_back(x,v);
    }
    dfs(1,0,1,0);
    /*for(int i=1;i<=14;i++)
        for(int j=1;j<=n;j++)
        {
            anc[i][j]=anc[i-1][anc[i-1][j]];
            dp[i][j]=max(dp[i-1][j],dp[i-1][anc[i-1][j]]);
        }
    while(q--)
    {
        int x,y;
        cin>>x>>y;
        cout<<max_lant(x,y)<<"\n";
    }*/
    return 0;
}