Mutual Training for Wannafly Union #6 E - Summer Trip(并查集)

本文介绍了一个基于并查集算法的学生旅行预算分配问题解决方案。一群学生计划前往海滩旅行,并需要合理分配预算。通过输入学生的数量、关系及各自的资金,算法能够找出不同群体并计算每个群体的总预算。

In this hot summer AIUB students decided to go on a trip to Cox’s Bazaar sea beach. Every student has some amount of money to spend in this trip. Each student belongs to some group. If student A knows student B, B knows C then A, B and C belong to the same group. In order to utilize the budget properly students hired a travel agency to get an optimal travel plan. They submitted the information about all the student’s money and relations among them. Now the agency wants to figure out the number of groups and the total budget of each group. Total budget of a group is the summation of all the student’s money in that group.

Input

The first line contains an integer T (1<=T<=50), denoting the number of test cases.

Each case starts with two integers N M. N (1<=N<=1000), denotes the number of students and M (0<=M<=(N*(N-1)/2)), denotes the number of relationships. In the next line N integers are given. The ith (1<=i<=N) integer ai (1<=ai<=100) denotes the money ith student has. Next M lines each contains two integers u v, (1<=u,v<=N and u != v) denoting that u knows v and vice versa.

Output

For each test case output “Case X: K” where X is the case number starting from 1 and K is the number of groups. In the next line output K integers, the total budget of all the groups in ascending order of budget.

Example

Input:
1
5 3
5 7 4 6 8
1 2
1 3
4 5

Output:
Case 1: 2
14 16
我比赛的时候用的dfs一直错,后来才知道为什么
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
#include<math.h>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<string>
#include<iostream>
#include<algorithm>
#include<utility>
typedef long long ll;
const double Pi = acos(-1.0);
const int N = 1e6+10, M = 1e3+20, mod = 1e9+7, inf = 2e9+10;
const double e=2.718281828459 ;
const double esp=1e-9;
using namespace std;
int pre[M];
int a[M];
int findset(int a)	//带路径压缩
{
    int r=a;
	while(pre[r] !=r)
	{
		r = pre[r];
	}
	int i=a,j;
    while(pre[i]!=r)
    {
        j=pre[i];
        pre[i]=r;
        i=j;
    }
    return r;
}

void union_nodes(int a, int b)     
{
	int a1 = findset(a);
	int b1 = findset(b);
	if(a1 != b1)		
	{
		pre[a1] = b1;		
	}
}
int main()
{
    int t,n,m,cnt=0;
    scanf("%d",&t);
    while(t--)
    {
        ++cnt;
        vector<int>ans,ss;
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            pre[i]=i;//初始化
        }
        for(int i=1;i<=m;i++)
        {
            int x,y;
            scanf("%d %d",&x,&y);
            union_nodes(x,y);
        }
        for(int i=1;i<=n;i++)
        {
            if(pre[i]==i) ss.push_back(i);
        }
        for(int i=0;i<ss.size();i++)
        {
            int sum=0;
            for(int j=1;j<=n;j++)
            {
                if(findset(j)==ss[i])
                    sum+=a[j];
            }
            ans.push_back(sum);
        }
        sort(ans.begin(),ans.end());
        int nn=ans.size();
        printf("Case %d: %d\n",cnt,nn);
        for(int i=0;i<nn;i++)
        printf("%d%c",ans[i],i==nn-1 ?'\n':' ');
    }
    return 0;
}


# ======================== Elasticsearch Configuration ========================= # # NOTE: Elasticsearch comes with reasonable defaults for most settings. # Before you set out to tweak and tune the configuration, make sure you # understand what are you trying to accomplish and the consequences. # # The primary way of configuring a node is via this file. This template lists # the most important settings you may want to configure for a production cluster. # # Please consult the documentation for further information on configuration options: # https://www.elastic.co/guide/en/elasticsearch/reference/index.html # # ---------------------------------- Cluster ----------------------------------- # # Use a descriptive name for your cluster: # #cluster.name: my-application cluster.name: douban-cluster # # ------------------------------------ Node ------------------------------------ # # Use a descriptive name for the node: # #node.name: node-1 node.name: windows-node # # Add custom attributes to the node: # #node.attr.rack: r1 # # ----------------------------------- Paths ------------------------------------ # # Path to directory where to store the data (separate multiple locations by comma): # #path.data: /path/to/data path.data: E:\elasticsearch-9.0.1-windows-x86_64\elasticsearch-9.0.1\data # # Path to log files: # #path.logs: /path/to/logs path.logs: E:\elasticsearch-9.0.1-windows-x86_64\elasticsearch-9.0.1\logs # # ----------------------------------- Memory ----------------------------------- # # Lock the memory on startup: # #bootstrap.memory_lock: true # # Make sure that the heap size is set to about half the memory available # on the system and that the owner of the process is allowed to use this # limit. # # Elasticsearch performs poorly when the system is swapping the memory. # # ---------------------------------- Network ----------------------------------- # # By default Elasticsearch is only accessible on localhost. Set a different # address here to expose this node on the network: # #network.host: 192.168.0.1 network.host: 0.0.0.0 # By default Elasticsearch listens for HTTP traffic on the first free port it # finds starting at 9200. Set a specific HTTP port here: # #http.port: 9200 http.port: 9200 # # For more information, consult the network module documentation. # # --------------------------------- Discovery ---------------------------------- # # Pass an initial list of hosts to perform discovery when this node is started: # The default list of hosts is ["127.0.0.1", "[::1]"] # # discovery.seed_hosts: ["host1", "host2"] # discovery.type: single-node # Bootstrap the cluster using an initial set of master-eligible nodes: # # # For more information, consult the discovery and cluster formation module documentation. # # ---------------------------------- Various ----------------------------------- # # Allow wildcard deletion of indices: # # action.destructive_requires_name: false #----------------------- BEGIN SECURITY AUTO CONFIGURATION ----------------------- # # The following settings, TLS certificates, and keys have been automatically # generated to configure Elasticsearch security features on 26-05-2025 02:15:52 # # -------------------------------------------------------------------------------- # Enable security features xpack.security.enabled: false xpack.security.enrollment.enabled: true # Enable encryption for HTTP API client connections, such as Kibana, Logstash, and Agents xpack.security.http.ssl: enabled: true keystore.path: certs/http.p12 # Enable encryption and mutual authentication between cluster nodes xpack.security.transport.ssl: enabled: true verification_mode: certificate keystore.path: certs/transport.p12 truststore.path: certs/transport.p12 # Create a new cluster with the current node only # Additional nodes can still join the cluster later cluster.initial_master_nodes: ["DESKTOP-QRJI156"] # Allow HTTP API connections from anywhere # Connections are encrypted and require user authentication http.host: 0.0.0.0 # Allow other nodes to join the cluster from anywhere # Connections are encrypted and mutually authenticated #transport.host: 0.0.0.0 #----------------------- END SECURITY AUTO CONFIGURATION ------------------------- 运行出现[2025-05-26T15:23:17,442][ERROR][o.e.b.Elasticsearch ] [windows-node] fatal exception while booting Elasticsearchjava.lang.IllegalArgumentException: setting [cluster.initial_master_nodes] is not allowed when [discovery.type] is set to [single-node] at org.elasticsearch.server@9.0.1/org.elasticsearch.cluster.coordination.ClusterBootstrapService.<init>(ClusterBootstrapService.java:88) at org.elasticsearch.server@9.0.1/org.elasticsearch.cluster.coordination.Coordinator.<init>(Coordinator.java:300) at org.elasticsearch.server@9.0.1/org.elasticsearch.discovery.DiscoveryModule.<init>(DiscoveryModule.java:188) at org.elasticsearch.server@9.0.1/org.elasticsearch.node.NodeConstruction.createDiscoveryModule(NodeConstruction.java:1600) at org.elasticsearch.server@9.0.1/org.elasticsearch.node.NodeConstruction.construct(NodeConstruction.java:1069) See logs for more details. ERROR: Elasticsearch did not exit normally - check the logs at E:\elasticsearch-9.0.1-windows-x86_64\elasticsearch-9.0.1\logs\douban-cluster.log
最新发布
05-27
### 解决方案 当 `discovery.type` 被设置为 `single-node` 时,Elasticsearch 不允许同时配置 `cluster.initial_master_nodes` 参数。这是因为在单节点模式下,Elasticsearch 假设只有一个节点运行,并自动处理主节点选举逻辑[^1]。 如果尝试在同一配置中保留 `cluster.initial_master_nodes`,将会触发以下异常: ```plaintext java.lang.IllegalArgumentException: setting [cluster.initial_master_nodes] is not allowed when [discovery.type] is set to [single-node] ``` 为了避免这一错误,可以采取以下措施之一来调整 Elasticsearch 的配置文件 (`elasticsearch.yml`) 或者 Docker 启动命令中的环境变量。 #### 方法一:移除 `cluster.initial_master_nodes` 配置 在单节点模式下,不需要显式指定初始主节点集合。因此可以直接注释掉或删除 `cluster.initial_master_nodes` 参数。修改后的配置示例如下: ```yaml # cluster.initial_master_nodes: ["node-1"] discovery.type: single-node ``` 通过这种方式,Elasticsearch 将不会抛出非法参数异常[^3]。 #### 方法二:仅使用 `-e "discovery.type=single-node"` 环境变量启动容器 如果通过 Docker 启动 Elasticsearch,则可以通过传递环境变量的方式禁用集群发现机制,而不需额外编辑配置文件。例如: ```bash docker run -d \ -e "discovery.type=single-node" \ -v $(pwd)/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml \ --name elasticsearch \ -p 9200:9200 \ -p 9300:9300 \ elasticsearch:7.10.0 ``` 在此场景下,即使原始配置文件中仍然存在 `cluster.initial_master_nodes` 参数,Docker 中的环境变量会覆盖它并强制启用单节点模式[^1]。 #### 方法三:重新初始化 Keystore 并清理旧数据 某些情况下,可能需要清除之前的 keystore 数据以确保新配置生效。这一步骤尤其适用于曾经作为多节点集群成员运行过的实例。操作流程如下: 1. 进入正在运行的 Elasticsearch 容器 CLI 控制台: ```bash docker exec -it <container_id> /bin/bash ``` 2. 删除现有的 keystore 文件(如果有): ```bash rm -f /usr/share/elasticsearch/config/elasticsearch.keystore ``` 3. 创建新的 keystore 实例: ```bash ./bin/elasticsearch-keystore create ``` 完成以上步骤后重启服务即可应用更改[^3]。 --- ### 总结代码片段 以下是完整的解决方案脚本示例: ```bash # 移除旧版Keystore (可选) docker exec -it <container_id> bash -c &#39;rm -f /usr/share/elasticsearch/config/elasticsearch.keystore&#39; # 初始化新的Keystore docker exec -it <container_id> bash -c &#39;./bin/elasticsearch-keystore create&#39; # 使用单节点模式启动Elasticsearch容器 docker run -d \ -e "discovery.type=single-node" \ -v $(pwd)/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml \ --name elasticsearch \ -p 9200:9200 \ -p 9300:9300 \ elasticsearch:7.10.0 ``` --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值