Cod sursa(job #3153145)

Utilizator RobertAcAcatrinei Robert-Marian RobertAc Data 28 septembrie 2023 13:28:05
Problema Radiatie Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.66 kb
#include <bits/stdc++.h>
//#define in cin
//#define out cout
using namespace std;

ifstream in("radiatie.in");
ofstream out("radiatie.out");

const int nmax = 1e4+5e3+7;
const int mmax = 2*nmax;

struct DSU
{
    struct DSUnode
    {
        int p,s;
        DSUnode()
        {
            p=0;
            s=1;
        }
    }nodes[nmax];
    int getP(int ind)
    {
        if(nodes[ind].p==0)return ind;
        return nodes[ind].p=getP(nodes[ind].p);
    }
    bool sameTree(int a,int b)
    {
        return getP(a)==getP(b);
    }
    void unite(int a,int b)
    {
        int pa=getP(a);
        int pb=getP(b);
        if(pa==pb)return;
        if(nodes[pa].s>nodes[pb].s)swap(pa,pb);
        nodes[pa].p=pb;
        nodes[pb].s+=nodes[pa].s;
    }
}root;

struct edge
{
    int a,b,c;
    edge()
    {
        c=0;
    }
    bool operator<(const edge &other)
    {
        return c<other.c;
    }
};

edge edges[mmax];
edge queries[nmax];

int n,m,k; 

int main()
{
    in>>n>>m>>k;
    for(int i=0;i<m;i++)
    {
        in>>edges[i].a>>edges[i].b>>edges[i].c;
    }
    sort(edges,edges+n);
    for(int i=0;i<k;i++)
    {
        in>>queries[i].a>>queries[i].b;
    }

    for(int i=0;i<m;i++)
    {
        if(!root.sameTree(edges[i].a,edges[i].b))
        {
            root.unite(edges[i].a,edges[i].b);
            for(int j=0;j<k;j++)
            {
                if(queries[j].c==0)
                {
                    if(root.sameTree(queries[j].a,queries[j].b))
                    {
                        queries[j].c=edges[i].c;
                    }
                }
            }
        }
    }
    for(int i=0;i<k;i++)
    {
        cout<<queries[i].c<<'\n';
    }
}