HDU 5486 Difference of Clustering 圖論

來源:互聯網
上載者:User

標籤:

Difference of Clustering

Time Limit: 1 Sec  

Memory Limit: 256 MB

題目串連http://acm.hdu.edu.cn/showproblem.php?pid=5486
Description

Given two clustering algorithms, the old and the new, you want to find the difference between their results.
A clustering algorithm takes many member entities as input and partition them into clusters. In this problem, a member entity must be clustered into exactly one cluster. However, we don’t have any pre-knowledge of the clusters, so different algorithms may produce different number of clusters as well as different cluster IDs. One thing we are sure about is that the memberIDs are stable, which means that the same member ID across different algorithms indicates the same member entity.
To compare two clustering algorithms, we care about three kinds of relationship between the old clusters and the new clusters: split, merge and 1:1. Please refer to the figure below.

 

Let’s explain them with examples. Say in the old result, m0, m1, m2 are clustered into one cluster c0, but in the new result, m0 and m1 are clustered into c0, but m2 alone is clustered into c1. We denote the relationship like the following:
● In the old, c0 = [m0, m1, m2]
● In the new, c0 = [m0, m1], c1 = [m2]
There is no other members in the new c0 and c1. Then we say the old c0 is split into new c0 and new c1. A few more examples:
● In the old, c0 = [m0, m1, m2]
● In the new, c0 = [m0, m1, m2].
This is 1:1.
● In the old, c0 = [m0, m1], c1 = [m2]
● In the new, c0 = [m0, m1, m2]
This is merge. Please note, besides these relationship, there is another kind called “n:n”:
● In the old, c0 = [m0, m1], c1 = [m2, m3]
● In the new, c0 = [m0, m1, m2], c1 = [m3]
We don’t care about n:n.
In this problem, we will give you two sets of clustering results, each describing the old and the new. We want to know the total number of splits, merges, and 1:1 respectively.

Input

The first line of input contains a number T indicating the number of test cases (T≤100).
Each test case starts with a line containing an integer N indicating the number of member entities (0≤N≤106 ). In the following N lines, the i-th line contains two integers c1 and c2, which means that the member entity with ID i is partitioned into cluster c1 and cluster c2 by the old algorithm and the new algorithm respectively. The cluster IDs c1 and c2 can always fit into a 32-bit signed integer.

Output

For each test case, output a single line consisting of “Case #X: A B C”. X is the test case number starting from 1. A, B, and C are the numbers of splits, merges, and 1:1s.

Sample Input

2
3
0 0
0 0
0 1
4
0 0
0 0
1 1
1 1

Sample Output

Case #1: 1 0 0
Case #2: 0 0 2

HINT

 

題意

給你很多個一開始的集合,和結束時候的集合

並且告訴你具體的這些元素是怎麼移動的

你要分別算出 分離、合并、1:1這三種操作有多少種

分離就是1個集合變成了多個集合,合并就是多個集合變成了一個集合,1:1就是一個變成了一個

題解:

當成圖論做的……

分離操作就是這個集合的邊集>1,這個集合連的所有集合的邊集都為1

1:1就是這個集合的邊集=1,這個集合連的集合的邊集也為1

合併作業就是分離操作的逆運算,swap一下,再跑一遍分離就好了

代碼:

//qscqesze#pragma comment(linker, "/STACK:1024000000,1024000000")#include <cstdio>#include <cmath>#include <cstring>#include <ctime>#include <iostream>#include <algorithm>#include <set>#include <bitset>#include <vector>#include <sstream>#include <queue>#include <typeinfo>#include <fstream>#include <map>#include <stack>typedef long long ll;using namespace std;//freopen("D.in","r",stdin);//freopen("D.out","w",stdout);#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)#define maxn 1000006#define mod 1000000007#define eps 1e-9#define e exp(1.0)#define PI acos(-1)const double EP  = 1E-10 ;int Num;//const int inf=0x7fffffff;const ll inf=999999999;inline ll read(){    ll x=0,f=1;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;}//*************************************************************************************vector<int> Q;map<int,int> H;struct node{    int x,y;};node p[maxn];int b[maxn];vector<int> Q1[maxn];int main(){    int t=read();    for(int cas=1;cas<=t;cas++)    {        int n=read();        H.clear();        for(int i=1;i<=n;i++)        {            p[i].x=read(),p[i].y=read();            Q.push_back(p[i].x);            Q.push_back(p[i].y);        }        sort(Q.begin(),Q.end());        Q.erase(unique(Q.begin(),Q.end()),Q.end());        int len = Q.size();        for(int i=0;i<len;i++)            H[Q[i]]=i;        for(int i=0;i<len;i++)            Q1[i].clear(),b[i]=0;        Q.clear();        for(int i=1;i<=n;i++)        {            Q1[H[p[i].x]].push_back(H[p[i].y]);        }        for(int i=0;i<len;i++)        {            sort(Q1[i].begin(),Q1[i].end());            Q1[i].erase(unique(Q1[i].begin(),Q1[i].end()),Q1[i].end());        }        for(int i=0;i<len;i++)        {            for(int j=0;j<Q1[i].size();j++)                b[Q1[i][j]]++;        }        int ans1 = 0,ans2 = 0,ans3 = 0;        for(int i=0;i<len;i++)        {            if(Q1[i].size()==0)continue;            if(Q1[i].size()==1)            {                if(b[Q1[i][0]]==1)                    ans3++;            }            else            {                int flag = 1;                for(int j=0;j<Q1[i].size();j++)                {                    if(b[Q1[i][j]]!=1)                    {                        flag = 0;                        break;                    }                }                if(flag)                    ans1++;            }        }        for(int i=0;i<len;i++)            Q1[i].clear(),b[i]=0;        for(int i=1;i<=n;i++)        {            Q1[H[p[i].y]].push_back(H[p[i].x]);        }        for(int i=0;i<len;i++)        {            sort(Q1[i].begin(),Q1[i].end());            Q1[i].erase(unique(Q1[i].begin(),Q1[i].end()),Q1[i].end());        }        for(int i=0;i<len;i++)        {            for(int j=0;j<Q1[i].size();j++)                b[Q1[i][j]]++;        }        for(int i=0;i<len;i++)        {            if(Q1[i].size()==0)continue;            if(Q1[i].size()==1)            {                if(b[Q1[i][0]]==1)                    ans3++;            }            else            {                int flag = 1;                for(int j=0;j<Q1[i].size();j++)                {                    if(b[Q1[i][j]]!=1)                    {                        flag = 0;                        break;                    }                }                if(flag)                    ans2++;            }        }        printf("Case #%d: %d %d %d\n",cas,ans1,ans2,ans3/2);    }}

 

HDU 5486 Difference of Clustering 圖論

聯繫我們

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