1899-Virtual Friends(map+并查集) ZCMU

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

The first line of input contains one integer specifying the number of test cases to follow. Each test case begins with a line containing an integer F, the number of friendships formed, 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<bits/stdc++.h>
const int MAX=100005;
using namespace std;
int farther[MAX],fri[MAX];
//fri[i]为编号为i的人的朋友圈人数
map<string,int> mark; //map记录每个人的编号
//初始化
void init()
{
    for(int i=1;i<=MAX;i++)
    {
        farther[i]=i; fri[i]=1; //每个人初始的朋友圈都是1,因为只有自己
    }
    mark.clear();
}
//并查集找父亲
int getfarther(int x)
{
    if(x!=farther[x])
        farther[x]=getfarther(farther[x]);
    return farther[x];
}
void union_set(int x,int y)
{
    x=getfarther(x);
    y=getfarther(y);
    if(x==y) //如果两人已经是朋友,那么只要输出一个人的朋友圈就可
        printf("%d\n",fri[x]);
    else ////结交了朋友圈外的朋友的时候,直接指定一个人的朋友圈为最后要输出的朋友圈,然后输出他们两个的朋友圈之和
    {
        farther[x]=y;
        fri[y]+=fri[x];
        printf("%d\n",fri[y]);
    }
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n,num=1;
        string p1,p2;
        scanf("%d",&n); init();
        for(int i=1;i<=n;i++)
        {
            cin>>p1>>p2;
            if(!mark[p1])
                mark[p1]=num++;
            if(!mark[p2])
                mark[p2]=num++;
            union_set(mark[p1],mark[p2]);
        }
    }
    return 0;
}

 

### 关于 ZCMU 1912 的 IT 内容或问题 ZCMU 1912 可能是指浙江大学城市学院(Zhejiang University City College,简称 ZCMU)的一个特定项目、竞赛题目或技术挑战。通常,这类编号可能出现在 ACM/ICPC 程序设计竞赛、算法挑战或其他技术性活动中[^1]。以下是对该问题的详细分析和解决方案。 #### 问题背景 在 ACM/ICPC 或其他类似的编程竞赛中,编号如 1912 通常用于标识一个具体的算法问题或任务。这些问题通常涉及数据结构、算法设计、数学建模等领域。例如,ZCMU 1912 可能要求解决一个与字符串处理、动态规划、图论或数论相关的问题[^2]。 #### 解决方案概述 为了提供一个完整的解决方案,需要明确 ZCMU 1912 的具体问题描述。然而,基于常见的竞赛题型,可以推测出以下几种可能的场景及其对应的解决方法: 1. **字符串处理**:如果问题涉及字符串匹配或模式识别,可以使用 KMP 算法或正则表达式来高效地解决问题。 ```python def kmp_search(text, pattern): lps = [0] * len(pattern) compute_lps(pattern, lps) i = j = 0 while i < len(text): if pattern[j] == text[i]: i += 1 j += 1 if j == len(pattern): return True elif i < len(text) and pattern[j] != text[i]: if j != 0: j = lps[j - 1] else: i += 1 return False ``` 2. **动态规划**:如果问题涉及优化路径或资源分配,动态规划是一种常用的方法。例如,背包问题可以通过以下代码实现: ```python def knapsack(weights, values, capacity): n = len(weights) dp = [[0 for _ in range(capacity + 1)] for _ in range(n + 1)] for i in range(1, n + 1): for w in range(1, capacity + 1): if weights[i - 1] <= w: dp[i][w] = max(dp[i - 1][w], dp[i - 1][w - weights[i - 1]] + values[i - 1]) else: dp[i][w] = dp[i - 1][w] return dp[n][capacity] ``` 3. **图论**:如果问题涉及最短路径或连通性,Dijkstra 或 Floyd-Warshall 算法可能是适用的选择。 ```python import heapq def dijkstra(graph, start): distances = {node: float('inf') for node in graph} distances[start] = 0 priority_queue = [(0, start)] while priority_queue: current_distance, current_node = heapq.heappop(priority_queue) if current_distance > distances[current_node]: continue for neighbor, weight in graph[current_node].items(): distance = current_distance + weight if distance < distances[neighbor]: distances[neighbor] = distance heapq.heappush(priority_queue, (distance, neighbor)) return distances ``` #### 结论 上述代码片段展示了针对不同问题类型的通用解决方案。具体到 ZCMU 1912,需要进一步明确其问题描述以选择合适的算法或数据结构[^3]。
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值