Auxiliary Set HDU - 5927(思维题)

本文介绍了一种针对树形结构的数据查询算法,该算法能够高效地处理一系列关于树中重要节点及其辅助集大小的查询请求。具体而言,对于一棵包含特定重要节点的树,算法需回答每个查询中非重要节点集合所构成的辅助集的规模。

Given a rooted tree with n vertices, some of the vertices are important.

An auxiliary set is a set containing vertices satisfying at least one of the two conditions:

∙It is an important vertex
∙It is the least common ancestor of two different important vertices.

You are given a tree with n vertices (1 is the root) and q queries.

Each query is a set of nodes which indicates the unimportant vertices in the tree. Answer the size (i.e. number of vertices) of the auxiliary set for each query.
Input
The first line contains only one integer T (T≤1000), which indicates the number of test cases.

For each test case, the first line contains two integers n (1≤n≤100000), q (0≤q≤100000).

In the following n -1 lines, the i-th line contains two integers ui,vi(1≤ui,vi≤n) indicating there is an edge between uii and vi in the tree.

In the next q lines, the i-th line first comes with an integer mi(1≤mi≤100000) indicating the number of vertices in the query set.Then comes with mi different integers, indicating the nodes in the query set.

It is guaranteed that ∑qi=1mi≤100000.

It is also guaranteed that the number of test cases in which n≥1000 or ∑qi=1mi≥1000 is no more than 10.
Output
For each test case, first output one line “Case #x:”, where x is the case number (starting from 1).

Then q lines follow, i-th line contains an integer indicating the size of the auxiliary set for each query.
Sample Input
1
6 3
6 4
2 5
5 4
1 5
5 3
3 1 2 3
1 5
3 3 1 4
Sample Output
Case #1:
3
6
3
这题是一个思维题,被唬住了,也没怎么仔细想,现在想来也的确是这么一回事。当时过的人多,的确应该好好思考,也不是不可能想出来。

代码;

#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<vector>
#define maxx 100005
using namespace std;
int fa[maxx],d[maxx];
vector<int> tree[maxx];
int n,q;
int a[maxx];
int son[maxx];
void dfs(int u,int last,int deep)
{
    d[u]=deep;
    fa[u]=last;
    for(int i=0;i<tree[u].size();i++)
    {
        int v=tree[u][i];
        if(v==last)
            continue;
        son[u]++;
        dfs(v,u,deep+1);
    }
}
void init(int x)
{
    for(int i=1;i<=x;i++)
        tree[i].clear();
    memset(son,0,sizeof(son));
}
bool cmp(int x,int y)
{
    return d[x]>d[y];
}
int _size[maxx];
int main()
{
    int t;
    cin>>t;
    int cal=1;
    while(t--)
    {
        scanf("%d%d",&n,&q);
        int x,y;
        init(n);
        for(int i=1;i<n;i++)
        {
            scanf("%d%d",&x,&y);
            tree[x].push_back(y);
            tree[y].push_back(x);
        }
        dfs(1,0,1);
        printf("Case #%d:\n",cal++);
        while(q--)
        {
            int num;
            scanf("%d",&num);
            for(int i=0;i<num;i++)
                scanf("%d",a+i);
            for(int i=0;i<num;i++)
                _size[a[i]]=son[a[i]];
            sort(a,a+num,cmp);
            int ans=n-num;
            for(int i=0;i<num;i++)
            {
                if(_size[a[i]]>=2)
                    ans++;
                else
                    if(!_size[a[i]])
                        _size[fa[a[i]]]--;
            }
            cout<<ans<<endl;
        }
    }
    return 0;
}
### 不使用辅助损失的机器学习模型和技术 在某些情况下,为了简化模型设计或者减少计算复杂度,可以考虑不依赖于辅助损失函数的技术或方法。以下是几种常见的不需要额外辅助损失即可完成训练的模型: #### 传统监督学习模型 传统的监督学习算法通常仅依靠单一的目标函数来进行优化,而无需引入任何形式的辅助损失。例如支持向量机(SVM)[^2]、逻辑回归(Logistic Regression),以及线性回归(Linear Regression)等经典技术都属于此类。 对于图像分类任务中的卷积神经网络(Convolutional Neural Networks,CNNs),如果采用标准交叉熵作为唯一目标,则该CNN也不涉及其他类型的附加约束条件[^1]。 #### 自编码器(Autoencoders) 自编码器是一种无监督特征提取工具,在其基本架构下只追求输入数据与其重建版本之间的差异最小化;即通过均方误差(MSE)或其他相似度衡量指标来调整参数直至达到最佳状态为止。这种单纯基于重构错误率的学习过程同样不存在所谓的“辅助”成分参与其中[^3]。 另外值得注意的是,虽然变分自动编码VAE(variational autoencoder,Vae)会涉及到KL散度项以确保潜在空间分布接近正态分布特性,但这并不完全等同于常规意义上的辅助loss概念因为这是由贝叶斯理论推导出来的必然组成部分而非人为添加进去用于改善性能表现的东西。 #### 强化学习(RL)-Monte Carlo Tree Search (MCTS) 蒙特卡洛树搜索结合深度前馈/循环单元构成策略评估体系时往往只需要定义奖励信号reward signal就足以驱动整个探索开发平衡机制从而找到最优解路径而不用再另行设置别的指导方向了[^2]。 ```python def monte_carlo_tree_search(node): while not node.is_terminal(): if not node.fully_expanded(): return expand_and_evaluate(node) else: node = best_child(node, exploration_weight=EXPLORATION_WEIGHT) return backpropagate(node.result(), node) ``` 以上代码片段展示了如何利用简单的回报反馈循环不断改进决策质量而不必求助外部帮助信息源的例子之一。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值