杭电 3172 Virtual Friends【并查集 + map容器】



                                           Virtual Friends

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6041    Accepted Submission(s): 1705

Problem Description
These days, you can do all sorts of things online. For example, you can use various websites to make virtual friends. For some people, growing their social network (their friends, their friends' friends, their friends' friends' friends, and so on), has become an addictive hobby. Just as some people collect stamps, other people collect virtual friends.

Your task is to observe the interactions on such a website and keep track of the size of each person's network.

Assume that every friendship is mutual. If Fred is Barney's friend, then Barney is also Fred's friend.
 

Input
Input file contains multiple test cases.
The first line of each case indicates the number of test friendship nest.
each friendship nest begins with a line containing an integer F, the number of friendships formed in this frindship nest, which is no more than 100 000. Each of the following F lines contains the names of two people who have just become friends, separated by a space. A name is a string of 1 to 20 letters (uppercase or lowercase).
 

Output
Whenever a friendship is formed, print a line containing one integer, the number of people in the social network of the two people who have just become friends.
 

Sample Input
  
  
1 3 Fred Barney Barney Betty Betty Wilma
 

Sample Output
  
  
2 3 4
 
#include <map>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define Fin(IN) freopen(IN,"r",stdin)
const int maxn = 200000+20;//人数最多为 2N
int p[maxn],root_cnt[maxn];
map<string,int>Map;
int Find(int x) {return x == p[x]?x:p[x] = Find(p[x]);}
int Union(int a,int b){
    int pa = Find(a);
    int pb = Find(b);
    if(pa != pb) {p[pb] = pa;root_cnt[pa] += root_cnt[pb];}
    return root_cnt[pa];
}
int main(){
    //Fin("F:\\ACM刷题\\ACM专题训练\\in.txt");
    int T;
    while(cin>>T){
        while(T--){
            //cout<<"--------"<<endl;
            int n,cnt = 0;
            Map.clear();
            scanf("%d",&n);
            for(int i = 1;i < n*2;i++) {p[i] = i;root_cnt[i] = 1;}
            for(int i = 1;i <= n;i++){
                char a[25],b[25];
                scanf("%s%s",a,b);//scanf()效率比cin高
                //string a,b;
                //cin>>a>>b;
                if(!Map[a]) Map[a] = ++cnt;
                if(!Map[b]) Map[b] = ++cnt;
                int ans = Union(Map[a],Map[b]);
                //这样一个个扫描是肯定会TLE的
                //int ans = 0;
                //ans = root_cnt[a]
                /*const int CTP = p[Map[a]];
                for(int i = 1;i <= cnt;i++){
                    if(CTP == p[i]) ans++;
                }
                */
                cout<<ans<<endl;
            }
        }
    }
    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PVE(Proxmox Virtual Environment)是一个开源的虚拟化平台,可以用于运行虚拟机和容器,同时支持多节点集群。Ceph 是一个分布式存储系统,可以提供高可用、可扩展的存储服务。Kubernetes 是一个容器编排平台,可以自动化地部署、扩展和管理容器化应用程序。Docker 是一个开源的容器引擎,可以将应用程序及其依赖项打包成容器,以便在任何地方进行部署。 将这些技术结合在一起,可以构建一个高度可扩展、高可用性的容器平台。以下是一些实现步骤: 1. 在多个物理节点上安装 PVE,并创建 PVE 集群。 2. 在 PVE 集群上安装 Ceph,并将其配置为多节点 Ceph 集群,以提供高可用的存储服务。 3. 在 Kubernetes 集群中使用 Ceph RBD 存储插件,以便将容器挂载到 Ceph 存储中。 4. 安装 Docker 和 Kubernetes,使用 kubeadm 初始化 Kubernetes 集群,并加入节点。 5. 在 Kubernetes 集群中安装容器平台,如 OpenShift 或 Rancher,以便管理和部署容器化应用程序。 6. 配置容器平台以使用 Ceph 存储和 Kubernetes 负载均衡服务。 7. 在容器平台上创建应用程序和服务,并部署到 Kubernetes 集群中,以实现高度可扩展、高可用性的容器化应用程序。 需要注意的是,这些技术的组合和部署非常复杂,需要深入了解每个技术的工作原理和实现方式,同时需要考虑安全性、性能和可维护性等因素。因此,在实现这样的容器平台之前,建议进行充分的规划和测试,以确保平台的稳定性和可靠性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值