Cod sursa(job #2226802)

Utilizator tifui.alexandruTifui Ioan Alexandru tifui.alexandru Data 30 iulie 2018 17:38:58
Problema Radiatie Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.45 kb
#include <bits/stdc++.h>
#define Nmax 15005
#define Mmax 30005
#define DIM 70000
using namespace std;

class Ifstream
{
private:
    FILE *f;
    char *buffer;
    int cursor;
    char read_ch()
    {
        if(++cursor==DIM)
        {
            cursor=0;
            fread(buffer,1,DIM,f);
        }
        return buffer[cursor];
    }

public:
    Ifstream(const char *file_name)
    {
        f=fopen(file_name,"r");
        buffer=new char[DIM]();
        cursor=DIM-1;
    }

    Ifstream &operator >> (int &n)
    {
        char c;
        n=0;
        while(!isdigit(c=read_ch()) and c!='-');

        int sgn=1;
        if(c=='-') sgn=-1;
        else n=c-'0';

        while(isdigit(c=read_ch()))
            n=10*n+c-'0';
        n*=sgn;

        return *this;
    }

};

class Ofstream
{
private:
    FILE *g;
    char *buffer;
    int cursor;
    void write_ch(char ch)
    {
        if(cursor==DIM)
        {
            fwrite(buffer,1,DIM,g);
            cursor=0;
        }
        buffer[cursor++]=ch;
    }

public:
    Ofstream(const char *file_name)
    {
        g=fopen(file_name,"w");
        buffer=new char[DIM]();
        cursor=0;
    }

    ~Ofstream()
    {
        fwrite(buffer,1,cursor,g);
        fclose(g);
    }

    Ofstream &operator << (int n)
    {
        if(n<10) write_ch(n+'0');
        else
        {
            (*this)<<(n/10);
            write_ch(n%10+'0');
        }
        return *this;
    }

    Ofstream &operator << (char c)
    {
        write_ch(c);
        return *this;
    }

};

Ifstream f("radiatie.in");
Ofstream g("radiatie.out");

struct edge
{
    int l,r,val;
    bool operator < (const edge &other) const
    {
        return this->val<other.val;
    }
};

edge E[Mmax];
int t[Nmax],r[Nmax],v[Nmax];
int n,m,Q;
list < pair <int,int> > tree[Nmax];

int Find(int x)
{
    int root=x;
    while(root!=t[root])
        root=t[root];
    int aux;
    while(x!=t[x])
    {
        aux=t[x];
        t[x]=root;
        x=aux;
    }
    return root;
}

void Unite(int x, int y)
{
    if(r[x]<r[y]) t[x]=y;
    else t[y]=x;
    if(r[x]==r[y]) r[x]++;
}

void Kruskal()
{
    sort(E+1,E+m+1);
    int i,x,y,nr;
    for(i=1;i<=n;i++)
    {
        t[i]=i;
        r[i]=1;
    }
    i=1;
    nr=0;
    while(nr<n-1)
    {
        x=Find(E[i].l);
        y=Find(E[i].r);
        if(x!=y)
        {
            Unite(x,y);
            tree[E[i].l].push_back({E[i].r,E[i].val});
            tree[E[i].r].push_back({E[i].l,E[i].val});
            ++nr;
        }
        ++i;
    }
    for(i=1;i<=n;i++)
        r[i]=t[i]=0;
}

void DFS(int x)
{
    for(const auto &it:tree[x])
        if(!r[it.first])
        {
            r[it.first]=r[x]+1;
            t[it.first]=x;
            v[it.first]=it.second;
            DFS(it.first);
        }
}

int LCA(int x, int y)
{
    int ans=INT_MIN;
    while(x!=y)
    {
        if(r[x]<r[y])
        {
            ans=max(ans,v[y]);
            y=t[y];
        }
        else
        {
            ans=max(ans,v[x]);
            x=t[x];
        }
    }
    return ans;
}

int main()
{
    f>>n>>m>>Q;
    int x,y,cnt;
    for(cnt=1;cnt<=m;cnt++)
        f>>E[cnt].l>>E[cnt].r>>E[cnt].val;
    Kruskal();
    r[1]=1;
    DFS(1);
    while(Q--)
    {
        f>>x>>y;
        g<<LCA(x,y)<<'\n';
    }

    return 0;
}