| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 434 人关注过本帖
标题:不知道为什么 , 路径压缩不完全, 求指教
只看楼主 加入收藏
windthenrain
Rank: 2
等 级:论坛游民
帖 子:21
专家分:15
注 册:2014-10-27
结帖率:100%
收藏
 问题点数:0 回复次数:0 
不知道为什么 , 路径压缩不完全, 求指教
More is better
Time Limit:1000MS     Memory Limit:102400KB     64bit IO Format:%I64d & %I64u
Submit
 
Status
Description
Mr Wang wants some boys to help him with a project. Because the project is rather complex, the more boys come, the better it will be. Of course there are certain requirements.

Mr Wang selected a room big enough to hold the boys. The boy who are not been chosen has to leave the room immediately. There are 10000000 boys in the room numbered from 1 to 10000000 at the very beginning. After Mr Wang's selection any two of them who are still in this room should be friends (direct or indirect), or there is only one boy left. Given all the direct friend-pairs, you should decide the best way.
 
Input
The first line of the input contains an integer n (0 ≤ n ≤ 100 000) - the number of direct friend-pairs. The following n lines each contains a pair of numbers A and B separated by a single space that suggests A and B are direct friends. (A ≠ B, 1 ≤ A, B ≤ 10000000)
 
Output
The output in one line contains exactly one integer equals to the maximum number of boys Mr Wang may keep.
 
Sample Input
 4
1 2
3 4
5 6
1 6
4
1 2
3 4
5 6
7 8
5
1 5
5 6
6 7
2 3
2 4
 
Sample Output
 4
2
4
Hint
 A and B are friends(direct or indirect), B and C are friends(direct or indirect), then A and C are also friends(indirect). In the first sample {1,2,5,6} is the result. In the second sample {1,2},{3,4},{5,6},{7,8} are four kinds of answers.
         
 题意:就是输出 无相图中 最大集合的元素的数量

我的输出:
e[1].u=1, e[1].v= 2
e[2].u=3, e[2].v= 4
e[3].u=5, e[3].v= 6
e[4].u=1, e[4].v= 6
2 6 4 4 6 6//应该是 6 6 4 4 6 6
3
e[1].u=1, e[1].v= 2
e[2].u=3, e[2].v= 4
e[3].u=5, e[3].v= 6
e[4].u=7, e[4].v= 8
2 2 4 4 6 6 8 8//这个倒是对=。=
2
e[1].u=1, e[1].v= 5
e[2].u=5, e[2].v= 6
e[3].u=6, e[3].v= 7
e[4].u=2, e[4].v= 3
e[5].u=2, e[5].v= 4
5 3 4 4 6 7 7//应该是7 4 4 4 6 7 7
1


我的代码:
#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++) {//赋值 , 并求得总定点数n
            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) ;
            f[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 + 1 ) ;
    }
    return 0 ;
}

能帮我看看那错了吗?

[ 本帖最后由 windthenrain 于 2015-2-10 21:15 编辑 ]
搜索更多相关主题的帖子: selected Because complex course Memory 
2015-02-10 21:06
快速回复:不知道为什么 , 路径压缩不完全, 求指教
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.033132 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved