• 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. 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.
    官方题解:
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    分享一个博主的做法
    一种比较不靠谱的方法

    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;
    }
    

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

    题目描述
    Today is Jaime’s birthday and, to celebrate, his friends ordered a cake decorated with eggfruits and persimmons. When the cake arrived, to their surprise, they noticed that the bakery didn’t use equal amounts of eggfruits and persimmons, but just randomly distributed the fruits over the cake’s border instead.
    Jaime eats persimmons every day, so he was eager to try some eggfruit on his birthday.
    However, as he doesn’t want to eat too much, his cake slice should be decorated with at most S fruits. Since Jaime doesn’t like when a fruit is cut into parts, each fruit should either be entirely in his slice or be left in the rest of the cake. The problem is, with the fruits distributed in such a chaotic order, his friends are having trouble cutting a suitable slice for him.
    Jaime is about to complain that his friends are taking too long to cut his slice, but in order to do so, he needs to know how many different slices with at least one eggfruit and containing at most S fruits there are. A slice is defined just based on the set of fruits it contains. As Jaime is quite focused on details, he is able to distinguish any two fruits, even if both fruits are of the same type. Hence, two slices are considered different when they do not contain exactly the same set of fruits. The following picture shows a possible cake, as well as the six different slices with at most S = 2 fruits that can be cut from it.

    输入
    The first line contains a circular string B (3 ≤ |B| ≤ 105) describing the border of the cake.Each character of B is either the uppercase letter “E” or the uppercase letter “P”, indicating
    respectively that there’s an eggfruit or a persimmon at the border of the cake. The second line contains an integer S (1 ≤ S < |B|) representing the maximum number of fruits that a slice can contain.
    输出
    Output a single line with an integer indicating the number of different slices with at most S fruits and at least one eggfruit.
    样例输入
    【样例1】
    PEPEP
    2
    【样例2】
    EPE
    1
    【样例3】
    PPPP
    1
    【样例4】
    EPEP
    2
    样例输出
    【样例1】
    6
    【样例2】
    2
    【样例3】
    0
    【样例4】
    6

    题目大意:
    给出一个首尾相接的字符串,问有多少长度不超过 n 的包含字符 ‘E’ 的子串
    方法: 尺取
    代码如下:

    string s;
    int n;
    int main()
    {
        cin >> s >> n;
        int len = s.length();
        s = s + s;
        for(int i=0,r = 0,x = 0;i < len ;i ++){
            while(r + 1 < s.size() && x == 0){
                if(s[r] == 'E') x++;
                r ++;
            }
            ans += max(n - (r - i - 1) ,0);
            if(s[i] == 'E') x --;
        }
        cout << ans <<endl;
        return 0;
    }
    

    队友的暴力代码(据说):

    ll m,n,cnt,a[maxn];
    string sz;
    ll t=1,sum=0;
    int main(){
        cin>>sz>>n;
        ll m=sz.size();
        for(ll i=0;i<m;i++)  sz.push_back(sz[i]);
        for(ll i=0;i<m*2;i++)    if(sz[i]=='E')  a[++cnt]=i;
        if(!cnt){
            cout<<0;
            return 0;
        }
        for(ll i=0;i<m;i++){
            if(a[t]<i&&t<=cnt)    t++;
            if(t>cnt)    break;
            sum+=max(ll(0),i+n-a[t]);
        }
        cout<<sum;
    }
    

    另一个队友的代码:

    const int N = 1e6 + 10;
     
    char s[N];
    int f[N];
     
    int cnt;
    struct node {
        int l, r;
    }q[N];
     
    vector<int> vt;
     
    ll get(ll n, ll x, const int len) {
        if (x == len) {
            return n;
        }
        else if (x >= n) return 0;
        else return n - x;
    }
     
    int main() {
     
        cin >> s + 1;
        int len = strlen(s + 1);
        int n; cin >> n;
     
        int cntE = 0, pos = 0;
        for (int i = len; i >= 1; i--) {
            if (s[i] == 'E') {
                f[i] = len; cntE++;
                pos = i;
            }
            else if (pos != 0) {
                f[i] = pos - i;
            }
        }
        if (cntE == 0) { cout << 0 << endl; return 0; }
        for (int i = len; i >= 1; i--) {
            if (f[i]) break;
            f[i] = len - i + pos;
        }
     
        ll ans = 0;
        for (int i = 1; i <= len; i++) {
            //cout << f[i] << " ";
            ans += get(n, f[i], len);
        }
     
        //puts("");
        cout << ans << endl;
        return 0;
    }
    

    Know your Aliens
    时间限制: 1 Sec 内存限制: 128 MB

    题目描述
    Our world has been invaded by shapeshifting aliens that kidnap people and steal their identities.You are an inspector from a task force dedicated to detect and capture them. As such, you were given special tools to detect aliens and differentiate them from real humans. Your current mission is to visit a city that is suspected of have been invaded, secretly inspect every person there so as to know whose are aliens and whose aren’t, and report it all to Headquarters. Then they can send forces to the city by surprise and capture all the aliens at once. The aliens are aware of the work of inspectors like you, and are monitoring all radio channels to detect the transmission of such reports, in order to anticipate any retaliation. Therefore, there have been several efforts to encrypt the reports, and the most recent method uses polynomials.
    The city you must visit has N citizens, each identified by a distinct even integer from 2 to 2N. You want to find a polynomial P such that, for every citizen i, P(i) > 0 if citizen i is
    a human, and P(i) < 0 otherwise. This polynomial will be transmitted to the Headquarters.With the aim of minimizing bandwidth, the polynomial has some additional requirements:each root and coefficient must be an integer, the coefficient of its highest degree term must be either 1 or −1, and its degree must be the lowest possible.
    For each citizen, you know whether they’re a human or not. Given this information, you must find a polynomial that satisfies the described constraints.
    输入
    The input consists of a single line that contains a string S of length N (1 ≤ N ≤ 104),where N is the population of the city. For i = 1, 2, . . . , N, the i-th character of S is either the uppercase letter “H” or the uppercase letter “A”, indicating respectively that citizen 2i is a human or an alien.
    输出
    The first line must contain an integer D indicating the degree of a polynomial that satisfies the described constraints. The second line must contain D + 1 integers representing the coefficients of the polynomial, in decreasing order of the corresponding terms. It’s guaranteed that there exists at least one solution such that the absolute value of each coefficient is less than 263.
    样例输入
    【样例1】
    HHH
    【样例2】
    AHHA
    【样例3】
    AHHHAH
    样例输出
    【样例1】
    0
    1
    【样例2】
    2
    -1 10 -21
    【样例3】
    3
    1 -23 159 -297
    Code :
    具体的方法

    string s;
    ll x[maxn];
    ll b[maxn];
    int cnt;
    void _Get_x(){
        int len = s.size();
        for(int i=1;i < len-1;i++){
            if(s[i] != s[i+1]) x[++cnt] = 2*i+1;
        }
        cout<<cnt<<endl;
    }
    void _Get_xi(){
        for(int i=1;i<= cnt;i++){
            for(int j=i+1;j>=1;j--){
                b[j] = b[j - 1];
            }
            for(int j=1;j<=i;j++){
                b[j] += b[j+1] * x[i];
            }
        }
    }
    int main()
    {
        b[1] = 1;
        cin >> s;
        s = 'a' + s;
        _Get_x();
        _Get_xi();
        int oper = 1;
        if(s[1] == 'H' && cnt % 2) oper *= -1;
        if(s[1] == 'A' && cnt % 2 == 0) oper *= -1;
        for(int i=cnt + 1;i >= 1;i --){
            printf("%lld",b[i]*oper);
            oper *= -1;
            if(i != 1) printf(" ");
        }
        return 0;
    }
    
  • 相关阅读:
    SQL数据类型详解
    将Excel表格导入DataTable的方法
    .net的反射机制
    经典SQL语句大全(一)
    c# Invoke和BeginInvoke 区别
    c#中两种常用的异步调用方法
    SQL存储过程参数问题
    API 函数大全(下)
    API函数大全 (上)
    javascript 常用function
  • 原文地址:https://www.cnblogs.com/PushyTao/p/14507424.html
Copyright © 2020-2023  润新知