hdu 3926 特殊同構圖+並查集

來源:互聯網
上載者:User

關鍵是注意到,每個點的度至多為2,所以整個圖是由一些獨立的鏈和環構成的。然後統計某種鏈或是環的個數,如果同種鏈或環的數目相同,則是同構圖。

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;#define maxn 10010struct bcj {        int fa,num;        bool cir;        void init(){fa=cir=0,num=1;}    };struct Bcj {    bcj no[maxn];    void init(int n=maxn){        for(int i=1;i<=n;++i)            no[i].init();    }    int find(int p) {        return no[p].fa?no[p].fa=find(no[p].fa):p;    }    void uni(int p,int q) {        int i=find(p),j=find(q);        if(i!=j)            no[i].num<no[j].num?no[i].fa=j,no[j].num+=no[i].num:no[j].fa=i,no[i].num+=no[j].num;        else no[i].cir=true;    }}bc1,bc2;void solve(int &n,int &m,Bcj &bc) {    cin >> n >> m;    bc.init(n);    for(int i=1;i<=m;++i) {        int a,b;        cin >> a >> b;        bc.uni(a,b);    }}bool cmp(const bcj &a,const bcj &b) {    if(a.fa!=b.fa) return a.fa < b.fa;    if(a.cir!=b.cir) return a.cir < b.cir;    return a.num < b.num;}int main() {    ios::sync_with_stdio(false);    int T,cas=0;    cin >> T;    while(++cas<=T) {        cout << "Case #" << cas << ": ";        int n,m,n1,m1;        solve(n,m,bc1);        solve(n1,m1,bc2);        if(n!=n1||m!=m1) {            cout << "NO" << endl;            continue;        }        sort(bc1.no+1,bc1.no+1+n,cmp);        sort(bc2.no+1,bc2.no+1+n,cmp);        bool flag=true;        for(int i=1;i<=n&&(!bc1.no[i].fa||!bc1.no[i].fa);++i)            if((bc1.no[i].fa!=bc2.no[i].fa)               ||bc1.no[i].cir!=bc2.no[i].cir               ||bc1.no[i].num!=bc2.no[i].num) {                flag=false;break;            }        if(flag) cout << "YES" << endl;        else cout << "NO" << endl;    }    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.