Cod sursa(job #2772395)

Utilizator CelestinNegraru Celestin Celestin Data 1 septembrie 2021 00:00:21
Problema Radiatie Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.43 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#define INF 0x3f3f3f3f
#define maxi 15005
using namespace std;
struct muchie{
     int x;
     int y;
     int cost;
};
ifstream f;
ofstream g;
vector<pair<int,int>> V[maxi];
vector<muchie> v;
vector<muchie>::iterator I1,I2;
int n,m,t,R[maxi],RANG[maxi],tata[maxi],first[maxi],euler[maxi<<1],lvl[maxi<<1],a[10*maxi],k,costt[maxi];
bool viz[maxi];
bool CMP(muchie A,muchie B)
{
    return(A.cost<B.cost);
}
void INIT()
{
    for(int i=1;i<=n;i++)
        R[i]=i,RANG[i]=1;
    return;
}
int REPREZENTANT(int x)
{
    while(x!=R[x])
        x=R[x];
    return x;
}
void UNIRE(int x,int y)
{
    if(RANG[x]>RANG[y])
    {
        R[y]=x;
    }
    if(RANG[y]>RANG[x])
    {
        R[x]=y;
    }
    if(RANG[x]==RANG[y])
    {
        RANG[x]++;
        R[y]=x;
    }
    return;
}
void KRUSKAL()
{
    INIT();
    I1=v.begin();
    I2=v.end();
    sort(I1,I2,CMP);
    for(auto a:v)
    {
        int rx=REPREZENTANT(a.x);
        int ry=REPREZENTANT(a.y);
        if(rx!=ry)
        {
            UNIRE(rx,ry);
            V[a.x].push_back(make_pair(a.y,a.cost));
            V[a.y].push_back(make_pair(a.x,a.cost));

        }
    }
  return;
}
void DFS(int x,int niv)
{
    viz[x]=true;
    euler[++k]=x;
    lvl[k]=niv;
    first[x]=k;
    for(auto a:V[x])
    {
        if(!viz[a.first])
            {tata[a.first]=x;
             costt[a.first]=a.second;
             DFS(a.first,niv+1);
            }
    }
}
void UPDATE(int nod,int st,int dr,int poz,int val)
{
    if(st>dr)
        return;
    if(st==dr)
    {
        a[nod]=poz;
        return;
    }
    int mid=(st+dr)>>1;
    if(poz<=mid)
        UPDATE(2*nod,st,mid,poz,val);
    else UPDATE(2*nod+1,mid+1,dr,poz,val);
    if(lvl[a[2*nod]]<=lvl[a[2*nod+1]])
        a[nod]=a[2*nod];
    else a[nod]=a[2*nod+1];
}
void QUERY(int nod,int st,int dr,int r,int t,int&hsol,int&sol)
{
    if(st>dr)
        return;
    if(st>=r&&dr<=t)
    {
        if(hsol>lvl[a[nod]])
        {
            hsol=lvl[a[nod]];
            sol=euler[a[nod]];
        }
        return;
    }
    int mid=(st+dr)>>1;
    if(r<=mid)
        QUERY(2*nod,st,mid,r,t,hsol,sol);
    if(t>mid)
        QUERY(2*nod+1,mid+1,dr,r,t,hsol,sol);
}
int LCA(int x,int y)
{
    int st=first[x];
    int dr=first[y];
    if(st>dr)
        swap(st,dr);
    int hmin=INF;
    int ss=0;
    QUERY(1,1,k,st,dr,hmin,ss);
    return ss;
}
void SOLVE()
{
    int p,l,o,e,z;
    f.open("radiatie.in",ios::in);
    g.open("radiatie.out",ios::out);
    f>>n>>m>>t;
    for(int i=1;i<=m;i++)
    {
        f>>p>>l>>o;
        v.push_back({p,l,o});
    }
    KRUSKAL();
    DFS(1,0);
    /*for(int i=1;i<=k;i++)
    {
        UPDATE(1,1,k,i,i);
    }*/
    for(int i=1;i<=t;i++)
    {
        f>>e>>z;
       /// int lca=LCA(e,z);
        int p1=e;
        int p2=z;
        int cost1=0;
        while(p1!=p2)
        {
            if(lvl[p1]>lvl[p2])
            {
                if(costt[p1]>cost1)
                    cost1=costt[p1];
                p1=tata[p1];
            }
            else {

                    if(costt[p2]>cost1)
                     cost1=costt[p2];
                    p2=tata[p2];

            }
        }
        g<<cost1<<'\n';

    }
    f.close();
    return;
}
int main()
{
    SOLVE();
    return 0;
}