【poj1962】 Corporative Network

來源:互聯網
上載者:User

標籤:

http://poj.org/problem?id=1962 (題目連結)

時隔多年又一次寫帶權並查集。

題意:n個節點,若干次詢問,I x y表示從x連一條邊到y,權值為|x-y|%1000;E x表示詢問x到x所指向的終點的距離。

Solution 
  很裸的帶權並查集。

代碼:

// poj1962#include<algorithm>#include<iostream>#include<cstdlib>#include<cstring>#include<cstdio>#include<cmath>#include<string>#define LL long long#define MOD 1000#define inf 2147483640#define Pi acos(-1.0)#define free(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);using namespace std;int getint() {    int f=1,x=0;char ch=getchar();    while (ch<=‘0‘ || ch>‘9‘) {if (ch==‘-‘) f=-1;ch=getchar();}    while (ch>=‘0‘ && ch<=‘9‘) {x=x*10+ch-‘0‘;ch=getchar();}    return x*f;}const int maxn=20010;int r[maxn],fa[maxn],n;int find(int x) {    if (x==fa[x]) return x;    int f=find(fa[x]);    r[x]=r[x]+r[fa[x]];    fa[x]=f;    return f;}int main() {    int T;scanf("%d",&T);    while (T--) {        scanf("%d",&n);        for (int i=1;i<=n;i++) fa[i]=i,r[i]=0;        char ch[10];        while (scanf("%s",ch)!=EOF) {            int x,y;            if (ch[0]==‘O‘) break;            else if (ch[0]==‘E‘) {                scanf("%d",&x);                find(x);                printf("%d\n",r[x]);            }            else {                scanf("%d%d",&x,&y);                fa[x]=y;                r[x]=abs(x-y)%MOD;            }        }    }    return 0;}

  

【poj1962】 Corporative Network

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.