Cod sursa(job #227698)

Utilizator toni2007Pripoae Teodor Anton toni2007 Data 5 decembrie 2008 11:05:46
Problema Radiatie Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 5.48 kb
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <ctime>
#include <string>
#include <algorithm>
#include <queue>
#include <deque>
#include <vector>
#include <set>
#include <bitset>
#include <map>
#include <stack>
#define pb push_back
#define mp make_pair
#define mt make_triple
#define For(i,a,b) for (i = a; i <= b; ++i)
#define INF 100000
#define INF2 100000000
#define FIN "radiatie.in"
#define FOUT "radiatie.out"
#define N 15010
#define M 100000
using namespace std;
int n,m,p;
vector<int> cine[N],cost[N];
int a,b,c,d,x,y,xp,yp;
int lev[N];
struct triplet {	int first,second,third;  }; 
vector<triplet> Muchie;
triplet make_triple(int a,int b,int c){	triplet x;	x = (triplet) { a, b, c};	return x;}
inline bool cmp(triplet a,triplet b){	return a.third < b.third; }
int par[N];
bool dif_comp(int x, int y) {
	int p1 = x, p2 = y;
	while (par[p1] != p1)
		p1 = par[p1];
	while (par[p2] != p2) 
		p2 = par[p2];
	
	if (p1 != p2) 	return true;
	else 			return false;
}

void merge_comp(int x, int y) {
	int p1 = x, p2 = y, ant, pp;
	while (par[p1] != p1) 
		p1 = par[p1];
	pp = p1;p1 = x;
	while (par[p1] != pp) {
		ant = p1;
		p1 = par[p1];
		par[ant] = pp;
	}
	while (par[p2] != pp) {
		ant = p2;
		p2 = par[p2];
		par[ant] = pp;
	}
}

void APM() {
	int i, ant, p, pp;
	for (i = 1; i <= n; ++i)
		par[i] = i;
	for (i = 0; i < m; ++i) {
		if (dif_comp(Muchie[i].first, Muchie[i].second)) {
			cine[Muchie[i].first].push_back(Muchie[i].second);
			cost[Muchie[i].first].push_back(Muchie[i].third);
			cine[Muchie[i].second].push_back(Muchie[i].first);
			cost[Muchie[i].second].push_back(Muchie[i].third);
			merge_comp(Muchie[i].first, Muchie[i].second);
		}
	}	
	for (i = 1; i <= n; ++i)
		if (par[i] == i) {
			cine[i].push_back(0);
			cine[0].push_back(i);
			cost[0].push_back(0);
			cost[i].push_back(0);
			par[i] = 0;
		}
}
void scan(){
    int i;
    freopen(FIN,"r",stdin);
    freopen(FOUT,"w",stdout);
    scanf("%d%d%d",&n,&m,&p);
    for (i = 1; i <= m; ++i){
		scanf("%d%d%d",&a,&b,&c);
		Muchie.pb(mt(a,b,c));
	}
	sort(Muchie.begin(), Muchie.end(), cmp);
	APM();
}
vector<int> euler,sol,high;
int z,viz[N],pozi[N],k=0;
int t1[N],t2[N],tt;
int tata[N],last[N];
void do_euler(int x,int level,int las,int c){
    int i;
    viz[x]=1;lev[x]=level;
    tata[x]=las;
    last[x]=c;
    t1[x]=++tt;
    euler.push_back(x);
    high.push_back(level);
    int k;
    k=euler.end()-euler.begin()-1;
    if (pozi[x]>k)
        pozi[x]=k;
    for (i=0;i<cine[x].end()-cine[x].begin();++i)
        if (!viz[cine[x][i]]){
            do_euler(cine[x][i],level+1,x,cost[x][i]);
            euler.push_back(x);
            high.push_back(level);
            ++tt;
        }
    t2[x]=tt;
}
int val,pos,start,finish;
int arb[M];
void update(int nod,int l,int r){
    if (l==r){
        arb[nod]=val;
        return;
    }
    int mid=(l+r)>>1;
    if (pos<=mid) update(2*nod,l,mid);
    else          update(2*nod+1,mid+1,r);
    if (high[arb[2*nod]]>high[arb[2*nod+1]])
        arb[nod]=arb[2*nod+1];
    else
        arb[nod]=arb[2*nod];
}
int lca;
void query(int nod,int l,int r){
    if (start<=l && r<=finish){
        if (high[arb[nod]]<high[lca] || lca==0)
            lca=arb[nod];
        return;
    }
    int mid;
    mid=(l+r)>>1;
    if (start<=mid) query(2*nod,l,mid);
    if (mid<finish) query(2*nod+1,mid+1,r);
}
void pre_arbint(){
    int nn=euler.size()-1,i;
    for (i=1;i<=nn;++i){
        pos=i;
        val=i;
        update(1,1,nn);
    }
}
int str[N][20],best[N][20];
void calc(){
    int i,j;
    //printf("%d\n",n);
    for (i = 0; i <= n; ++i){
        str[i][0] = tata[i]; assert(tata[i] >= 0 && tata[i] <= n);
        best[i][0] = last[i];
    }
    for (i = 1; i <= n; ++i)
        for (j = 1; (1 << j) <= n; ++j)
            best[i][j]=str[i][j]=INF2;
    for (i = 1; i <= n; ++i)
        for (j = 1; (1 << j) <= n; ++j)
            str[i][j] = str[str[i][j-1]][j-1];
    /*for (i = 1; i <= n; ++i)
        for (j = 1; (1 << j) <= n; ++j)
            best[i][j]=max(best[i][j-1],best[str[i][j-1]][j-1]);
   /* for (i=1;i<=n;++i){
        for (j=0;(1 << j)<=n;++j)
            printf("(%d %d) ",str[i][j],best[i][j]);
        printf("\n");
    }*/
}
int rez_rmq;
void query_rmq(int x,int y){
    int p,q,f,j,now;
    p=lev[x];
    q=lev[y];
    f=p-q;now=x;
    //fprintf(stderr,"%d %d\n",x,y);
    while (f){
        for (j=0;(1<<j)<f;++j);if (j)--j;
        //fprintf(stderr,"%d %d %d %d\n",x,f,now,j);
        rez_rmq=max(rez_rmq,best[now][j]);
        now=str[now][j];
        p=lev[now];
        f=p-q;
    }
}
void solve(){
    int i,s2,s1,bla_now;
    for (i=1;i<=n;++i)
        pozi[i]=INF;
    euler.push_back(0);
    high.push_back(0);
    do_euler(1,1,0,0);
    pre_arbint();
    calc();
    sol.push_back(0);
    /*for (i=1;i<=p;++i){
    	scanf("%d%d",&x,&y);
        if (t1[x]<t1[y] && t2[y]<t2[x])
            lca=x;
        else if(t1[y]<t1[x] && t2[x]<t2[y])
            lca=y;
        else{
            lca=0;
            s1=pozi[x];
            s2=pozi[y];
            start=min(s1,s2);
            finish=max(s1,s2);
            query(1,1,euler.size()-1);
            lca=euler[lca];
        }
        if (x==y)
            bla_now=0;
        else{
            rez_rmq=0;
            query_rmq(x,lca);
            bla_now=rez_rmq;
            rez_rmq=0;
            query_rmq(y,lca);
            bla_now=max(bla_now,rez_rmq);
        }
        printf("%d\n",bla_now);
    }*/
}
void print(){
    int i;
    for (i=sol.size()-p;i<sol.size();++i)
        printf("%d\n",sol[i]);
}
main(){
    scan();
    solve();
   // print();
}