UPC组队训练-补题记录(上)

简介: Game on a Tree时间限制: 1 Sec 内存限制: 1024 MB题目描述Alice and Bob play a game on a tree. Initially, all nodes are white.Alice is the first to move. She chooses any node and put a chip on it. The node becomes black. After that players take turns.

Game on a Tree

时间限制: 1 Sec  内存限制: 1024 MB


题目描述


Alice and Bob play a game on a tree. Initially, all nodes are white.

Alice is the first to move. She chooses any node and put a chip on it. The node becomes black. After that players take turns. In each turn, a player moves the chip from the current position to an ancestor or descendant node, as long as the node is not black. This node also becomes black. The player who cannot move the chip looses.

Who wins the game?

An ancestor of a node v in a rooted tree is any node on the path between v and the root of the tree.

A descendant of a node v in a rooted tree is any node w such that node v is located on the path between w and the root of the tree.

We consider that the root of the tree is 1.


输入


The first line contains one integer n (1 ≤ n ≤ 100 000) — the number of nodes.

Each of the next n − 1 lines contains two integers u and v (1 ≤ u, v ≤ n) — the edges of the tree. It is guaranteed that they form a tree.


输出


In a single line, print “Alice” (without quotes), if Alice wins. Otherwise, print “Bob”.


样例输入


【样例1】

4
1 2
2 3
3 4

【样例2】

7
2 1
2 6
1 3
2 5
7 2
2 4


样例输出


【样例1】

Bob

【样例2】

Alice


提示


样例解释:


In the first test case, the tree is a straight line and has 4 nodes, so Bob always can choose the last white node.

In the second test case, the optimal strategy for Alice is to place the chip on 3. This node will become black. Bob has to choose the node 1. Alice can choose any of 4, 5, 6, or 7. Bob can only choose 2. Alice chooses any of the white sons of 2, and Bob cannot make a move.


题目大意:


先手可以随意选一个节点并将节点涂黑,然后另一个人只能够在刚刚涂黑的节点的基础上选择这个节点的父节点或者是子节点再将其涂黑,注意,选择的时候不能够选择已经被涂黑的节点,当没有办法操作的时候,对应的人输掉比赛,最后输出赢家是哪一位

当然,Alice是先手

int n;
int dp[maxn];
struct node{
    int u;
    int v;
    int next;
}a[maxn];
int cnt;
int head[maxn];
void _Init(){
    cnt = 0;
    for(int i=0;i<maxn;i++) head[i] = -1;
}
void _Add(int u,int v){
    a[cnt].u = u;
    a[cnt].v = v;
    a[cnt].next = head[u];
    head[u] = cnt ++;
}
void Work(int u,int p){
    dp[u] = 0;
    for(int i=head[u];~i;i = a[i].next){
        int v = a[i].v;
        if(p == v) continue;
        Work(v,u);
        dp[u] += dp[v];
    }
    if(dp[u] == 0) dp[u] = 1;
    else dp[u] -- ;
}
int main()
{
    _Init();
    n=read;
    for(int i=1;i<n;i++){
        int u=read,v=read;
        _Add(u,v);
        _Add(v,u);
    }
    Work(1,1);
    if(dp[1] == 0) puts("Bob");
    else puts("Alice");
    return 0;
}


Graph and Cycles

时间限制: 1 Sec 内存限制: 128 MB


题目描述


There is an undirected weighted complete graph of n vertices where n is odd.


Let’s define a cycle-array of size k as an array of edges [e1,e2,…,ek] that has the following properties:

·k is greater than 1.

·For any i from 1 to k, an edge ei has exactly one common vertex with edge ei−1 and exactly one common vertex with edge ei+1 and these vertices are distinct (consider e0=ek, ek+1=e1).

It is obvious that edges in a cycle-array form a cycle.


Let’s define f(e1,e2) as a function that takes edges e1 and e2 as parameters and returns the maximum between the weights of e1 and e2.


Let’s say that we have a cycle-array C=[e1,e2,…,ek]. Let’s define the price of a cycle-array as the sum of f(ei,ei+1) for all i from 1 to k (consider ek+1=e1).


Let’s define a cycle-split of a graph as a set of non-intersecting cycle-arrays, such that the union of them contains all of the edges of the graph. Let’s define the price of a cycle-split as the sum of prices of the arrays that belong to it.


There might be many possible cycle-splits of a graph. Given a graph, your task is to find the cycle-split with the minimum price and print the price of it.


输入


The first line contains one integer n (3≤n≤999, n is odd) — the number of nodes in the graph.


Each of the following n⋅(n−1)/2 lines contain three space-separated integers u, v and w (1≤u,v≤n,u≠v,1≤w≤109), meaning that there is an edge between the nodes u and v that has weight w.


输出


Print one integer — the minimum possible price of a cycle-split of the graph.


样例输入


【样例1】

3
1 2 1
2 3 1
3 1 1

【样例2】

5
4 5 4
1 3 4
1 2 4
3 2 3
3 5 2
1 4 3
4 2 2
1 5 4
5 2 4
3 4 2


样例输出


【样例1】
3
【样例2】
35


提示


Let’s enumerate each edge in the same way as they appear in the input. I will use ei to represent the edge that appears i-th in the input.

The only possible cycle-split in the first sample is S = {[e1, e2, e3]}. f(e1, e2)+f(e2, e3)+f(e3, e1) = 1+1+1 = 3.

The optimal cycle-split in the second sample is S = {[e3, e8, e9], [e2, e4, e7, e10, e5, e1, e6]}. The price of [e3, e8, e9] is equal to 12, the price of [e2, e4, e7, e10, e5, e1, e6] is equal to 23, thus the price of the split is equal to 35.


官方题解:


微信图片_20220607152854.png微信图片_20220607152922.png微信图片_20220607152933.png

分享一个博主的做法

一种比较不靠谱的方法

vector <int> vet[maxn];
int main()
{
    int n=read;
    int u,v,w;
    while(cin>>u>>v>>w){
        vet[u].push_back(w);
        vet[v].push_back(w);
    }
    ll ans = 0;
    for(int i=1;i<=n;i++){
        sort(vet[i].begin(),vet[i].end());
        for(int j=1;j<=n-1;j++) ans += vet[i][j],j++;
    }
    cout << ans <<endl;
    return 0;
}


另一种方法:

和上面的方法很相似,但是需要先对输入的节点进行排序(按照边权的大小进行排序),然后再对图进行建边操作,然后遍历所有的节点,将与该节点相邻的节点的边进行类似上面方法的操作进行相加求和,最后输出最终的答案即可

代码如下:


struct node{
    int u;
    int v;
    int w;
}a[maxn];
bool cmp(node x,node y){
    return x.w < y.w;
}
int head[maxn];
struct Node{
    ///int u;
    int v;
    int next;
    int w;
}b[maxn];
int cnt;
void _Add(int u,int v,int w){
    ///b[cnt].u = u;
    b[cnt].v = v;
    b[cnt].w = w;
    b[cnt].next = head[u];
    head[u] = cnt ++;
}
void _Init(){
    for(int i=0;i<maxn;i++){
        head[i] = -1;
    }
}
int main()
{
    ll n=read;
    _Init();
    ll lim = (n - 1) * n >> 1;
    for(int i=1;i<=lim;i++){
        a[i].u = read;
        a[i].v = read;
        a[i].w = read;
    }
    sort(a+1,a+1+lim,cmp);
    for(int i=1;i<=lim;i++){
        _Add(a[i].u,a[i].v,a[i].w);
        _Add(a[i].v,a[i].u,a[i].w);
    }
    ll ans = 0;
    for(int i=1;i<=n;i++){
        for(int j=head[i];~j;j = b[j].next){
            ans += b[j].w;
            j = b[j].next;
        }
    }
    cout<< ans <<endl;
    return 0;
}


目录
相关文章
|
5月前
|
算法 图形学 计算机视觉
CVPR 2024:合成视频数据集里只有单人数据?M3Act破解人群行为标注难题
【6月更文挑战第12天】CVPR 2024上的M3Act数据集解决了复杂人群行为标注难题,提供多视角、多群体的合成视频数据,助力计算机视觉研究。利用Unity引擎生成高度真实的人类动作和群体活动,促进以人类为中心任务的学习。实验显示,M3Act能提升目标检测等任务性能,降低数据收集成本,并支持3D群体活动的可控生成。尽管面临数据复杂性、偏差和计算资源限制等问题,M3Act为相关研究提供了宝贵资源。[论文链接](https://arxiv.org/abs/2306.16772)
70 4
|
6月前
|
算法 数据可视化 数据挖掘
JCR一区10.9分|单细胞:有一手数据的肿瘤课题组怎么冲高分文章
这篇文章介绍了在《肿瘤免疫疗法》杂志上发表的一项研究,该研究利用单细胞RNA测序技术揭示了肝细胞癌(HCC)中FABP1(脂肪酸结合蛋白1)依赖的免疫抑制环境。研究分析了II期和III期HCC患者样本的免疫细胞,发现FABP1在III期HCC的肿瘤相关巨噬细胞(TAMs)中过度表达,并与免疫抑制有关。FABP1与PPARG(过氧化物酶体增殖物激活受体伽玛)相互作用,促进了HCC中的脂肪酸氧化,进而影响免疫应答。
89 0
|
6月前
|
数据可视化 Go
快刀斩乱麻,二区7分今年9月发表,孟德尔随机化如何做药靶筛选?
该文章是2023年9月发表在《Journal of Translational Medicine》的孟德尔随机化研究,探索风湿性关节炎(RA)的潜在药物靶点。研究通过遗传学方法鉴定,发现7个可能的药物靶点,这些基因与免疫功能相关,有望为RA药物开发提供新方向,节省成本,并增加临床试验成功的可能性。分析过程包括MR分析、共定位、功能富集和药物预测等步骤。
118 0
|
6月前
|
人工智能
GEE数据的白天day/夜晚night LST数据按照QC掩膜后的结果差异明显
GEE数据的白天day/夜晚night LST数据按照QC掩膜后的结果差异明显
69 0
成信大ENVI_IDL第二周课后作业:提取n个点的气溶胶厚度+详细解析
成信大ENVI_IDL第二周课后作业:提取n个点的气溶胶厚度+详细解析
83 0
|
人工智能
UPC——2020年春混合个人训练第二十四场(DEFG)
UPC——2020年春混合个人训练第二十四场(DEFG)
113 0
UPC——2020年春混合个人训练第二十四场(DEFG)
|
机器学习/深度学习 数据采集 自然语言处理
2022搜狐校园 情感分析 × 推荐排序 算法大赛 baseline
2022搜狐校园 情感分析 × 推荐排序 算法大赛 baseline
803 0
2022搜狐校园 情感分析 × 推荐排序 算法大赛 baseline
|
人工智能 定位技术 Go
UPC——2020年春混合个人训练第二十五场(FG)
UPC——2020年春混合个人训练第二十五场(FG)
92 0
|
监控
UPC——西⽐拉先知系统(分块)
UPC——西⽐拉先知系统(分块)
99 0
|
分布式计算 数据安全/隐私保护
2021-07-21训练日记upc联通数(思维)|赛博朋克(唯一分解)
A. 联通数 题目描述 数学高手小G最近发现了一种新型的数! 他首先在草稿纸写下任意长度的数字串kkkkkkkkkkk…(1≤k≤9)并在其中间添加加号,且相邻两个加号之间至少含有两个数字k (默认数字串第一个数字前与最后一个数字后也有两个加号),然后对其进行求和得出一个新的数。像这样得出的数他将其定义为 “k联通数 ” 。 小G对于他的发现感到非常的自豪, 像数字854就能表示为77+777,因此854是7联通数。 小G现在非常好奇, 究竟有哪些数可以是k联通数呢?他想考验一下你。 询问T次,每次给定两个数n,k,判断 n是否为k联通数, 如果是,输出 YES,否则出 NO。
162 0
2021-07-21训练日记upc联通数(思维)|赛博朋克(唯一分解)
下一篇
无影云桌面