设为首页 加入收藏

TOP

HDU 3172 Virtual Friends(带权并查集)
2015-07-20 17:33:49 来源: 作者: 【 】 浏览:2
Tags:HDU 3172 Virtual Friends 查集

题目地址:HDU 3172

带权并查集水题。每次合并的时候维护一下权值。注意坑爹的输入。。

代码如下:

#include 
  
   
#include 
   
     #include 
    
      #include 
     
       #include 
      
        #include 
       
         #include 
        
          #include 
         
           #include 
           #include 
           
             #include 
            
              using namespace std; #define LL long long int bin[210000], num[210000]; int find1(int x) { return bin[x]==x?x:bin[x]=find1(bin[x]); } void join(int x, int y) { int f1=find1(x); int f2=find1(y); if(f1!=f2) { bin[f2]=f1; num[f1]+=num[f2]; printf("%d\n",num[f1]); } else printf("%d\n",num[f2]); } int main() { int t, n, i, cnt; char s1[30], s2[30]; map
             
              q; while(scanf("%d",&t)!=EOF) { while(t--) { scanf("%d",&n); for(i=1; i<210000; i++) { bin[i]=i; num[i]=1; } q.clear(); cnt=1; while(n--) { getchar(); scanf("%s%s",s1,s2); if(!q[s1]) q[s1]=cnt++; if(!q[s2]) q[s2]=cnt++; join(q[s1],q[s2]); } } } return 0; } 
             
            
           
         
        
       
      
     
    
   
  


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇HNU13028Attacking rooks (二分匹.. 下一篇HNU13033Football (贪心)

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·PostgreSQL 索引 - (2025-12-25 22:20:43)
·MySQL Node.js 连接 (2025-12-25 22:20:41)
·SQL 撤销索引、表以 (2025-12-25 22:20:38)
·Linux系统简介 (2025-12-25 21:55:25)
·Linux安装MySQL过程 (2025-12-25 21:55:22)