标签:
#include<stdio.h> #include<algorithm> #include<string.h> using namespace std; int f[10000010] , maxn , vis[10000010]; struct edge { int u , v ; }e[100010]; int m , n , cnt ; void init (int n) { for (int i = 1 ; i <= n ; i++) { f[i] = i ; vis[i] = 0 ; } } /* int find (int x)//压缩路径 { if ( x != f[x]) { return f[x] = find (f[x]) ; } return f[x] ; }*/ int find(int x) //查找x元素所在的集合,回溯时压缩路径 { /* if (x != f[x]) { return f[x] = find(f[x]); //回溯时的压缩路径 } //从x结点搜索到祖先结点所经过的结点都指向该祖先结点 return f[x]; */ return x == f[x] ? x : f[x] = find(f[x]); } void binary (int key , int l , int r) { if ( l >= r) return ; int mid = l + r >> 1 ; binary ( key , l , mid ) ; binary ( key , mid + 1 , r ) ; if ( key == f[mid] ) { vis[mid] = 1 ; cnt++ ; } // printf ("%d\n" , f[mid] ) ; } int main () { freopen ("a.txt" , "r" , stdin ) ; int x , y , k; while (~ scanf ("%d" , &m) ) { n = 0 ; for (int i = 1 ; i <= m ; i++) { scanf ("%d%d" , &e[i].u , &e[i].v) ; k = max ( e[i].u , e[i].v ) ; n = max ( n , k ) ; } init (n) ; for (int i = 1 ; i <= m ; i++ ) { // printf ("e[%d].u=%d, e[%d].v= %d\n" , i,e[i].u ,i, e[i].v ) ; x = find (e[i].u) ; y = find (e[i].v) ; if ( x != y ) f[x] = y ; printf ("x=%d,y=%d\n" , x , y ) ; } for (int i = 1 ; i <= n ; i++ ) { printf ("%d " , f[i] ) ; } puts("") ; maxn = 0 ; for (int i = 1 ; i <= n ; i++) { cnt = 0 ; if ( !vis[i] ) { // printf ("key=%d\n" , f[i] ) ; binary (f[i] , i + 1 , n + 1 ) ; } vis[i] = 1 ; maxn = max (cnt , maxn) ; } printf ("%d\n" , maxn ) ; } return 0 ; }
标签:
原文地址:http://www.cnblogs.com/get-an-AC-everyday/p/4284950.html