Timus Online Judge 2055 Urban Geography 動態樹維護mst(第44case逾時了)---留著以後做吧

來源:互聯網
上載者:User

2055. Urban Geography Time limit: 2.0 second
Memory limit: 128 MB
Android Vasya prepares a project in Urban geography. The aim of the project is to improve the infrasructure of the city he lives in. Now the city consists of  n districts, some of which are connected by roads. Using these roads one can get from any district to any other district of the city by car. Vasya thinks that such big amount of roads makes citizens use their own cars instead of walking or cycling. He wants to close as many roads for cars as possible and turn them into boulevards. Of course, Vasya wants to keep the possibility to get from any district to any other district of the city by car using roads. Now citizens pay for using roads, and prices for different roads may vary very much. Vasya thinks that leaving open some expensive and some cheap roads at the same time is not a good idea beacuse it can increase social tension in the city. That’s why he wants to minimize the price spread between the most expensive and the cheapest roads. Help Vasya choose the roads to keep open. Input The first line contains integers  n и  m that are the number of city districts and roads correspondingly (2 ≤  n ≤ 50 000;  n − 1 ≤  m ≤ 50 000). The next  m lines contain triples of integers  a i,  b i and  c i, meaning that between the city districts  a i and  b i there is a road with the price  c i (1 ≤  a i,  b i ≤  n;  a i ≠  b i; 1 ≤  c i ≤ 10 9). There can be several roads between two districts. Output In the only line output the sequence of integers — numbers of the roads which should be kept open in the city. The roads are numbered as they appear in the input data. If there are several solutions, output any of them. Samples

input output
3 31 2 12 3 33 1 4
2 3
4 51 2 12 3 11 3 21 4 22 4 1
1 2 5
Problem Author: Nikita Burlakov
Problem Source: Ural Sport Programming Championship 2015


構建一棵樹,是的最長邊-最短邊最小。

用動態樹維護mst。更新最小距離。

動態樹的建發:

對每個點建立一個結點。對每條邊建立一個結點。加邊<u,v>的時候,如果u,v在同一棵樹上

把u,v,access到同一個splay中,找到邊最小的結點x,cut(x,v),cut(x,u)

把u,v分別變成自己所在樹的根,然後對於邊建立點x,link(x,u),link(x,v)即可。

動態樹要維護的是樹中邊的權值。

44case T了一天了。實在沒辦法,換一種方法做吧,下次回來做這題



#include<cstdio>#include<cstring>#include<algorithm>#include<vector>#include<set>using namespace std;#define maxn 400007#define inf  1000000000int cnt = 0;struct Node{    Node *fa,*ch[2];    bool root,rev;    int val,mval;    Node(){}    Node* init(int v,Node *f){        fa = f;        ch[0] = ch[1] = f;        root = true;        val = mval = v;        rev = false;        return this;    }    void update(){        mval = val;        if(ch[0]->mval < mval)            mval = ch[0]->mval;        if(ch[1]->mval < mval)            mval = ch[1]->mval;    }    void update_rev(){        if(val == inf + 10000000)return ;        rev = !rev;        swap(ch[0],ch[1]);    }    void pushdown(){        if(rev){            ch[0]->update_rev();            ch[1]->update_rev();            rev = false;        }    }    void rotate(){        Node *f = fa, *ff = f->fa;        int t = (f->ch[1] == this);        if(f->root)            root = true, f->root = false;        else ff->ch[ff->ch[1] == f] = this;        fa = ff;        f->ch[t] = ch[t^1];        ch[t^1]->fa = f;        ch[t^1] = f;        f->fa = this;        f->update();    }    void push(){        if(!root) fa->push();        pushdown();    }    void splay(){        push();        Node *f, *ff;        while(!root){            f = fa,ff = f->fa;            if(!f->root)                if((ff->ch[1]==f)&&(f->ch[1] == this)) f->rotate();                else rotate();            rotate();        }        update();    }};Node pool[maxn];Node *nil,*tree[maxn];void init(){    cnt = 1;    nil = tree[0] = pool;    nil->val = nil -> mval = inf+10000000;}//將x到樹根的路徑並成一條path******Node *access(Node *x){    Node *y = nil;    while(x != nil){        x->splay();        x->ch[1]->root = true;        (x->ch[1] = y)->root = false;        x->update();        y = x;        x = x->fa;    }    return y;}//將結點x變成樹根******void be_root(Node *x){    //cout<<x->val-inf<<endl;    access(x);    x->splay();    x->update_rev();    //update_rev(x);}//將x串連到結點f上******void link(Node *x, Node *f){    be_root(x);    x->fa = f;}//將x,y分離******void cut(Node *x,Node *y){    be_root(x);    access(x);    y->splay();    y->fa = nil;}Node *findminNode(Node *x){    if(x->val <= x->ch[0]->mval && x->val <= x->ch[1]->mval)        return x;    if(x->ch[0]->mval < x->ch[1]->mval)        return findminNode(x->ch[0]);    return findminNode(x->ch[1]);}Node *findRoot(Node *x){    if(x->ch[0] == nil) return x;    return findRoot(x->ch[0]);}struct Edge{    int u,v,w;    Edge (){}    int getn(){        int n = 0;        char x;        while(1){            x = getchar();            if(x == ' ' || x == '\n') return n;            n = n*10+x-'0';        }    }    void init(){        u = getn();        v = getn();        w = getn();    }};Edge edge[maxn];int fa[100007];int find(int u){    if(fa[u]  == u) return u;    return fa[u] = find(fa[u]);}multiset<int>haha;int comp1(int a,int b){    return edge[a].w < edge[b].w;}int id[100007];int main(){    //freopen("j.in","r",stdin);    //freopen("jl.out","w",stdout);    int n,m;    while(scanf("%d%d",&n,&m)!=EOF){        haha.clear();        //edge.clear();        Edge e;        getchar();        for(int i = 0;i < m; i++){            edge[i].init();            if(edge[i].u == edge[i].v){                m--;                i--;                continue;            }            id[i] = i;        }        sort(id,id+m,comp1);        init();        for(int i = 1;i <= n;i++)            tree[i] = pool[cnt++].init(inf+i,nil);        int num = n,ans=1000000000,be,en;        Node *x,*y,*z;        for(int i = 0;i < m; i++){            int u = edge[id[i]].u,v = edge[id[i]].v,w=edge[id[i]].w;            haha.insert(w);            z = pool[cnt++].init(w,nil);            be_root(tree[u]);            x = access(tree[v]);            y = findRoot(x);            if(tree[u] == y){                x = findminNode(x);                cut(x,tree[u]);                cut(x,tree[v]);                haha.erase(haha.find(x->val));                num++;            }            link(tree[u],z);            link(tree[v],z);            num--;            if(num == 1){                int mval = *haha.begin();                if(w - mval < ans){                    ans = w - mval;                    be = mval;                    en = w;                }            }        }        num = n;        for(int i = 0;i <= n; i++)            fa[i] = i;        for(int i = 0;i < m; i++){            e = edge[i];            if(e.w >= be && e.w <= en){                int f1 = find(e.u);                int f2 = find(e.v);                if(f1 != f2){                    fa[f1] = fa[f2];                    if(num != n) putchar(' ');                    printf("%d",i+1);                    num--;                }            }        }        printf("\n");    }    return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.