Cod sursa(job #1555990)

Utilizator papinubPapa Victor papinub Data 23 decembrie 2015 21:36:08
Problema Radiatie Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.95 kb
# include <cstdio>
# include <vector>
# include <algorithm>
# define pb push_back
using namespace std;
FILE *f=freopen("radiatie.in","r",stdin);
FILE *g=freopen("radiatie.out","w",stdout);
const int NMAX= 15001;
const int bufferDim= 10000;
class inputReader {

private:
        int pos;
        char buffer[bufferDim];
public:

        void getBuffer() {

            pos= 0;
            fread(buffer, 1, bufferDim, stdin);
        }

        bool digit(char c) {

            return ('0' <= c && c <= '9');
        }

        void getInt(int &nr) {

            while(!digit(buffer[pos]))
                if(++pos == bufferDim)
                    getBuffer();
            nr = 0;
            while(digit(buffer[pos])) {

                nr = nr * 10 + buffer[pos] - '0';
                if(++pos == bufferDim)
                    getBuffer();
            }
        }
} cin;


struct muchie{
    int a,b,cost;

    muchie (const int &a, const int &b, const int &cost)
    {
        this->a=a;
        this->b=b;
        this->cost=cost;
    }
    bool operator < (const muchie &other) const
    {
        return this->cost < other.cost;
    }
};
int n,m,q;
vector <muchie> candidati;
int father[NMAX];
int cost[NMAX];
int depth[NMAX];
bool rez[NMAX];
int group(int x)
{
    if (x!=father[x])
        x=group(father[x]);
    return x;
}
void getDepth(int node) {

    if(node == father[node])
        return;
    getDepth(father[node]);
    depth[node] = depth[father[node]] + 1;
}
void read()
{
    cin.getBuffer();
    cin.getInt(n);
    cin.getInt(m);
    cin.getInt(q);
    for (int i=1;i<=m;i++)
    {
        int x,y,c;
        cin.getInt(x);
        cin.getInt(y);
        cin.getInt(c);
        candidati.pb(muchie(x,y,c));
    }
}
void getAPM()
{
    sort(candidati.begin(), candidati.end());
    for(int i = 1; i <= n; ++i) father[i] = i;

    for (int i=0;i<m;i++)
    {
        muchie mc=candidati[i];
        int x=group(mc.a);
        int y=group(mc.b);
        if (x!=y)
        {
            father[x]=y;
            cost[x]=candidati[i].cost;
        }
    }
    //for(int i = 1; i <= m; ++i)
        //if(!depth[i])
            //getDepth(i);
    for ( int i= 1; i<=n; ++i )
    {
        for ( int j= i; j!=father[j]; j= father[j] )
        {
            depth[i]++;
        }
    }
}
int query(int x, int y)
{
    int maxim = 0;

    while (depth[x]>depth[y])
    {
        maxim = max(cost[x], maxim);
        x=father[x];
    }
    while (depth[y]>depth[x])
    {
        maxim = max(cost[y], maxim);
        y=father[y];
    }
    while (x!=y)
    {
        maxim = max(cost[x], max(cost[y], maxim));
        x=father[x];
        y=father[y];
    }
    return maxim;
}
int main()
{
    read();
    getAPM();
    for(; q; --q) {

        int x, y;
        cin.getInt(x); cin.getInt(y);
        printf("%d\n", query(x, y));
    }
    return 0;
}